• Log in or sign up for an account here: https://www.sythe.org/login

[Python] Facebook Archive Message Splitter

Syed

Hero

Retired Sectional Moderator
$50 USD Donor New
Joined
Jan 22, 2009
Sythe Gold
11
Messages
9,857
Location
USA
Sythe Awards 2012 Winner
Sythe Awards 2010 Winner
Gohan has AIDS
Discord Verified
Discord Voice
I'm LAAAAAAAME
Rust Player
Shitting Rainbow
Sythe Awards 2016 Winner
[Python] Facebook Archive Message Splitter
You can download an archive of all your chats, etc, through facebook.com, under account settings -> Download a copy of your data.

You get a messages.html file with all your chats in there, this splits it by person.

Just drop it into a folder where messages.html and the style.css provided by Facebook is, and it should work fine.

I'll probably be dropping a couple random scripts here whenever I make one, for whatever reason, this one was made on somebody's request.

Code:
#!/usr/bin/env python
# -*- coding: utf8 -*- 
import os, re, glob, sys, codecs
from random import randint

# Stupid Facebook splitting shit.
try:
    f = codecs.open('messages.html', 'r', 'utf-8')
except:
    print "No messages.html, exiting."
    raise SystemExit 
lines = f.readlines()

def namefunc(arg):
    global f2
    global fname
    f2 = arg
    names = arg.replace("\n", "").split(', ')
    pers1 = names[0].split('<span class="profile fn">')[1][:-7]
    pers2 = names[1].split('<span class="profile fn">')[1][:-7]
    if pers1 != igname and pers1 != "Unknown" and len(names) == 2:
        fname = pers1.decode('utf-8')
    elif pers1 == igname and len(names) == 2:
        fname = pers2.decode('utf-8')
    elif pers1 == "Unknown":
        fname = "Unknown" + str(randint(100, 999))
    elif len(names) > 2:
        fname = "Group Chat" + str(randint(100,999))

def checkifnooldfiles():
    filelist = glob.glob("*.html")
    for f in filelist:
        if "messages.html" not in f:
            os.remove(f)
    print "Deleted old files"         

def splitfunc(arg, arg2):
    try:
        s = codecs.open(arg2 +".html", "w", "utf-8")
        s.write("""<head>
<title>""" + igname+""" -  Messages </title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" href="style.css" type="text/css"/>
</head>
<div id="bdiv">
<table id="btab"><tr>
<td id="rhs">
<h1>""" + igname +"""</h1>
<div id="content" class="tabmessages">


<div class="thread"><div class="border">
<div class="header">""")
        s.write(arg)
        s.close()
    except:
        print "It fucked up for some reason."
        pass
    
# CONSOLE OUTPUT
print "Make sure the file this script is in contains both messages.html and style.css(optional)\n"
clear = raw_input("Checking if old files in this directory, if so, deleting. If you want to clear files, but not run the script again, type 'clear' now, if not type anything else.")
if clear == "clear":
    checkifnooldfiles()
    print "Everything cleared, script exiting now."
    raise SystemExit
checkifnooldfiles()   
igname = raw_input('Enter your name here, as seen exactly in messages.html\n').decode(sys.stdin.encoding)

for line in lines:
    global f2
    if re.match('<span class="profile fn">(.*)', line):
        namefunc(line)
    if "f2" in vars() and "fname" in vars():
        if re.match(f2, line):
            save = True
            msglist = []
    if "save" in vars():
        msglist.append(line)
    if "</div></div>" in line:
        splitfunc("".join(msglist), fname)
        print "Split and created new file by the name of: "+ fname
        del save
 
[Python] Facebook Archive Message Splitter
vouch for syed i am his important somebody
 
Runescape Gamblingrunescape gambling
Back
Top