2015-03-18 20:04:10 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import HTMLParser, tweepy, os, sqlite3, datetime, dateutil.parser, time
|
|
|
|
|
2015-03-19 17:16:17 +00:00
|
|
|
user = "User Name (without @)"
|
2015-03-18 20:04:10 +00:00
|
|
|
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:
|
2015-03-19 19:45:59 +00:00
|
|
|
text = status.text.encode("UTF-8").replace(".."," ")
|
2015-03-18 20:04:10 +00:00
|
|
|
sender = status.user.screen_name.encode("UTF-8")
|
|
|
|
twid = int(status.id)
|
2015-03-19 18:57:26 +00:00
|
|
|
comment = ""
|
2015-03-19 19:06:42 +00:00
|
|
|
wordlist = []
|
2015-03-19 17:16:17 +00:00
|
|
|
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
|
2015-03-19 18:38:33 +00:00
|
|
|
except Exception, e:
|
|
|
|
print "Error in " + str(twid)
|
|
|
|
print e
|
2015-03-19 17:16:17 +00:00
|
|
|
continue
|
2015-03-18 20:04:10 +00:00
|
|
|
try:
|
2015-03-19 17:16:17 +00:00
|
|
|
date = dateutil.parser.parse(text,dayfirst=True,fuzzy=True)
|
|
|
|
except ValueError, e:
|
2015-03-19 18:38:33 +00:00
|
|
|
print "Error in " + str(twid)
|
2015-03-19 17:16:17 +00:00
|
|
|
print e
|
|
|
|
try:
|
|
|
|
api.update_status("@%s Sorry, ich verstehe deinen Tweet nicht... :(" % sender, twid)
|
|
|
|
except Exception, f:
|
|
|
|
print f
|
2015-03-18 20:04:10 +00:00
|
|
|
continue
|
|
|
|
|
2015-03-19 17:16:17 +00:00
|
|
|
if date < datetime.datetime.now():
|
|
|
|
if date.date() == datetime.datetime.now().date():
|
2015-03-19 17:44:12 +00:00
|
|
|
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
|
2015-03-19 17:16:17 +00:00
|
|
|
else:
|
2015-03-19 18:10:28 +00:00
|
|
|
try:
|
|
|
|
api.update_status("@%s Das war doch schon...?" % sender, twid)
|
|
|
|
except Exception, e:
|
2015-03-19 18:38:33 +00:00
|
|
|
print "Error in " + str(twid)
|
2015-03-19 18:10:28 +00:00
|
|
|
print e
|
2015-03-19 17:16:17 +00:00
|
|
|
continue
|
2015-03-19 19:56:57 +00:00
|
|
|
|
|
|
|
words = text.split(" ")
|
|
|
|
for word in words:
|
|
|
|
wordlist += word
|
|
|
|
|
|
|
|
wordlist = list(set(wordlist))
|
|
|
|
try:
|
2015-03-19 19:58:29 +00:00
|
|
|
wordlist.remove("@"+ user)
|
|
|
|
wordlist.remove("@" + sender)
|
2015-03-19 19:56:57 +00:00
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
2015-03-19 18:57:26 +00:00
|
|
|
cur.execute("INSERT INTO tweets VALUES(%i,'%s','%s','%s',0)" % (twid,date.strftime("%Y-%m-%dT%H:%M:%S"),sender,comment.strip()))
|
2015-03-18 21:50:03 +00:00
|
|
|
sql_conn.commit()
|
2015-03-18 20:04:10 +00:00
|
|
|
|
|
|
|
savepoint = twid
|
2015-03-18 20:33:04 +00:00
|
|
|
|
|
|
|
time.sleep(60)
|