-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathf_twitter_daily.py
executable file
·46 lines (36 loc) · 1.41 KB
/
f_twitter_daily.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
#!/usr/bin/python
# Twitter - Extract fact - Daily
import lyf, logging
import os
from lyf import psql
from datetime import date, timedelta, datetime # Date time
from dateutil.parser import parse # Date parser
def main():
try:
twitter_rec = {}
api = lyf.twitter_api()
me = api.me() # Details about me
twitter_rec['date_id'] = date.today().strftime('%Y%m%d')
twitter_rec['total_followers'] = me.followers_count
twitter_rec['total_following'] = me.friends_count
twitter_rec['total_tweets'] = me.statuses_count
yesterday = date.today() - timedelta(days=1)
yesterday = yesterday.strftime('%Y%m%d')
# Check for yesterday's records to derive today's followers
db = psql.DB()
result = db.query("select * from f_twitter_daily where date_id = '%s'" % yesterday)
if (len(result) > 0):
twitter_rec['followers'] = int(twitter_rec['total_followers']) - int(result[0]['total_followers'])
twitter_rec['following'] = int(twitter_rec['total_following']) - int(result[0]['total_following'])
twitter_rec['tweets'] = int(twitter_rec['total_tweets']) - int(result[0]['total_tweets'])
else:
twitter_rec['followers'] = 0
twitter_rec['following'] = 0
twitter_rec['tweets'] = 0
insert = db.upsert('f_twitter_daily', twitter_rec, ['date_id'])
db.close()
logging.info('Merged %s row into f_twitter_daily.' % insert)
except Exception as err:
logging.error(err)
if __name__ == '__main__':
main()