forked from Nandaka/PixivUtil2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_PixivHelper.py
318 lines (261 loc) · 14.5 KB
/
test_PixivHelper.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
#!C:/Python37-32/python
# -*- coding: UTF-8 -*-
import json
import os
import platform
import unittest
from bs4 import BeautifulSoup
import PixivConfig
import PixivConstant
import PixivHelper
from PixivArtist import PixivArtist
from PixivImage import PixivImage
PixivConstant.PIXIVUTIL_LOG_FILE = 'pixivutil.test.log'
class TestPixivHelper(unittest.TestCase):
currPath = os.path.abspath('.')
PixivHelper.get_logger()
def testSanitizeFilename(self):
rootDir = '.'
filename = u'12345.jpg'
currentDir = os.path.abspath('.')
expected = currentDir + os.sep + filename
result = PixivHelper.sanitize_filename(filename, rootDir)
self.assertEqual(result, expected)
self.assertTrue(len(result) < 255)
def testSanitizeFilename3(self):
rootDir = 'D:\\Temp\\Pixiv2\\'
if platform.system() != 'Windows':
rootDir = '/home/travis/build/Nandaka/PixivUtil2/'
nameformat = '%searchTags%\\%member_id% %member_token%\\%R-18% %urlFilename% - %title%'
p = open('./test/test-image-unicode.htm', 'r', encoding="utf-8")
page = p.read()
image = PixivImage(2493913, page)
self.assertEqual(image.imageUrls[0], "https://i.pximg.net/img-original/img/2008/12/23/21/01/21/2493913_p0.jpg")
filename = PixivHelper.make_filename(nameformat, image, fileUrl="2493913_p0.jpg")
expected = "D:\\Temp\\Pixiv2\\267014 balzehn\\R-18 2493913_p0 - アラクネのいる日常2.jpg"
if platform.system() != 'Windows':
expected = "/home/travis/build/Nandaka/PixivUtil2/267014 balzehn/R-18 2493913_p0 - アラクネのいる日常2.jpg"
result = PixivHelper.sanitize_filename(filename, rootDir)
self.assertEqual(result, expected)
self.assertTrue(len(result) < 255)
def testCreateMangaFilename(self):
p = open(r'./test/test-image-manga.htm', 'r', encoding='utf-8')
page = p.read()
imageInfo = PixivImage(28820443, page)
imageInfo.imageCount = 100
# cross check with json value for artist info
js_file = open('./test/detail-554800.json', 'r')
js = json.load(js_file)
self.assertEqual(imageInfo.artist.artistId, str(js["user"]["id"]))
self.assertEqual(imageInfo.artist.artistToken, js["user"]["account"])
self.assertEqual(imageInfo.artist.artistAvatar, js["user"]["profile_image_urls"]["medium"].replace("_170", ""))
nameFormat = '%member_token% (%member_id%)\\%urlFilename% %page_number% %works_date_only% %works_res% %title%'
expected = u'maidoll (554800)\\28865189_p0 001 2012-07-22 Multiple images: 2P C82おまけ本 「沙耶は俺の嫁」サンプル.jpg'
result = PixivHelper.make_filename(nameFormat,
imageInfo,
artistInfo=None,
tagsSeparator=' ',
fileUrl='http://i2.pixiv.net/img26/img/ffei/28865189_p0.jpg')
# print(result)
self.assertEqual(result, expected)
expected = u'maidoll (554800)\\28865189_p14 015 2012-07-22 Multiple images: 2P C82おまけ本 「沙耶は俺の嫁」サンプル.jpg'
result = PixivHelper.make_filename(nameFormat,
imageInfo,
artistInfo=None,
tagsSeparator=' ',
fileUrl='http://i2.pixiv.net/img26/img/ffei/28865189_p14.jpg')
# print(result)
self.assertEqual(result, expected)
expected = u'maidoll (554800)\\28865189_p921 922 2012-07-22 Multiple images: 2P C82おまけ本 「沙耶は俺の嫁」サンプル.jpg'
result = PixivHelper.make_filename(nameFormat,
imageInfo,
artistInfo=None,
tagsSeparator=' ',
fileUrl='http://i2.pixiv.net/img26/img/ffei/28865189_p921.jpg')
# print(result)
self.assertEqual(result, expected)
def testCreateFilenameUnicode(self):
p = open('./test/test-image-unicode.htm', 'r', encoding='utf-8')
page = p.read()
imageInfo = PixivImage(2493913, page)
# cross check with json value for artist info
js_file = open('./test/detail-267014.json', 'r', encoding='utf-8')
js = json.load(js_file)
self.assertEqual(imageInfo.artist.artistId, str(js["user"]["id"]))
self.assertEqual(imageInfo.artist.artistToken, js["user"]["account"])
self.assertEqual(imageInfo.artist.artistAvatar, js["user"]["profile_image_urls"]["medium"].replace("_170", ""))
nameFormat = '%member_token% (%member_id%)\\%urlFilename% %works_date_only% %works_res% %title%'
expected = u'balzehn (267014)\\2493913 2008-12-23 852x1200 アラクネのいる日常2.jpg'
result = PixivHelper.make_filename(nameFormat,
imageInfo,
artistInfo=None,
tagsSeparator=' ',
fileUrl='http://i2.pixiv.net/img16/img/balzehn/2493913.jpg')
# print(result)
self.assertEqual(result, expected)
def testCreateFilenameTranslatedTag(self):
p = open('./test/test-image-unicode.htm', 'r', encoding='utf-8')
page = p.read()
imageInfo = PixivImage(2493913, page)
# cross check with json value for artist info
js_file = open('./test/detail-267014.json', 'r', encoding='utf-8')
js = json.load(js_file)
self.assertEqual(imageInfo.artist.artistId, str(js["user"]["id"]))
self.assertEqual(imageInfo.artist.artistToken, js["user"]["account"])
self.assertEqual(imageInfo.artist.artistAvatar, js["user"]["profile_image_urls"]["medium"].replace("_170", ""))
nameFormat = '%member_token% (%member_id%)\\%urlFilename% %works_date_only% %works_res% %title% %tags%'
expected = 'balzehn (267014)\\2493913 2008-12-23 852x1200 アラクネのいる日常2 arachne monster girl モン娘のいる日常シリーズ non-human monster girl R-18 tsundere spider woman love-making.jpg'
result = PixivHelper.make_filename(nameFormat,
imageInfo,
artistInfo=None,
tagsSeparator=' ',
fileUrl='http://i2.pixiv.net/img16/img/balzehn/2493913.jpg',
useTranslatedTag=True,
tagTranslationLocale="en")
# print(result)
self.assertEqual(result, expected)
nameFormat2 = '%member_token% (%member_id%)\\folder%force_extension{png}%'
expected2 = 'balzehn (267014)\\folder.png'
result2 = PixivHelper.make_filename(nameFormat2,
imageInfo,
artistInfo=None,
tagsSeparator=' ',
fileUrl='http://i2.pixiv.net/img16/img/balzehn/2493913.jpg',
useTranslatedTag=True,
tagTranslationLocale="en")
# #940
self.assertEqual(result2, expected2)
def testcreateAvatarFilenameFormatNoSubfolderNoRootDir(self):
p = open('./test/all-14095911.json', 'r')
artist = PixivArtist(14095911, p.read(), False, 192, 48)
self.assertIsNotNone(artist)
p2 = open('./test/userdetail-14095911.json', 'r')
info = json.loads(p2.read())
artist.ParseInfo(info, False, False)
targetDir = ''
# change the config value
_config = PixivConfig.PixivConfig()
_config.avatarNameFormat = ''
_config.filenameFormat = '%image_id% - %title%'
_config.tagsSeparator = ' '
_config.tagsLimit = 0
PixivHelper.set_config(_config)
filename = PixivHelper.create_avabg_filename(artist, targetDir, _config)
self.assertEqual(filename[0], self.currPath + os.sep + u'folder.png')
self.assertEqual(filename[1], self.currPath + os.sep + u'bg_folder.jpg')
def testcreateAvatarFilenameFormatNoSubfolderNoRootDir883(self):
p = open('./test/all-4991959.json', 'r')
artist = PixivArtist(4991959, p.read(), False, 192, 48)
self.assertIsNotNone(artist)
p2 = open('./test/userdetail-4991959.json', 'r')
info = json.loads(p2.read())
artist.ParseInfo(info, False, False)
targetDir = ''
# change the config value
_config = PixivConfig.PixivConfig()
_config.avatarNameFormat = ''
_config.filenameFormat = "%artist% (%member_id%)" + os.sep + "%urlFilename% - %title%"
_config.tagsSeparator = ' '
_config.tagsLimit = 0
PixivHelper.set_config(_config)
filename = PixivHelper.create_avabg_filename(artist, targetDir, _config)
self.assertEqual(filename[0], self.currPath + os.sep + f'{artist.artistName} ({artist.artistId}){os.sep}folder.png')
self.assertEqual(filename[1], '')
self.assertTrue(artist.artistAvatar != "no_profile")
self.assertTrue(artist.artistBackground == "no_background")
def testcreateAvatarFilenameFormatWithSubfolderNoRootDir(self):
p = open('./test/all-14095911.json', 'r')
artist = PixivArtist(14095911, p.read(), False, 192, 48)
self.assertIsNotNone(artist)
p2 = open('./test/userdetail-14095911.json', 'r')
info = json.loads(p2.read())
artist.ParseInfo(info, False, False)
targetDir = ''
_config = PixivConfig.PixivConfig()
_config.avatarNameFormat = ''
_config.filenameFormat = '%member_token% (%member_id%)' + os.sep + '%image_id% - %title% - %tags%'
_config.tagsSeparator = ' '
_config.tagsLimit = 0
PixivHelper.set_config(_config)
filename = PixivHelper.create_avabg_filename(artist, targetDir, _config)
self.assertEqual(filename[0], self.currPath + os.sep + u'p199451 (14095911)' + os.sep + 'folder.png')
def testcreateAvatarFilenameFormatNoSubfolderWithRootDir3(self):
p = open('./test/all-14095911.json', 'r')
artist = PixivArtist(14095911, p.read(), False, 192, 48)
self.assertIsNotNone(artist)
p2 = open('./test/userdetail-14095911.json', 'r')
info = json.loads(p2.read())
artist.ParseInfo(info, False, False)
targetDir = os.path.abspath('.')
_config = PixivConfig.PixivConfig()
_config.avatarNameFormat = ''
_config.filenameFormat = '%image_id% - %title%'
_config.tagsSeparator = ' '
_config.tagsLimit = 0
PixivHelper.set_config(_config)
filename = PixivHelper.create_avabg_filename(artist, targetDir, _config)
self.assertEqual(filename[0], targetDir + os.sep + u'folder.png')
def testcreateAvatarFilenameFormatWithSubfolderWithRootDir4(self):
p = open('./test/all-14095911.json', 'r')
artist = PixivArtist(14095911, p.read(), False, 192, 48)
self.assertIsNotNone(artist)
p2 = open('./test/userdetail-14095911.json', 'r')
info = json.loads(p2.read())
artist.ParseInfo(info, False, False)
targetDir = os.path.abspath('.')
_config = PixivConfig.PixivConfig()
_config.avatarNameFormat = ''
_config.filenameFormat = '%member_token% (%member_id%)' + os.sep + '%R-18%' + os.sep + '%image_id% - %title% - %tags%'
_config.tagsSeparator = ' '
_config.tagsLimit = 0
PixivHelper.set_config(_config)
filename = PixivHelper.create_avabg_filename(artist, targetDir, _config)
self.assertEqual(filename[0], targetDir + os.sep + u'p199451 (14095911)' + os.sep + 'folder.png')
def testcreateAvatarFilenameFormatNoSubfolderWithCustomRootDir5(self):
p = open('./test/all-14095911.json', 'r')
artist = PixivArtist(14095911, p.read(), False, 192, 48)
self.assertIsNotNone(artist)
p2 = open('./test/userdetail-14095911.json', 'r')
info = json.loads(p2.read())
artist.ParseInfo(info, False, False)
targetDir = os.path.abspath(os.sep + 'images')
_config = PixivConfig.PixivConfig()
_config.avatarNameFormat = ''
_config.filenameFormat = '%image_id% - %title%'
_config.tagsSeparator = ' '
_config.tagsLimit = 0
PixivHelper.set_config(_config)
filename = PixivHelper.create_avabg_filename(artist, targetDir, _config)
self.assertEqual(filename[0], targetDir + os.sep + 'folder.png')
def testcreateAvatarFilenameFormatWithSubfolderWithCustomRootDir6(self):
p = open('./test/all-14095911.json', 'r')
artist = PixivArtist(14095911, p.read(), False, 192, 48)
self.assertIsNotNone(artist)
p2 = open('./test/userdetail-14095911.json', 'r')
info = json.loads(p2.read())
artist.ParseInfo(info, False, False)
targetDir = os.path.abspath(os.sep + 'images')
_config = PixivConfig.PixivConfig()
_config.avatarNameFormat = ''
_config.filenameFormat = '%member_token% (%member_id%)' + os.sep + '%R-18%' + os.sep + '%image_id% - %title% - %tags%'
_config.tagsSeparator = ' '
_config.tagsLimit = 0
PixivHelper.set_config(_config)
filename = PixivHelper.create_avabg_filename(artist, targetDir, _config)
self.assertEqual(filename[0], targetDir + os.sep + 'p199451 (14095911)' + os.sep + 'folder.png')
def testParseLoginError(self):
p = open('./test/test-login-error.htm', 'r', encoding='utf-8')
page = BeautifulSoup(p.read(), features="html5lib")
r = page.findAll('span', attrs={'class': 'error'})
self.assertTrue(len(r) > 0)
self.assertEqual(u'Please ensure your pixiv ID, email address and password is entered correctly.', r[0].string)
def testParseLoginForm(self):
p = open('./test/test-login-form.html', 'r', encoding='utf-8')
page = BeautifulSoup(p.read(), features="html5lib")
r = page.findAll('form', attrs={'action': '/login.php'})
# print(r)
self.assertTrue(len(r) > 0)
if __name__ == '__main__':
# unittest.main()
suite = unittest.TestLoader().loadTestsFromTestCase(TestPixivHelper)
unittest.TextTestRunner(verbosity=5).run(suite)