Skip to content

Instantly share code, notes, and snippets.

@realdubb
Forked from onekiloparsec/whatsapp2slack.py
Created May 23, 2017 23:01
Show Gist options
  • Select an option

  • Save realdubb/df975731fcc4dcb929ffb28a7e5ea92b to your computer and use it in GitHub Desktop.

Select an option

Save realdubb/df975731fcc4dcb929ffb28a7e5ea92b to your computer and use it in GitHub Desktop.

Revisions

  1. @onekiloparsec onekiloparsec revised this gist Jan 26, 2016. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions whatsapp2slack.py
    Original file line number Diff line number Diff line change
    @@ -81,8 +81,7 @@ def main():
    output_elements = {}


    print("\n{0}Done. Enjoy!\n".format(console_prefix))

    print("\n 🌖 {0}Done. Enjoy!\n".format(console_prefix))

    if __name__ == "__main__":
    main()
  2. @onekiloparsec onekiloparsec created this gist Jan 26, 2016.
    90 changes: 90 additions & 0 deletions whatsapp2slack.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,90 @@
    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-

    import os
    import sys
    import argparse
    import datetime

    def main():
    console_prefix = "$ "
    channel_name = "#whatsapp"
    description = "Transform exported whatsapp discussions into ready-for-import slack.com threads."

    parser = argparse.ArgumentParser(description=description)
    parser.add_argument("input", type=argparse.FileType('r'), help="Input filename")
    parser.add_argument("-c", "--channel", default=channel_name, help="Slack.com channel name, default: "+channel_name)
    parser.add_argument("-o", "--output", type=argparse.FileType('w'), help="Output filename")
    # parser.print_help()

    args = parser.parse_args()

    # Print description in case of parse success
    print("\n 🚀 {0}: {1}\n".format(os.path.basename(sys.argv[0]), description))

    input_file = args.input
    output_file = open("Slack Import "+args.input.name, 'w') if args.output is None else args.output

    print("{0}input filename: '{1}'".format(console_prefix, input_file.name))
    print("{0}output filename: '{1}'".format(console_prefix, output_file.name))
    print("{0}slack channel name: '{1}'".format(console_prefix, channel_name))

    print("{0}Reading input file...".format(console_prefix))
    input_lines = input_file.readlines()
    usernames_mapping = {}

    # Looping through raw lines to group combine lines
    output_line = None
    output_elements = {}

    with open(output_file.name, 'w') as outfile:

    for line in input_lines:
    try:
    dt = datetime.datetime.strptime(line[:19], "%d/%m/%Y %H:%M:%S")
    except ValueError:
    # We cannot find a date, it's a continuation of a line, most probably...
    output_elements["content"] += "\n"+line.strip()
    else:
    if output_elements.get("content", None) is not None:
    new_line = '{0} {1} @{2} "{3}"'.format(output_elements["date"].timestamp(), channel_name, output_elements["username"], output_elements["content"])
    print(new_line)
    outfile.write(new_line+"\n")
    output_elements = {}

    # We can find a date at start of line, it's a new line
    output_line = line.strip()
    output_elements["date"] = dt

    # Make sure to change all double quotes to standard ones
    for quote in ['"', '‟', '″', '˝', '“']:
    output_line = output_line.replace(quote, '\"')

    # Oh, by the way, look for a username. The presence of a username followed by a colon is the only fkag we can use.
    if line[20:].count(':') > 0:
    input_username = line[20:].split(':')[0].strip()
    if input_username not in usernames_mapping.keys():
    output_username = input("\n{0}Unknown username '{1}'. Enter corresponding Slack.com username (<Enter>=identical): ".format(console_prefix, input_username))
    if len(output_username.strip()) > 0:
    usernames_mapping[input_username] = output_username.strip()

    output_username = usernames_mapping.get(input_username, None)
    if output_username is not None:
    output_elements["username"] = output_username
    output_elements["content"] = line[20:].replace(input_username+":", "").strip()

    # We need this to get the last line...
    if output_elements.get("content", None) is not None:
    new_line = '{0} {1} @{2} "{3}"'.format(output_elements["date"].timestamp(), channel_name, output_elements["username"], output_elements["content"])
    print(new_line)
    outfile.write(new_line+"\n")
    output_elements = {}


    print("\n{0}Done. Enjoy!\n".format(console_prefix))


    if __name__ == "__main__":
    main()