-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter.py
76 lines (56 loc) · 2.35 KB
/
twitter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
from datetime import datetime
from google.appengine.ext import webapp, db
from google.appengine.ext.webapp import util
from google.appengine.api.labs import taskqueue
from google.appengine.api import urlfetch
from django.utils import simplejson as json
from oauth import TwitterClient
from post import Post
import config, secret
class TwitterWorker(webapp.RequestHandler):
def update_twitter(self, post, found=False):
try:
client = TwitterClient(secret.CONSUMER_KEY,
secret.CONSUMER_SECRET, "/")
info_url = "http://www.neperczagtu.lv/info/%s" % post.key().id()
params = {}
if found:
params['status'] = "Tikko atrasts velosipēds: %s :)" % info_url
else:
params['status'] = "Tikko pievienots nozagts velosipēds: %s :(" % info_url
response = client.make_request("http://api.twitter.com/1/statuses/update.json",
secret.TOKEN,
secret.TOKEN_SECRET,
params,
True,
urlfetch.POST)
data = json.loads(response.content)
logging.info('Twitter status update response: %s' % data)
except Exception, e:
self.error(500)
logging.exception(e)
return False
return True
def post(self):
key = self.request.get('key')
def txn():
post = db.get(key)
if post.found_at and post.service_updated_at:
# new found bike
if self.update_twitter(post, found=True):
post.service_updated_at = datetime.utcnow()
post.put()
else:
# new stolen bike
if self.update_twitter(post):
post.service_updated_at = datetime.utcnow()
post.put()
db.run_in_transaction(txn)
application = webapp.WSGIApplication([('/post/twitter', TwitterWorker)], debug=config.DEBUG)
def main():
util.run_wsgi_app(application)
if __name__ == '__main__':
main()