-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.py
94 lines (80 loc) · 2.63 KB
/
api.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import tweepy
import os
import json
import brains
from secrets import *
from config import *
import brains
def tweet_to_string(tweet):
return u"\n".join((
u"text: " + unicode(tweet.text),
u"words: " + unicode(tweet.words),
u"tags: " + unicode(tweet.tags),
u"countries: " + unicode(tweet.countries),
u"id: " + unicode(tweet.id),
u"\n",
))
tweepy.models.SearchResult.__repr__ = tweet_to_string
class Tweet():
def __init__(self, text):
self.text = text
class Ask(webapp.RequestHandler):
def post(self):
q = self.request.POST.get('q')
answers = []
for spec in configobject['lexicon']:
res = brains.select(brains.parse(spec, [Tweet(q)]))
for item in res:
answers.append(brains.compose(item).tariff_bot_says)
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(json.dumps(answers))
class Respond(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
api = brains.t_con()
for spec in configobject['lexicon']:
for_me = api.search(configobject['my_handle'])
results = brains.parse(spec, for_me)
results = brains.select(results)
for tweet in results:
tweet.respond = True
self.response.out.write('Tweeted\n')
self.response.out.write(brains.send(brains.compose(tweet)).tariff_bot_says)
self.response.out.write('\nIn response to:\n')
self.response.out.write(tweet.text)
self.response.out.write('\n')
self.response.out.write('\n')
class Search(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
api = brains.t_con()
for spec in configobject['lexicon']:
self.response.out.write('Twitter search for ' + spec['twitter_search_term'] + '\n')
results = api.search(spec['twitter_search_term'])
results = brains.parse(spec, results)
results = brains.select(results, needhashtags = True)
tweeted = []
for result in results:
self.response.out.write(u"found: " + unicode(results))
result.respond = True #respond to everyone now.
tweeted.append(brains.send(brains.compose(result)))
for tweet in tweeted:
self.response.out.write('Tweeted\n')
self.response.out.write(tweet.tariff_bot_says)
self.response.out.write('\nIn response to:\n')
self.response.out.write(tweet.text)
self.response.out.write('\n')
self.response.out.write('\n')
application = webapp.WSGIApplication(
[
('/ask', Ask),
('/search', Search),
('/respond', Respond),
],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()