Skip to content

Commit

Permalink
improve background job
Browse files Browse the repository at this point in the history
  • Loading branch information
vanita5 committed Nov 25, 2014
1 parent e5e26d8 commit 652a667
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 51 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.db
config.py
webForm/since_id
webForm/since_id
*.log
87 changes: 38 additions & 49 deletions webForm/ask.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
import sqlite3
import calendar
import datetime
import time
from flask_limiter import Limiter
from contextlib import closing
from flask import Flask, render_template, request, redirect, g
import logging
logging.basicConfig(filename='logfile.log',level=logging.DEBUG)

# #################
##################
# INITIALIZATION #
##################

Expand All @@ -22,8 +21,9 @@
auth.set_access_token(config.access_token, config.access_token_secret)
twitter = tweepy.API(auth)
ME = twitter.me()
except:
except Exception as e:
print "Twitter authentication failed!"
logging.exception(e)
sys.exit(1)

# init Flask
Expand All @@ -38,13 +38,6 @@
def before_request():
g.db = connect_db()

thread = getattr(g, 'thread', None)
if thread is None:
g.thread = AnswerDownloader()

if not g.thread.isAlive():
g.thread.start()


@app.teardown_request
def teardown_request(exception):
Expand Down Expand Up @@ -119,9 +112,6 @@ def doAsk():
timestamp_utc = calendar.timegm(result.created_at.utctimetuple())
insert_question(tweet_id, question, timestamp_utc)


# TODO persist in database

return redirect('/ask?asked=1')
except Exception as e:
return redirect('/ask?error=1')
Expand Down Expand Up @@ -199,46 +189,45 @@ def ratelimit_handler(e):
return render_template('ask.html', name=config.USERNAME, rate=1)


###############
# CLASS BLOCK #
###############

class AnswerDownloader(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.daemon = True

def run(self):
try:
while True:
since_id = get_since_id()
if len(since_id) > 0:
mentions = twitter.mentions_timeline(since_id)
else:
mentions = twitter.mentions_timeline()

for i in range(len(mentions) - 1, -1, -1):
tweet_id = mentions[i].id
timestamp_utc = calendar.timegm(mentions[i].created_at.utctimetuple())
q_id = mentions[i].in_reply_to_status_id

answer = mentions[i].text
my_screen_name = ME.screen_name
answer = answer.replace('@' + my_screen_name + ' ', '')
try:
insert_answer(q_id, answer, tweet_id, timestamp_utc)
except:
pass
if i == 0:
save_since_id(mentions[i].id)

time.sleep(120)
##################
# BACKGROUND JOB #
##################

except Exception as e:
logging.exception(e)
def answerCrawlerBackgroundJob():
since_id = get_since_id()
print "background job started"
try:
if len(since_id) > 0:
mentions = twitter.mentions_timeline(since_id)
else:
mentions = twitter.mentions_timeline()

for i in range(len(mentions) - 1, -1, -1):
if mentions[i].author.screen_name != config.SCREENNAME:
break
tweet_id = mentions[i].id
timestamp_utc = calendar.timegm(mentions[i].created_at.utctimetuple())
q_id = mentions[i].in_reply_to_status_id

answer = mentions[i].text
my_screen_name = ME.screen_name
answer = answer.replace('@' + my_screen_name + ' ', '')

try:
insert_answer(q_id, answer, tweet_id, timestamp_utc)
except sqlite3.IntegrityError:
pass

if i == 0:
save_since_id(mentions[i].id)
except Exception as e:
logging.exception(e)
pass

threading.Timer(140, answerCrawlerBackgroundJob).start()


if __name__ == '__main__':
init_db()
answerCrawlerBackgroundJob()
app.run(host='0.0.0.0', debug=True)
1 change: 0 additions & 1 deletion webForm/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<title>Ask Me Anything</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Ask @TODO anything anonymously">
<meta name="author" content="">
<link rel="stylesheet" href="{{ url_for('static', filename='bootstrap.min.css') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='bootcards-desktop.min.css') }}">
Expand Down

0 comments on commit 652a667

Please sign in to comment.