forked from Nandaka/PixivUtil2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.PixivModel.py
547 lines (481 loc) · 21 KB
/
test.PixivModel.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#!/c/Python27/python.exe
# -*- coding: UTF-8 -*-
from PixivModel import PixivArtist, PixivImage, PixivBookmark,PixivNewIllustBookmark, PixivTags, PixivGroup
from PixivException import PixivException
from BeautifulSoup import BeautifulSoup
from mechanize import Browser
import os
import unittest
class TestPixivArtist(unittest.TestCase):
def testPixivArtistProfileDataSrc(self):
#print '\nTesting member page ProfileDataSrc'
p = open('./test/test-helper-avatar-name.htm', 'r')
page = BeautifulSoup(p.read())
try:
artist = PixivArtist(1107124, page)
except PixivException as ex:
print ex
page.decompose()
del page
self.assertNotEqual(artist, None)
self.assertEqual(artist.artistId, 1107124)
self.assertEqual(artist.artistToken, 'kirabara29')
def testPixivArtistNoImage(self):
#print '\nTesting member page - no image'
p = open('./test/test-noimage.htm', 'r')
page = BeautifulSoup(p.read())
with self.assertRaises(PixivException):
PixivArtist(1233, page)
page.decompose()
del page
def testPixivArtistNoMember(self):
#print '\nTesting member page - no member'
p = open('./test/test-nouser.htm', 'r')
page = BeautifulSoup(p.read())
with self.assertRaises(PixivException):
PixivArtist(1, page)
page.decompose()
del page
def testPixivArtistNoAvatar(self):
#print '\nTesting member page without avatar image'
p = open('./test/test-member-noavatar.htm', 'r')
page = BeautifulSoup(p.read())
try:
artist = PixivArtist(26357, page)
#artist.PrintInfo()
except PixivException as ex:
print ex
page.decompose()
del page
self.assertNotEqual(artist, None)
self.assertEqual(artist.artistId, 26357)
self.assertEqual(artist.artistToken, 'yukimaruko')
def testPixivArtistSuspended(self):
#print '\nTesting member page - suspended member'
p = open('./test/test-member-suspended.htm', 'r')
page = BeautifulSoup(p.read())
with self.assertRaises(PixivException) as ex:
PixivArtist(123, page)
self.assertEqual(ex.exception.errorCode, 1002)
page.decompose()
del page
def testPixivArtistNotLoggedIn(self):
p = open('./test/test-member-nologin.htm', 'r')
page = BeautifulSoup(p.read())
with self.assertRaises(PixivException) as ex:
PixivArtist(143229, page)
self.assertEqual(ex.exception.errorCode, 100)
page.decompose()
del page
def testPixivArtistBookmark(self):
#print '\nTesting member page'
p = open('./test/test-member-bookmark.htm', 'r')
page = BeautifulSoup(p.read())
try:
artist = PixivArtist(3281699, page)
#artist.PrintInfo()
except PixivException as ex:
print ex
page.decompose()
del page
self.assertNotEqual(artist, None)
self.assertEqual(artist.artistId, 3281699)
def testPixivArtistServerError(self):
#print '\nTesting member page'
p = open('./test/test-server-error.html', 'r')
page = BeautifulSoup(p.read())
with self.assertRaises(PixivException) as ex:
artist = PixivArtist(234753, page)
self.assertEqual(ex.exception.errorCode, PixivException.SERVER_ERROR)
page.decompose()
del page
class TestPixivImage(unittest.TestCase):
def testPixivImageParseInfo(self):
p = open('./test/test-image-info.html', 'r')
page = BeautifulSoup(p.read())
image2 = PixivImage(32039274, page)
page.decompose()
del page
self.assertEqual(image2.imageId, 32039274)
self.assertEqual(image2.imageTitle, u"新しいお姫様")
self.assertTrue(u'MAYU' in image2.imageTags)
self.assertTrue(u'VOCALOID' in image2.imageTags)
self.assertTrue(u'VOCALOID3' in image2.imageTags)
self.assertTrue(u'うさぎになりたい' in image2.imageTags)
self.assertTrue(u'なにこれかわいい' in image2.imageTags)
self.assertTrue(u'やはり存在する斧' in image2.imageTags)
self.assertTrue(u'ヤンデレ' in image2.imageTags)
self.assertTrue(u'吸いこまれそうな瞳の色' in image2.imageTags)
self.assertEqual(image2.imageMode, "big")
self.assertEqual(image2.worksDate,'12-11-2012 00:23')
self.assertEqual(image2.worksResolution,'642x900')
self.assertEqual(image2.worksTools, 'Photoshop SAI')
#self.assertEqual(image2.jd_rtv, 88190)
#self.assertEqual(image2.jd_rtc, 6711)
#self.assertEqual(image2.jd_rtt, 66470)
self.assertEqual(image2.artist.artistToken, 'nardack')
def testPixivImageParseInfoJa(self):
p = open('./test/test-image-parse-image-40273739-ja.html', 'r')
page = BeautifulSoup(p.read())
image2 = PixivImage(40273739, page)
page.decompose()
del page
self.assertEqual(image2.imageId, 40273739)
self.assertEqual(image2.imageTitle, u"Cos-Nurse")
self.assertTrue(u'東方' in image2.imageTags)
self.assertTrue(u'幽々子' in image2.imageTags)
self.assertTrue(u'むちむち' in image2.imageTags)
self.assertTrue(u'おっぱい' in image2.imageTags)
self.assertTrue(u'尻' in image2.imageTags)
self.assertTrue(u'東方グラマラス' in image2.imageTags)
self.assertTrue(u'誰だお前' in image2.imageTags)
self.assertEqual(image2.imageMode, "big")
self.assertEqual(image2.worksDate,u"2013年12月14日 19:00")
self.assertEqual(image2.worksResolution,'855x1133')
self.assertEqual(image2.worksTools, 'Photoshop SAI')
self.assertEqual(image2.artist.artistToken, 'k2321656')
def testPixivImageParseInfoPixivPremiumOffer(self):
p = open('./test/test-image-parse-image-38826533-pixiv-premium.html', 'r')
page = BeautifulSoup(p.read())
image2 = PixivImage(38826533, page)
page.decompose()
del page
self.assertEqual(image2.imageId, 38826533)
self.assertEqual(image2.imageTitle, u"てやり")
self.assertEqual(image2.imageCaption, u'一応シーダ様です。')
self.assertTrue(u'R-18' in image2.imageTags)
self.assertTrue(u'FE' in image2.imageTags)
self.assertTrue(u'ファイアーエムブレム' in image2.imageTags)
self.assertTrue(u'シーダ' in image2.imageTags)
self.assertEqual(image2.imageMode, "big")
self.assertEqual(image2.worksDate,'9-30-2013 01:43')
self.assertEqual(image2.worksResolution,'1000x2317')
self.assertEqual(image2.worksTools, 'CLIP STUDIO PAINT')
#self.assertEqual(image2.jd_rtv, 88190)
#self.assertEqual(image2.jd_rtc, 6711)
#self.assertEqual(image2.jd_rtt, 66470)
self.assertEqual(image2.artist.artistToken, 'hvcv')
def testPixivImageNoAvatar(self):
#print '\nTesting artist page without avatar image'
p = open('./test/test-image-noavatar.htm', 'r')
page = BeautifulSoup(p.read())
image = PixivImage(20496355, page)
page.decompose()
del page
##self.assertNotEqual(image, None)
self.assertEqual(image.artist.artistToken, 'iymt')
self.assertEqual(image.imageId, 20496355)
#07/22/2011 03:09|512×600|RETAS STUDIO
#print image.worksDate, image.worksResolution, image.worksTools
self.assertEqual(image.worksDate,'7-22-2011 03:09')
self.assertEqual(image.worksResolution,'512x600')
self.assertEqual(image.worksTools,'RETAS STUDIO')
def testPixivImageParseTags(self):
p = open('./test/test-image-parse-tags.htm', 'r')
page = BeautifulSoup(p.read())
try:
image = PixivImage(11164869, page)
except PixivException as ex:
print ex
page.decompose()
del page
self.assertNotEqual(image, None)
self.assertEqual(image.imageId, 11164869)
self.assertEqual(image.worksDate,'6-9-2010 02:33')
self.assertEqual(image.worksResolution,'1009x683')
self.assertEqual(image.worksTools,u'SAI')
##print image.imageTags
joinedResult = " ".join(image.imageTags)
self.assertEqual(joinedResult.find("VOCALOID") > -1, True)
def testPixivImageParseNoTags(self):
p = open('./test/test-image-no_tags.htm', 'r')
page = BeautifulSoup(p.read())
try:
image = PixivImage(9175987, page)
except PixivException as ex:
print ex
page.decompose()
del page
self.assertNotEqual(image, None)
self.assertEqual(image.imageId, 9175987)
self.assertEqual(image.worksDate,'3-6-2010 03:04')
self.assertEqual(image.worksResolution,'1155x768')
self.assertEqual(image.worksTools,u'SAI')
self.assertEqual(image.imageTags,[])
def testPixivImageUnicode(self):
#print '\nTesting image page - big'
p = open('./test/test-image-unicode.htm', 'r')
page = BeautifulSoup(p.read())
try:
image = PixivImage(2493913, page)
#image.PrintInfo()
except PixivException as ex:
print ex
page.decompose()
del page
self.assertNotEqual(image, None)
self.assertEqual(image.imageId, 2493913)
self.assertEqual(image.imageMode, 'big')
self.assertEqual(image.worksDate,'12-23-2008 21:01')
self.assertEqual(image.worksResolution,'852x1200')
#print image.worksTools
self.assertEqual(image.worksTools,u'Photoshop SAI つけペン')
def testPixivImageRateCount(self):
p = open('./test/test-image-rate_count.htm', 'r')
page = BeautifulSoup(p.read())
try:
image = PixivImage(28865189, page)
#image.PrintInfo()
except PixivException as ex:
print ex
page.decompose()
del page
self.assertNotEqual(image, None)
self.assertEqual(image.imageId, 28865189)
self.assertEqual(image.imageMode, 'manga')
self.assertTrue(image.jd_rtv > 0)
self.assertTrue(image.jd_rtc > 0)
self.assertTrue(image.jd_rtt > 0)
self.assertEqual(image.worksTools, "Photoshop")
def testPixivImageNoImage(self):
#print '\nTesting image page - no image'
p = open('./test/test-image-noimage.htm', 'r')
page = BeautifulSoup(p.read())
with self.assertRaises(PixivException):
PixivImage(123, page)
page.decompose()
del page
def testPixivImageNoImageEng(self):
#print '\nTesting image page - no image'
p = open('./test/test-image-noimage-eng.htm', 'r')
page = BeautifulSoup(p.read())
with self.assertRaises(PixivException):
PixivImage(123, page)
page.decompose()
del page
def testPixivImageModeManga(self):
#print '\nTesting image page - manga'
p = open('./test/test-image-manga.htm', 'r')
page = BeautifulSoup(p.read())
try:
image = PixivImage(28820443, page)
#image.PrintInfo()
except PixivException as ex:
print ex
page.decompose()
del page
self.assertNotEqual(image, None)
self.assertEqual(image.imageId, 28820443)
self.assertEqual(image.imageMode, 'manga')
def testPixivImageParseBig(self):
#print '\nTesting parse Big Image'
p = open('./test/test-image-parsebig.htm', 'r')
page = BeautifulSoup(p.read())
image = PixivImage()
urls = image.ParseImages(page, mode='big')
self.assertEqual(len(urls), 1)
imageId = urls[0].split('/')[-1].split('.')[0]
#print 'imageId:',imageId
self.assertEqual(int(imageId), 20644633)
def testPixivImageParseManga(self):
#print '\nTesting parse Manga Images'
p = open('./test/test-image-parsemanga.htm', 'r')
page = BeautifulSoup(p.read())
image = PixivImage()
urls = image.ParseImages(page, mode='manga')
#print urls
self.assertEqual(len(urls), 39*2)
imageId = urls[0].split('/')[-1].split('.')[0]
#print 'imageId:',imageId
self.assertEqual(imageId, '20592252_big_p0')
def testPixivImageNoLogin(self):
#print '\nTesting not logged in'
p = open('./test/test-image-nologin.htm', 'r')
page = BeautifulSoup(p.read())
try:
image = PixivImage(9138317, page)
self.assertRaises(PixivException)
except PixivException as ex:
self.assertEqual(ex.errorCode, PixivException.NOT_LOGGED_IN)
def testPixivImageServerError(self):
#print '\nTesting image page'
p = open('./test/test-server-error.html', 'r')
page = BeautifulSoup(p.read())
with self.assertRaises(PixivException) as ex:
image = PixivImage(9138317, page)
self.assertEqual(ex.exception.errorCode, PixivException.SERVER_ERROR)
page.decompose()
del page
def testPixivImageServerError2(self):
#print '\nTesting image page'
p = open('./test/test-image-generic-error.html', 'r')
page = BeautifulSoup(p.read())
with self.assertRaises(PixivException) as ex:
image = PixivImage(37882549, page)
self.assertEqual(ex.exception.errorCode, PixivException.SERVER_ERROR)
page.decompose()
del page
class TestPixivBookmark(unittest.TestCase):
def testPixivBookmarkNewIlust(self):
#print '\nTesting BookmarkNewIlust'
p = open('./test/test-bookmarks_new_ilust.htm', 'r')
page = BeautifulSoup(p.read())
result = PixivNewIllustBookmark(page)
self.assertEqual(len(result.imageList), 20)
def testPixivImageBookmark(self):
#print '\nTesting PixivImageBookmark'
p = open('./test/test-image-bookmark.htm', 'r')
page = BeautifulSoup(p.read())
result = PixivBookmark.parseImageBookmark(page)
self.assertEqual(len(result), 19)
self.assertTrue(35303260 in result)
self.assertTrue(28629066 in result)
self.assertTrue(27249307 in result)
self.assertTrue(30119925 in result)
class TestMyPickPage(unittest.TestCase):
def testMyPickPage(self):
try:
br = Browser()
path = 'file:///' + os.path.abspath('./test/test-image-my_pick.html').replace(os.sep,'/')
p = br.open(path, 'r')
page = BeautifulSoup(p.read())
image = PixivImage(12467674,page)
self.assertRaises(PixivException)
except PixivException as ex:
self.assertEqual(ex.errorCode, 2002)
def testMyPickPageEng(self):
try:
br = Browser()
path = 'file:///' + os.path.abspath('./test/test-image-my_pick-e.html').replace(os.sep,'/')
p = br.open(path, 'r')
page = BeautifulSoup(p.read())
image = PixivImage(28688383,page)
self.assertRaises(PixivException)
except PixivException as ex:
self.assertEqual(ex.errorCode, 2002)
def testGuroPageEng(self):
try:
br = Browser()
path = 'file:///' + os.path.abspath('./test/test-image-guro-e.html').replace(os.sep,'/')
p = br.open(path, 'r')
page = BeautifulSoup(p.read())
image = PixivImage(31111130,page)
self.assertRaises(PixivException)
except PixivException as ex:
self.assertEqual(ex.errorCode, 2005)
def testEroPageEng(self):
try:
br = Browser()
path = 'file:///' + os.path.abspath('./test/test-image-ero-e.html').replace(os.sep,'/')
p = br.open(path, 'r')
page = BeautifulSoup(p.read())
image = PixivImage(31115956,page)
self.assertRaises(PixivException)
except PixivException as ex:
self.assertEqual(ex.errorCode, 2005)
class TestPixivTags(unittest.TestCase):
## tags.php?tag=%E3%81%93%E3%81%AE%E4%B8%AD%E3%81%AB1%E4%BA%BA%E3%80%81%E5%A6%B9%E3%81%8C%E3%81%84%E3%82%8B%21
def testTagsSearchExact(self):
br = Browser()
path = 'file:///' + os.path.abspath('./test/test-tags-search-exact.htm').replace(os.sep,'/')
p = br.open(path, 'r')
page = BeautifulSoup(p.read())
image = PixivTags()
image.parseTags(page)
self.assertEqual(len(image.itemList), 20)
self.assertEqual(image.isLastPage, False)
def testTagsSearchExactLast(self):
br = Browser()
path = 'file:///' + os.path.abspath('./test/test-tags-search-exact-last.htm').replace(os.sep,'/')
p = br.open(path, 'r')
page = BeautifulSoup(p.read())
image = PixivTags()
image.parseTags(page)
##self.assertEqual(len(image.itemList), 3)
self.assertEqual(image.itemList[-1].imageId, 15060554)
self.assertEqual(image.isLastPage, True)
## search.php?s_mode=s_tag&word=%E5%88%9D%E6%98%A5%E9%A3%BE%E5%88%A9
def testTagsSearchPartial(self):
br = Browser()
path = 'file:///' + os.path.abspath('./test/test-tags-search-partial.htm').replace(os.sep,'/')
p = br.open(path, 'r')
page = BeautifulSoup(p.read())
image = PixivTags()
image.parseTags(page)
self.assertEqual(len(image.itemList), 20)
self.assertEqual(image.isLastPage, False)
def testTagsSearchPartialLast(self):
br = Browser()
path = 'file:///' + os.path.abspath('./test/test-tags-search-partial-last.htm').replace(os.sep,'/')
p = br.open(path, 'r')
page = BeautifulSoup(p.read())
image = PixivTags()
image.parseTags(page)
self.assertEqual(image.itemList[-1].imageId, 15060554)
self.assertEqual(image.isLastPage, True)
def testTagsSearchParseDetails(self):
br = Browser()
path = 'file:///' + os.path.abspath('./test/test-tags-search-exact-parse_details.htm').replace(os.sep,'/')
p = br.open(path, 'r')
page = BeautifulSoup(p.read())
image = PixivTags()
image.parseTags(page)
##self.assertEqual(len(image.itemList), 20)
self.assertEqual(image.itemList[-1].imageId, 33815932)
self.assertEqual(image.itemList[-1].bookmarkCount, 4)
self.assertEqual(image.itemList[-1].imageResponse, -1)
def testTagsMemberSearch(self):
br = Browser()
path = 'file:///' + os.path.abspath('./test/test-tags-member-search.htm').replace(os.sep,'/')
p = br.open(path, 'r')
page = BeautifulSoup(p.read())
image = PixivTags()
image.parseMemberTags(page)
self.assertEqual(len(image.itemList), 20)
self.assertEqual(image.itemList[0].imageId, 25757869)
self.assertEqual(image.itemList[19].imageId, 14818847)
self.assertEqual(image.isLastPage, False)
def testTagsMemberSearchLast(self):
br = Browser()
path = 'file:///' + os.path.abspath('./test/test-tags-member-search-last.htm').replace(os.sep,'/')
p = br.open(path, 'r')
page = BeautifulSoup(p.read())
image = PixivTags()
image.parseMemberTags(page)
##self.assertEqual(len(image.itemList), 10)
self.assertEqual(image.itemList[-1].imageId, 1804545)
self.assertEqual(image.isLastPage, True)
def testTagsSkipShowcase(self):
br = Browser()
path = 'file:///' + os.path.abspath('./test/test-tags-search-skip-showcase.htm').replace(os.sep,'/')
p = br.open(path, 'r')
page = BeautifulSoup(p.read())
image = PixivTags()
image.parseTags(page)
self.assertEqual(len(image.itemList), 20)
class TestPixivGroup(unittest.TestCase):
def testParseJson(self):
path = os.path.abspath('./test/group.json').replace(os.sep,'/')
p = open(path)
result = PixivGroup(p)
self.assertEqual(len(result.imageList), 34)
self.assertEqual(len(result.externalImageList), 2)
self.assertEqual(result.maxId, 626288)
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(TestPixivArtist)
unittest.TextTestRunner(verbosity=5).run(suite)
print "================================================================"
suite = unittest.TestLoader().loadTestsFromTestCase(TestPixivImage)
unittest.TextTestRunner(verbosity=5).run(suite)
print "================================================================"
suite = unittest.TestLoader().loadTestsFromTestCase(TestPixivBookmark)
unittest.TextTestRunner(verbosity=5).run(suite)
print "================================================================"
suite = unittest.TestLoader().loadTestsFromTestCase(TestMyPickPage)
unittest.TextTestRunner(verbosity=5).run(suite)
print "================================================================"
suite = unittest.TestLoader().loadTestsFromTestCase(TestPixivTags)
unittest.TextTestRunner(verbosity=5).run(suite)
print "================================================================"
suite = unittest.TestLoader().loadTestsFromTestCase(TestPixivGroup)
unittest.TextTestRunner(verbosity=5).run(suite)