This repository has been archived by the owner on Dec 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
snapshot_twitter.py
executable file
·63 lines (56 loc) · 2.3 KB
/
snapshot_twitter.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
#!/usr/bin/env python
import argparse
import re
from lib.backend import Session
from lib.backend.model import SnapshotOfTwitterAccount
from sqlalchemy import func
from datetime import datetime,timedelta
import twitter
import os
def open_api():
# Connect to a twitter account as configured in the environment
api = twitter.Api(consumer_key=os.getenv('TWITTER_CONSUMER_KEY'),
consumer_secret=os.getenv('TWITTER_CONSUMER_SECRET'),
access_token_key=os.getenv('TWITTER_ACCESS_TOKEN'),
access_token_secret=os.getenv('TWITTER_ACCESS_SECRET'))
api.VerifyCredentials()
return api
def snapshot_twitteraccounts(verbose=False):
"""Create today's SnapshotOfTwitterAccounts"""
api = open_api()
friends = api.GetFriends()
for friend in friends:
if verbose: print 'Scraping %s...' % friend.screen_name
screen_name = friend.screen_name.lower()
if screen_name=='theannotator':
# legacy reasons
screen_name = 'TheAnnotator'
followers = friend.followers_count
following = friend.friends_count
tweets = friend.statuses_count
today = datetime.now().date()
# How long since we scraped this account?
latest = Session.query(SnapshotOfTwitterAccount)\
.filter(SnapshotOfTwitterAccount.screen_name==screen_name)\
.order_by(SnapshotOfTwitterAccount.timestamp.desc())\
.first()
if latest and latest.timestamp>=today:
if verbose: print ' -> most recent snapshot for %s has already been processed.' % screen_name
continue
# Create a snapshot
sn = SnapshotOfTwitterAccount(\
timestamp=today,\
screen_name=screen_name,\
followers=followers,\
following=following,\
tweets=tweets)
Session.add(sn)
if verbose: print ' -> ',sn.toJson()
Session.commit()
def main():
parser = argparse.ArgumentParser(description='Scrape Twitter account information into the database.')
parser.add_argument('-v', '--verbose', action='store_true', dest='verbose', help='Verbose output')
arg = parser.parse_args()
snapshot_twitteraccounts(verbose=arg.verbose)
if __name__=='__main__':
main()