weckbot/getter.py
Klaus-Uwe Mitterer b987f3f381 Fix typo...
2015-03-19 21:14:02 +01:00

91 lines
2.3 KiB
Python
Executable file

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import HTMLParser, tweepy, os, sqlite3, datetime, dateutil.parser, time
user = "User Name (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:
text = status.text.encode("UTF-8").replace(".."," ")
sender = status.user.screen_name.encode("UTF-8")
twid = int(status.id)
comment = ""
wordlist = []
if "storn" in text.lower():
try:
api.update_status("@%s Okay, dein Wecktweet wurde storniert." % sender, twid)
cur.execute("DELETE FROM tweets WHERE sender = %s;" % sender)
sql_conn.commit()
continue
except Exception, e:
print "Error in " + str(twid)
print e
continue
try:
date = dateutil.parser.parse(text,dayfirst=True,fuzzy=True)
except ValueError, e:
print "Error in " + str(twid)
print e
try:
api.update_status("@%s Sorry, ich verstehe deinen Tweet nicht... :(" % sender, twid)
except Exception, f:
print f
continue
if date < datetime.datetime.now():
if date.date() == datetime.datetime.now().date():
if (not date == datetime.datetime.now().replace(hour = 0, minute = 0, second = 0, microsecond = 0)) or "0:0" in text:
date += datetime.timedelta(days = 1)
else:
continue
else:
try:
api.update_status("@%s Das war doch schon...?" % sender, twid)
except Exception, e:
print "Error in " + str(twid)
print e
continue
words = text.split(" ")
for word in words:
if word[0] == "@":
wordlist += [word]
wordlist = list(set(wordlist))
try:
wordlist.remove("@"+ user)
wordlist.remove("@" + sender)
except ValueError:
pass
cur.execute("INSERT INTO tweets VALUES(%i,'%s','%s','%s',0)" % (twid,date.strftime("%Y-%m-%dT%H:%M:%S"),sender,comment.strip()))
sql_conn.commit()
savepoint = twid
time.sleep(60)