-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfb-activity-tracker.py
101 lines (73 loc) · 2.55 KB
/
fb-activity-tracker.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/python
# Facebook Activity Tracker
# made by Dante383
# github.com/Dante383/fb-activity-tracker
# USAGE: python fb-activity-tracker.py
# [--config=<path to a config file>]
# [--env=<dev or prod>]
# [--user=<account email>]
# [--password=<account password>]
#
# CLI variables have bigger priority than the config file
import logging, argparse, yaml, sys
import TrackerClient
class ActivityTracker:
defaultConfig = {
'env': 'dev',
'db': 'updates.db'
}
clients = []
def __init__ (self):
parser = argparse.ArgumentParser()
parser.add_argument('--env', type=str, default='dev', help='prod or dev')
parser.add_argument('--config', type=str, default='config.yaml', help='Config file path')
parser.add_argument('--db', type=str, default='updates.db', help='Database file')
parser.add_argument('--user', type=str, help='User email (optional)')
parser.add_argument('--password', type=str, help='User password (optional)')
args = vars(parser.parse_args())
logging.basicConfig(format="%(asctime)s %(message)s")
self.logger = logging.getLogger('fb-activity-tracker')
self.config = self.parseConfig(args)
if (self.config['env'] == 'dev'):
self.logger.setLevel('DEBUG')
else:
self.logger.setLevel('CRITICAL');
if (not 'accounts' in self.config):
self.logger.error('No accounts found!')
self.exit(1)
for account in self.config['accounts']:
client = TrackerClient.TrackerClient(account, self.logger, self.config)
client.listen()
self.clients.append(client)
def parseConfig (self, args):
# is config set?
if (args['config']):
stream = False
try:
stream = open(args['config'], 'r')
except FileNotFoundError:
self.logger.warning('Config not found! Using default variables')
config = self.defaultConfig
pass
if (stream != False):
config = yaml.load(stream)
else:
config = self.defaultConfig
# but, if we have --user and --password, we will want to set it as accounts[0]
if (not 'accounts' in config):
if (args['user']):
# no password? lets ask for it interactively
if (not args['password']):
args['password'] = input('Password for ' + args['user'] + ':')
# okay, but its not going to work like this.
config['accounts'] = [{'username': args['user'], 'password': args['password']}]
del args['user'], args['password']
del args['config']
# merge config and our command line arguments
config = {**config, **args}
return config
def exit (self, status=0):
self.logger.info('Exitting...')
sys.exit(status)
if (__name__ == '__main__'):
ActivityTracker()