-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddPage.py
347 lines (276 loc) · 9.62 KB
/
addPage.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import sys, random, collections
import textGen
import pymysql
import png
import platform
import time
from switch import switch
'''
PageRow = collections.namedtuple('pageRow', "id site_id next_id uri_path")
t = PageROw(*row)
t.id
t.site_id
t.next_id
t.uri_path
'''
def AddXKCD(cur):
#check if it is the proper number of day updates. Each time this is run it is considered 1 day
try:
f = open('xkcd.day', 'r+')
day = int(f.read())
f.close()
except Exception as e:
f = open('xkcd.day', 'w')
day = 0
f.write(str(day))
f.close()
#increment day by 1
f = open('xkcd.day', 'w')
f.write(str((day+1)%7))
f.close()
if not (day in [1, 3, 5]):
print('Day Skipped');
return
#find site ID
cur.execute("SELECT id FROM sites WHERE class = 'XKCD'")
for row in cur.fetchall():
siteId, = row
break
#get last page
cur.execute("SELECT * FROM pages WHERE site_id = {0} AND next_id IS NULL".format(siteId))
firstPageExists = False
for row in cur.fetchall():
firstPageExists = True
id, site_id, next_id, uri_path = row
lastPage = {'id': id, 'site_id': site_id, 'next_id': next_id, 'uri_path': uri_path}
break
newId = None
if firstPageExists:
#build next page uri
nextUri = '/' + str(int(lastPage['uri_path'][1:-1]) + 1) + '/'
#insert new page
cur.execute("INSERT INTO pages (site_id, uri_path) VALUES ({0}, '{1}')".format(siteId, nextUri))
#find the new id
cur.execute("SELECT LAST_INSERT_ID()")
for row in cur.fetchall():
newId, = row
break
#point next page to the new page
cur.execute("UPDATE pages SET next_id = {0} WHERE id = {1}".format(newId, lastPage['id']))
else:
#make first uri
nextUri = '/1/';
#insert first page
cur.execute("INSERT INTO pages (site_id, uri_path) VALUES({0}, '{1}')".format(siteId, nextUri))
#find the new id
cur.execute("SELECT LAST_INSERT_ID()")
for row in cur.fetchall():
newId, = row
break
###
#add variables for the new page
#make the title
title = textGen.BuildPhrase(random.randint(2,5), False).title()
hover = textGen.BuildParagraph(random.randint(1,3))
#add title
cur.execute("INSERT INTO components (`key`, `value`) VALUES('{0}', '{1}')".format('title', title))
#find the title id
cur.execute("SELECT LAST_INSERT_ID()")
for row in cur.fetchall():
titleId, = row
break
#add hover
cur.execute("INSERT INTO components (`key`, `value`) VALUES('{0}', '{1}')".format('hover', hover))
#find the hover id
cur.execute("SELECT LAST_INSERT_ID()")
for row in cur.fetchall():
hoverId, = row
break
#link the components to the page
cur.execute("INSERT INTO page_components (page_id, component_id) VALUES({0}, {1}), ({0}, {2})".format(newId, titleId, hoverId))
###
#generate lowercase title
lowTitle = title.lower().replace(' ', '_')
try:
#create comic in appropriate spot
f = open("webroot/www.xkcd.com/comics/{0}.png".format(lowTitle), 'wb') # binary mode is important
width = random.randint(248, 713)
height = random.randint(217, 437)
w = png.Writer(width, height)
#build image noise
lines = []
for y in range(height):
row = []
for x in range(width):
row.append(random.randint(0, 255))
row.append(random.randint(0, 255))
row.append(random.randint(0, 255))
lines.append(row)
#write lines to files
w.write(f, lines)
f.close()
except Exception as e:
print(e)
print("New Page (id:{0}) added to XKCD".format(newId))
path = 'www.xkcd.com.list' #this is the "domain" field in the database + '.list'
#we are running on windows, use ./ as path
if platform.system() == 'Windows':
path = './' + path
#we are running on server, use /usr/local/bin/ as path
else :
path = '/usr/local/bin/' + path
try:
f = open(path, 'a+')
f.write("http://www.xkcd.com{0}\n".format(nextUri))
f.close()
except Exception as e:
print(e)
def AddDoomsDayMyDear(cur):
#check if it is the proper number of day updates. Each time this is run it is considered 1 day
try:
f = open('doomsdaymydear.day', 'r+')
day = int(f.read())
f.close()
except Exception as e:
f = open('doomsdaymydear.day', 'w')
day = 0
f.write(str(day))
f.close()
#increment day by 1
f = open('doomsdaymydear.day', 'w')
f.write(str((day+1)%7))
f.close()
if not (day in [1, 5]):
print('Day Skipped');
return
#wait 0-4 hours (5sec = 1 hour for accelerated time)
duration = random.random() * 4 * 5
print("Waiting {0} seconds".format(duration))
time.sleep(duration)
#find site ID
cur.execute("SELECT id FROM sites WHERE class = 'DoomsDayMyDear'")
for row in cur.fetchall():
siteId, = row
break
#get last page
cur.execute("SELECT * FROM pages WHERE site_id = {0} AND next_id IS NULL".format(siteId))
firstPageExists = False
for row in cur.fetchall():
firstPageExists = True
id, site_id, next_id, uri_path = row
lastPage = {'id': id, 'site_id': site_id, 'next_id': next_id, 'uri_path': uri_path}
break
#there is already a page
if firstPageExists:
#build next page uri
nextId = int(lastPage['uri_path'][5:]) + 1
nextUri = '/?id=' + str(nextId)
#insert new page
cur.execute("INSERT INTO pages (site_id, uri_path) VALUES ({0}, '{1}')".format(siteId, nextUri))
#find the new id
cur.execute("SELECT LAST_INSERT_ID()")
for row in cur.fetchall():
newId, = row
break
#point next page to the new page
cur.execute("UPDATE pages SET next_id = {0} WHERE id = {1}".format(newId, lastPage['id']))
else:
#make first uri
nextId = 1
firstUri = '/?id=1';
#insert first page
cur.execute("INSERT INTO pages (site_id, uri_path) VALUES({0}, '{1}')".format(siteId, firstUri))
#find the new id
cur.execute("SELECT LAST_INSERT_ID()")
for row in cur.fetchall():
newId, = row
break
###
#add variables for the new page
#build news item
cur.execute("SELECT MAX(value) FROM components WHERE `key` LIKE 'news_id'")
for row in cur.fetchall():
maxNewsId, = row
if maxNewsId == None:
maxNewsId = 0
break
news_title = textGen.BuildPhrase(random.randint(2, 5), False).title()
news_content = textGen.BuildParagraph(random.randint(1,3))
news_id = int(maxNewsId) + 1
#add news title
cur.execute("INSERT INTO components (`key`, `value`) VALUES('{0}', '{1}')".format('news_title', news_title))
#find the title id
cur.execute("SELECT LAST_INSERT_ID()")
for row in cur.fetchall():
titleId, = row
break
#add news content
cur.execute("INSERT INTO components (`key`, `value`) VALUES('{0}', '{1}')".format('news_content', news_content))
#find the hover id
cur.execute("SELECT LAST_INSERT_ID()")
for row in cur.fetchall():
contentId, = row
break
#add news id
cur.execute("INSERT INTO components (`key`, `value`) VALUES('{0}', '{1}')".format('news_id', news_id))
#find the hover id
cur.execute("SELECT LAST_INSERT_ID()")
for row in cur.fetchall():
newsId, = row
break
#link the components to the page
cur.execute("INSERT INTO page_components (page_id, component_id) VALUES({0}, {1}), ({0}, {2}), ({0}, {3})".format(newId, titleId, contentId, newsId))
###
#create comic in appropriate spot
try:
f = open("webroot/www.doomsdaymydear.com/img/comic/{0}.png".format(nextId), 'wb') # binary mode is important
width = 800
height = 1140
w = png.Writer(width, height)
#build image noise
lines = []
for y in range(height):
row = []
for x in range(width):
row.append(random.randint(0, 255))
row.append(random.randint(0, 255))
row.append(random.randint(0, 255))
lines.append(row)
#write lines to files
w.write(f, lines)
f.close()
except Exception as e:
print(e)
print("New Page (id:{0}) added to DoomsDayMyDear".format(newId))
path = 'www.doomsdaymydear.com.list' #this is the "domain" field in the database + '.list'
#we are running on windows, use ./ as path
if platform.system() == 'Windows':
path = './' + path
#we are running on server, use /usr/local/bin/ as path
else :
path = '/usr/local/bin/' + path
try:
f = open(path, 'a+')
f.write("http://www.doomsdaymydear.com/?id={0}\n".format(nextId))
f.close()
except Exception as e:
print(e)
if __name__ == '__main__':
site = sys.argv[1]
#connect to mysql
conn = pymysql.connect(host='localhost', port=3306, user='ohmu', passwd='TGSTGSTGS', db='crts')
cur = conn.cursor()
for case in switch(site):
if case('XKCD'):
AddXKCD(cur)
break
if case('DoomsDayMyDear'):
AddDoomsDayMyDear(cur)
break
if case():
print ("That site isn't recognized!")
break
#more cases here
#close connections
cur.close()
conn.close()