/
util.py
182 lines (144 loc) · 5.02 KB
/
util.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import os
import datetime
import logging as log
from datetime import datetime
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.api import users
from models import *
import time
class MainHandler(webapp.RequestHandler):
def get(self):
path = os.path.join(os.path.dirname(__file__), 'templates/util.html')
user = users.get_current_user()
if user:
logout_url = users.create_logout_url("/")
template_values = {
'logout_url': logout_url,
'current_user': user.nickname(),
'companies': Company.all()
}
self.response.out.write(template.render(path, template_values))
class LoadAll(webapp.RequestHandler):
def get(self):
load_words()
load_companies()
self.response.out.write('Done')
class KillAll(webapp.RequestHandler):
def get(self):
template_values = {}
path = os.path.join(os.path.dirname(__file__), 'templates/killall.html')
self.response.out.write(template.render(path, template_values))
class Kill(webapp.RequestHandler):
def get(self):
entity = self.request.get('ent')
q = db.GqlQuery("SELECT __key__ FROM " + entity)
db.delete(q.fetch(500))
template_values = {
'ent': entity,
'count': q.count(1)
}
path = os.path.join(os.path.dirname(__file__), 'templates/kill.html')
self.response.out.write(template.render(path, template_values))
class WordsLoader(webapp.RequestHandler):
def get(self):
load_words()
self.response.out.write('Done')
class CompanyLoader(webapp.RequestHandler):
def get(self):
load_companies()
self.response.out.write('Done')
def load_words():
for line in open(os.path.realpath('./data/adjectives.dat')):
line = line.replace('\n', '').replace(' ', '')
seperator = line.find(':')
word = line[0:seperator]
value = line[seperator+1:len(line)]
log.debug('word:' + word + ' - value:' + value)
word_db = Word.all().filter("word = ", word).get()
if word_db is None:
word_db = Word(word=word, value=int(value))
else:
word_db.word=word
word_db.value=int(value)
word_db.put()
def loadTerm(company, text, display_text):
term = Term.all().filter("company = ", company).filter("text = ", text).get()
if term is None:
term = Term(company=company, text=text, display_text=display_text)
else:
term.display_text = display_text
term.put()
def loadFalseTerm(company, text):
term = FalseTerm.all().filter("company = ", company).filter("text = ", text).get()
if term is None:
term = FalseTerm(company=company, text=text)
term.put()
def loadCompany(name):
comp = Company.all().filter("name = ", name).get()
if comp is None:
comp = Company(name=name)
comp.put()
return comp
def load_companies():
apple = loadCompany("Apple")
loadTerm(apple, "apple", "Apple")
loadTerm(apple, "aapl", "AAPL")
loadTerm(apple, "mac", "Mac")
loadTerm(apple, "ipod", "iPod")
loadTerm(apple, "ipad", "iPad")
loadTerm(apple, "iphone", "iPhone")
loadTerm(apple, "imac", "iMac")
loadTerm(apple, "macbook", "Macbook")
loadFalseTerm(apple, "apple pie")
loadFalseTerm(apple, "apple cider")
loadFalseTerm(apple, "apple juice")
loadFalseTerm(apple, "apple caramel")
loadFalseTerm(apple, "candy apple")
loadFalseTerm(apple, "big apple")
loadFalseTerm(apple, "breakfast")
loadFalseTerm(apple, "dinner")
loadFalseTerm(apple, "mac n cheese")
loadFalseTerm(apple, "mac and cheese")
loadFalseTerm(apple, "big mac")
google = loadCompany("Google")
loadTerm(google, "google", "Google")
loadTerm(google, "goog", "GOOG")
loadTerm(google, "gmail", "Gmail")
loadTerm(google, "adwords", "AdWords")
loadTerm(google, "youtube", "YouTube")
lockheed = loadCompany("Lockheed")
loadTerm(lockheed, "lockheed", "Lockheed")
loadTerm(lockheed, "lmt", "LMT")
loadTerm(lockheed, "f-35", "F-35")
loadTerm(lockheed, "f-22", "F-22")
loadTerm(lockheed, "orion", "Orion")
verizon = loadCompany("Verizon")
loadTerm(verizon, "verizon", "Verizon")
loadTerm(verizon, "vz", "VZ")
loadTerm(verizon, "fios", "FIOS")
comcast = loadCompany("Comcast")
loadTerm(comcast, "comcast", "Comcast")
loadTerm(comcast, "cmcsa", "CMCSA")
loadTerm(comcast, "xfinity", "xfinity")
for company in Company.all():
company.query = None
for term in Term.all().filter('company =', company):
if company.query is not None:
company.query += "+OR+"
else:
company.query = ""
company.query += term.text
company.put()
def main():
application = webapp.WSGIApplication([
('/util/', MainHandler),
('/util/killall', KillAll),
('/util/kill', Kill),
('/util/loadall', LoadAll)
],
debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()