-
Notifications
You must be signed in to change notification settings - Fork 100
/
parse.py
84 lines (69 loc) · 3.58 KB
/
parse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import logging.config
import sys
from parsers.config import config
from utils import ArgParseDefault
USAGE_DESC = """
python parse.py <command> [<args>]
Available commands:
telegram Parse logs from telegram
hangouts Parse logs from hangouts
messenger Parse logs from messenger
whatsapp Parse logs from whatsapp
"""
def add_common_parse_arguments(parser):
parser.add_argument('--own-name', dest='own_name', type=str, default=None, help='Name of the owner of the chat logs, written as in the logs',
required=False)
parser.add_argument('--max', '--max-exported-messages', dest='max', type=int, default=config['MAX_EXPORTED_MESSAGES'],
help='Maximum number of messages to export')
return parser
def str2bool(v):
return v != 'false'
class ArgParse():
def __init__(self):
logging.config.fileConfig('logging.conf')
parser = ArgParseDefault(
description='',
usage=USAGE_DESC)
parser.add_argument('command', help='Subcommand to run')
args = parser.parse_args(sys.argv[1:2])
if not hasattr(self, args.command):
print('Unrecognized command')
parser.print_help()
sys.exit(1)
getattr(self, args.command)()
def telegram(self):
from parsers.telegram import main
parser = ArgParseDefault(description='Parse message logs from Telegram')
parser = add_common_parse_arguments(parser)
parser.add_argument('--max-dialog', dest='max_dialog', type=int, default=config['telegram']['USER_DIALOG_MESSAGES_LIMIT'],
help='Maximum number of messages to export per dialog')
args = parser.parse_args(sys.argv[2:])
main(args.own_name, max_exported_messages=args.max, user_dialog_messages_limit=args.max_dialog)
def hangouts(self):
from parsers.hangouts import main
parser = ArgParseDefault(description='Parse message logs from Google Hangouts')
parser = add_common_parse_arguments(parser)
parser.add_argument('-f', '--file-path', dest='file_path', default=config['hangouts']['DEFAULT_RAW_LOCATION'],
help='Path to Hangouts chat log file (json file)')
args = parser.parse_args(sys.argv[2:])
main(args.own_name, args.file_path, args.max)
def messenger(self):
from parsers.messenger import main
parser = ArgParseDefault(description='Parse message logs from Facebook Messenger')
parser = add_common_parse_arguments(parser)
parser.add_argument('-f', '--file-path', dest='file_path', default=config['messenger']['DEFAULT_RAW_LOCATION'],
help='Path to Facebook messenger chat log folder')
args = parser.parse_args(sys.argv[2:])
main(args.own_name, args.file_path, args.max)
def whatsapp(self):
from parsers.whatsapp import main
parser = ArgParseDefault(description='Parse message logs from Whatsapp')
parser = add_common_parse_arguments(parser)
parser.add_argument('-f', '--file-path', dest='file_path', default=config['whatsapp']['DEFAULT_RAW_LOCATION'],
help='Path to Facebook messenger chat log folder')
parser.add_argument('-i', '--infer-datetime', dest='infer_datetime', type=str2bool, nargs='?',
default=True, help='Infer datetime regex for each chat if true')
args = parser.parse_args(sys.argv[2:])
main(args.own_name, args.file_path, args.max, args.infer_datetime)
if __name__ == '__main__':
ArgParse()