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
user = "Twitter Handle (without @)"
user = "User Name (without @)"
cke = "Consumer Key"
cse = "Consumer Secret"
ato = "Access Token"
@ -31,24 +31,35 @@ while True:
timeline = tweepy.Cursor(api.search, q=search, since_id=savepoint).items()
for status in timeline:
if status.text.find("@" + user) > 1:
continue
text = status.text.encode("UTF-8")[status.text.find(" ")+1:].strip()
text = status.text.encode("UTF-8")
sender = status.user.screen_name.encode("UTF-8")
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:
if "-" in text:
loc = text.find("-")
date = dateutil.parser.parse(text[:loc-1],dayfirst=True,fuzzy=True)
comment = HTMLParser.HTMLParser().unescape(text[loc+1:]).strip()
else:
date = dateutil.parser.parse(text,dayfirst=True,fuzzy=True)
comment = ""
except ValueError:
api.update_status("@%s Sorry, ich verstehe deinen Tweet nicht... :(" % sender, twid)
date = dateutil.parser.parse(text,dayfirst=True,fuzzy=True)
except ValueError, e:
print e
try:
api.update_status("@%s Sorry, ich verstehe deinen Tweet nicht... :(" % sender, twid)
except Exception, f:
print f
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()
savepoint = twid

View file

@ -1,7 +1,7 @@
#!/usr/bin/env python
# -*- 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
# the getter and the tweeter.
@ -28,8 +28,16 @@ while True:
original = int(status[0])
recipient = status[2]
comment = status[3]
try:
api.update_status("@%s Es wär soweit... :)" % recipient, original)
try:
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)
sql_conn.commit()
except: