forked from Nandaka/PixivUtil2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_PixivHelper.py
253 lines (211 loc) · 11.9 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
#!C:/Python37-32/python
# -*- coding: UTF-8 -*-
import json
import os
import platform
import unittest
from bs4 import BeautifulSoup
import PixivHelper
from PixivImage import PixivImage
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 testSanitizeFilename2(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-f.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% %works_tools% %title%'
expected = u'maidoll (554800)\\28865189_p0 001 07/22/12 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 07/22/12 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 07/22/12 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% %works_tools% %title%'
expected = u'balzehn (267014)\\2493913 12/23/08 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 12/23/08 852x1200 アラクネのいる日常2 arachne monster girl モン娘のいる日常シリーズ non-human monster girl R-18 tsundere spider woman.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)
# def testcreateAvatarFilenameFormatNoSubfolderNoRootDir(self):
# p = open('./test/test-helper-avatar-name.htm', 'r')
# page = BeautifulSoup(p.read())
# artist = PixivArtist(mid=1107124, page=page)
# targetDir = ''
# # change the config value
# _config = PixivConfig.PixivConfig()
# _config.avatarNameFormat = ''
# _config.filenameFormat = '%image_id% - %title%'
# _config.tagsSeparator = ' '
# _config.tagsLimit = 0
# PixivHelper.setConfig(_config)
# filename = PixivHelper.createAvatarFilename(artist, targetDir)
# self.assertEqual(filename, self.currPath + os.sep + u'folder.jpg')
# def testcreateAvatarFilenameFormatWithSubfolderNoRootDir(self):
# p = open('./test/test-helper-avatar-name.htm', 'r')
# page = BeautifulSoup(p.read())
# artist = PixivArtist(mid=1107124, page=page)
# targetDir = ''
# _config = PixivConfig.PixivConfig()
# _config.avatarNameFormat = ''
# _config.filenameFormat = '%member_token% (%member_id%)' + os.sep + '%image_id% - %title% - %tags%'
# _config.tagsSeparator = ' '
# _config.tagsLimit = 0
# PixivHelper.setConfig(_config)
# filename = PixivHelper.createAvatarFilename(artist, targetDir)
# self.assertEqual(filename, self.currPath + os.sep + u'kirabara29 (1107124)' + os.sep + 'folder.jpg')
# def testcreateAvatarFilenameFormatNoSubfolderWithRootDir3(self):
# p = open('./test/test-helper-avatar-name.htm', 'r')
# page = BeautifulSoup(p.read())
# artist = PixivArtist(mid=1107124, page=page)
# targetDir = os.path.abspath('.')
# _config = PixivConfig.PixivConfig()
# _config.avatarNameFormat = ''
# _config.filenameFormat = '%image_id% - %title%'
# _config.tagsSeparator = ' '
# _config.tagsLimit = 0
# filename = PixivHelper.createAvatarFilename(artist, targetDir)
# self.assertEqual(filename, targetDir + os.sep + u'folder.jpg')
# def testcreateAvatarFilenameFormatWithSubfolderWithRootDir4(self):
# p = open('./test/test-helper-avatar-name.htm', 'r')
# page = BeautifulSoup(p.read())
# artist = PixivArtist(mid=1107124, page=page)
# 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
# filename = PixivHelper.createAvatarFilename(artist, targetDir)
# self.assertEqual(filename, targetDir + os.sep + u'kirabara29 (1107124)' + os.sep + 'folder.jpg')
# def testcreateAvatarFilenameFormatNoSubfolderWithCustomRootDir5(self):
# p = open('./test/test-helper-avatar-name.htm', 'r')
# page = BeautifulSoup(p.read())
# artist = PixivArtist(mid=1107124, page=page)
# targetDir = os.path.abspath(os.sep + 'images')
# _config = PixivConfig.PixivConfig()
# _config.avatarNameFormat = ''
# _config.filenameFormat = '%image_id% - %title%'
# _config.tagsSeparator = ' '
# _config.tagsLimit = 0
# filename = PixivHelper.createAvatarFilename(artist, targetDir)
# self.assertEqual(filename, targetDir + os.sep + 'folder.jpg')
# def testcreateAvatarFilenameFormatWithSubfolderWithCustomRootDir6(self):
# p = open('./test/test-helper-avatar-name.htm', 'r')
# page = BeautifulSoup(p.read())
# artist = PixivArtist(mid=1107124, page=page)
# 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
# filename = PixivHelper.createAvatarFilename(artist, targetDir)
# self.assertEqual(filename, targetDir + os.sep + 'kirabara29 (1107124)' + os.sep + 'folder.jpg')
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)