weckbot/getter.py

57 lines
1.5 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import HTMLParser, tweepy, os, sqlite3, datetime, dateutil.parser, time
user = "Twitter Handle (without @)"
cke = "Consumer Key"
cse = "Consumer Secret"
ato = "Access Token"
ase = "Access Secret"
database_filename = "Database.db"
search = "@" + user
auth = tweepy.OAuthHandler(cke, cse)
auth.set_access_token(ato, ase)
api = tweepy.API(auth)
sql_conn = sqlite3.connect(database_filename)
cur = sql_conn.cursor()
try:
cur.execute("SELECT max(tweet_id) FROM tweets")
savepoint = int(cur.fetchone()[0])
except:
savepoint = ""
print "No savepoint found. Trying to get as many results as possible."
while True:
timeline = tweepy.Cursor(api.search, q=search, since_id=savepoint).items()
for status in timeline:
if status.text.find("@" + user) > 1:
continue
2015-03-18 22:25:15 +00:00
text = status.text.encode("UTF-8")[status.text.find(" ")+1:].strip()
sender = status.user.screen_name.encode("UTF-8")
twid = int(status.id)
try:
if "-" in text:
2015-03-18 22:14:27 +00:00
loc = text.find("-")
2015-03-18 22:51:26 +00:00
date = dateutil.parser.parse(text[:loc-1],dayfirst=True,fuzzy=True)
2015-03-18 22:25:15 +00:00
comment = HTMLParser.HTMLParser().unescape(text[loc+1:]).strip()
else:
2015-03-18 22:51:26 +00:00
date = dateutil.parser.parse(text,dayfirst=True,fuzzy=True)
2015-03-18 20:27:17 +00:00
comment = ""
except ValueError:
api.update_status("@%s Sorry, ich verstehe deinen Tweet nicht... :(" % sender, twid)
continue
cur.execute("INSERT INTO tweets VALUES(%i,'%s','%s','%s',0)" % (twid,date.strftime("%Y-%m-%dT%H:%M:%S"),sender,comment))
2015-03-18 21:50:03 +00:00
sql_conn.commit()
savepoint = twid
time.sleep(60)