[Python] Facebook Archive Message Splitter

Discussion in 'Programming General' started by Syed, Jan 27, 2013.

[Python] Facebook Archive Message Splitter
  1. Unread #1 - Jan 27, 2013 at 1:32 PM
  2. Syed
    Joined:
    Jan 22, 2009
    Posts:
    9,857
    Referrals:
    1
    Sythe Gold:
    11
    Sythe Awards 2012 Winner Gohan has AIDS (3) ??? Rust Player I'm LAAAAAAAME (2) Shitting Rainbow (2)

    Syed Hero
    Retired Sectional Moderator $50 USD Donor New

    [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
     
  3. Unread #2 - Jan 27, 2013 at 2:31 PM
  4. Jei jei KK
    Joined:
    Aug 26, 2005
    Posts:
    224
    Referrals:
    0
    Sythe Gold:
    0

    Jei jei KK Active Member

    [Python] Facebook Archive Message Splitter

    vouch for syed i am his important somebody
     
< What languages do you know? | Looking for a Programmer // Simple Bank Program >

Users viewing this thread
1 guest


 
 
Adblock breaks this site