Skip to content

Instantly share code, notes, and snippets.

@giubil
Last active June 17, 2017 11:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giubil/27d015c7a3ac196967de0b3deb05b9ff to your computer and use it in GitHub Desktop.
Save giubil/27d015c7a3ac196967de0b3deb05b9ff to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import markovify
import argparse
import json
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument("data_dir", help="The path to the directory containing your messages, extracted with fbchat-archive-parser, in json format")
args = parser.parse_args()
return args.data_dir
def get_all_messages_from_participant(convo):
participant = convo['participants'][0]
buff_convo = convo["messages"]
corpus = ""
for elem in buff_convo:
if elem['sender'] == participant:
corpus = corpus + elem['message'] + "\n"
return corpus
def main():
data_dir = parse_arguments()
f = open(data_dir + "/manifest.txt", "r")
f.readline()
f.readline()
for line in f:
print (line, end='')
f.close()
convo_number = input("Please enter the number of the convo you want to analyse : ")
f = open(data_dir + "thread_{}.json".format(convo_number), "r")
convo = json.load(f)
f.close()
corpus = get_all_messages_from_participant(convo)
model = markovify.NewlineText(corpus)
print(chr(27) + "[2J")
while True:
print (model.make_sentence(), end='')
input("")
if __name__ == "__main__":
main()
@giubil
Copy link
Author

giubil commented Jun 17, 2017

To generate the files needed for this to work, you need to download the archive facebook provides with all your contents.

Then you need to install a package that parses those files :

pip install fbchat-archive-parser

And then launch the extraction (I recommend using --resolve, which requires your facebook crendentials, to resolve facebook ids to logins)

fbcap /path/to/messages.htm -f json -d . --resolve

Then launch this script :

python3 messenger_bot.py /path/to/fbchat_dump_XXXXXXX

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment