-
Notifications
You must be signed in to change notification settings - Fork 0
/
twiner.py
75 lines (59 loc) · 2.26 KB
/
twiner.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
import ConfigParser
import datetime
import io
import sys
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from api import Api
import models
def run(path_to_conf='conf.ini', **kwargs):
conf = ConfigParser.SafeConfigParser()
with io.open(path_to_conf, 'rb') as fd:
conf.readfp(fd)
params = {}
for key, val in conf.items('params'):
if key in ['trim_user', 'exclude_replies', 'contributor_details',
'include_rts']:
params[key] = int(conf.getboolean('params', key))
else:
params[key] = val
params.update(kwargs)
try:
db_url = conf.get('database', 'url')
except ConfigParser.NoOptionError:
if conf.get('database', 'type') == 'sqlite':
db_url = 'sqlite://{0}'.format(conf.get('database', 'name'))
else:
db_url = '{0}://{1}:{2}@{3}/{4}'.format(
conf.get('database', 'type'),
conf.get('database', 'user'),
conf.get('database', 'password'),
conf.get('database', 'host'),
conf.get('database', 'name')
)
engine = create_engine(db_url)
models.Base.metadata.create_all(bind=engine)
Session = sessionmaker(bind=engine)
session = Session()
api = Api(conf.get('api', 'consumer_key'), conf.get('api',
'consumer_secret'))
for tl in session.query(models.Timeline):
if not tl.last_update or datetime.datetime.now() >= \
tl.last_update + tl.interval:
last_tweet = tl.tweets.order_by(models.Tweet.id.desc()).first()
if last_tweet:
params['since_id'] = last_tweet.id
elif 'since_id' in params:
del params['since_id']
tweets = api.get_user_timeline(params=params,
screen_name=tl.screen_name)
for tweet in tweets:
if not session.query(models.Tweet).get(tweet['id']):
tl.tweets.append(models.Tweet(tweet))
tl.last_update = datetime.datetime.now()
session.commit()
if __name__ == '__main__':
if len(sys.argv) == 1:
run()
else:
run(sys.argv[1])