Improving behavior in several ways

This commit is contained in:
Klaus-Uwe Mitterer 2015-03-19 18:16:17 +01:00
parent 10001b0c28
commit 1b8f4ec533
2 changed files with 36 additions and 17 deletions

View file

@ -3,7 +3,7 @@
import HTMLParser, tweepy, os, sqlite3, datetime, dateutil.parser, time import HTMLParser, tweepy, os, sqlite3, datetime, dateutil.parser, time
user = "Twitter Handle (without @)" user = "User Name (without @)"
cke = "Consumer Key" cke = "Consumer Key"
cse = "Consumer Secret" cse = "Consumer Secret"
ato = "Access Token" ato = "Access Token"
@ -31,24 +31,35 @@ while True:
timeline = tweepy.Cursor(api.search, q=search, since_id=savepoint).items() timeline = tweepy.Cursor(api.search, q=search, since_id=savepoint).items()
for status in timeline: for status in timeline:
if status.text.find("@" + user) > 1: text = status.text.encode("UTF-8")
continue
text = status.text.encode("UTF-8")[status.text.find(" ")+1:].strip()
sender = status.user.screen_name.encode("UTF-8") sender = status.user.screen_name.encode("UTF-8")
twid = int(status.id) twid = int(status.id)
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:
continue
try: try:
if "-" in text: date = dateutil.parser.parse(text,dayfirst=True,fuzzy=True)
loc = text.find("-") except ValueError, e:
date = dateutil.parser.parse(text[:loc-1],dayfirst=True,fuzzy=True) print e
comment = HTMLParser.HTMLParser().unescape(text[loc+1:]).strip() try:
else: api.update_status("@%s Sorry, ich verstehe deinen Tweet nicht... :(" % sender, twid)
date = dateutil.parser.parse(text,dayfirst=True,fuzzy=True) except Exception, f:
comment = "" print f
except ValueError:
api.update_status("@%s Sorry, ich verstehe deinen Tweet nicht... :(" % sender, twid)
continue continue
cur.execute("INSERT INTO tweets VALUES(%i,'%s','%s','%s',0)" % (twid,date.strftime("%Y-%m-%dT%H:%M:%S"),sender,comment)) if date < datetime.datetime.now():
if date.date() == datetime.datetime.now().date():
date += datetime.timedelta(days = 1)
else:
api.update_status("@%s Das war doch schon...?" % sender, twid)
continue
cur.execute("INSERT INTO tweets VALUES(%i,'%s','%s','',0)" % (twid,date.strftime("%Y-%m-%dT%H:%M:%S"),sender))
sql_conn.commit() sql_conn.commit()
savepoint = twid savepoint = twid

View file

@ -1,7 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import HTMLParser, tweepy, os, sqlite3, time import HTMLParser, tweepy, os, sqlite3, time, random
# Notice: You may want to create seperate apps (or even accounts) for # Notice: You may want to create seperate apps (or even accounts) for
# the getter and the tweeter. # the getter and the tweeter.
@ -28,8 +28,16 @@ while True:
original = int(status[0]) original = int(status[0])
recipient = status[2] recipient = status[2]
comment = status[3] comment = status[3]
try: try:
api.update_status("@%s Es wär soweit... :)" % recipient, original) greeting = random.randint(1,3)
if greeting == 1:
text = "Es ist soweit."
elif greeting == 2:
text = "Die Zeit ist gekommen."
else:
text = "Guten Morgen. :P"
api.update_status("@%s %s" % (recipient, text), original)
cur.execute("UPDATE tweets SET sent = 1 WHERE tweet_id = %i" % original) cur.execute("UPDATE tweets SET sent = 1 WHERE tweet_id = %i" % original)
sql_conn.commit() sql_conn.commit()
except: except: