forked from Nandaka/PixivUtil2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPixivBrowserFactory.py
770 lines (659 loc) · 31.2 KB
/
PixivBrowserFactory.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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
# -*- coding: utf-8 -*-
# pylint: disable=W0603, C0325
from __future__ import print_function
import cookielib
import httplib
import json
import re
import socket
import sys
import time
from datetime import datetime, timedelta
import urllib
import urllib2
import urlparse
import uuid
import demjson
import mechanize
import socks
from BeautifulSoup import BeautifulSoup
import PixivHelper
import PixivModel
import PixivModelWhiteCube
from PixivException import PixivException
from PixivModelFanbox import Fanbox, FanboxArtist
from PixivOAuth import PixivOAuth
defaultCookieJar = None
defaultConfig = None
_browser = None
# pylint: disable=E1101
class PixivBrowser(mechanize.Browser):
_config = None
_isWhitecube = False
_whitecubeToken = ""
_cache = dict()
_myId = 0
_username = None
_password = None
_oauth_manager = None
def _put_to_cache(self, key, item, expiration=3600):
expiry = time.time() + expiration
self._cache[key] = (item, expiry)
def _get_from_cache(self, key):
if key in self._cache.keys():
(item, expiry) = self._cache[key]
if expiry - time.time() > 0:
return item
else:
del item
self._cache.pop(key)
return None
def __init__(self, config, cookie_jar):
# fix #218
try:
mechanize.Browser.__init__(self, factory=mechanize.RobustFactory())
except BaseException:
PixivHelper.GetLogger().info("Using default factory (mechanize 3.x ?)")
mechanize.Browser.__init__(self)
self._configureBrowser(config)
self._configureCookie(cookie_jar)
def clear_history(self):
mechanize.Browser.clear_history(self)
return
def back(self):
mechanize.Browser.back(self)
return
def _configureBrowser(self, config):
if config is None:
PixivHelper.GetLogger().info("No config given")
return
global defaultConfig
if defaultConfig is None:
defaultConfig = config
self._config = config
if config.useProxy:
if config.proxyAddress.startswith('socks'):
parseResult = urlparse.urlparse(config.proxyAddress)
assert parseResult.scheme and parseResult.hostname and parseResult.port
socksType = socks.PROXY_TYPE_SOCKS5 if parseResult.scheme == 'socks5' else socks.PROXY_TYPE_SOCKS4
socks.setdefaultproxy(socksType, parseResult.hostname, parseResult.port)
socks.wrapmodule(urllib)
socks.wrapmodule(urllib2)
socks.wrapmodule(httplib)
PixivHelper.GetLogger().info("Using SOCKS Proxy: %s", config.proxyAddress)
else:
self.set_proxies(config.proxy)
PixivHelper.GetLogger().info("Using Proxy: %s", config.proxyAddress)
# self.set_handle_equiv(True)
# self.set_handle_gzip(True)
self.set_handle_redirect(True)
self.set_handle_referer(True)
self.set_handle_robots(False)
self.set_debug_http(config.debugHttp)
if config.debugHttp:
PixivHelper.GetLogger().info('Debug HTTP enabled.')
# self.visit_response
self.addheaders = [('User-agent', config.useragent)]
# force utf-8, fix issue #184
self.addheaders = [('Accept-Charset', 'utf-8')]
socket.setdefaulttimeout(config.timeout)
def _configureCookie(self, cookie_jar):
if cookie_jar is not None:
self.set_cookiejar(cookie_jar)
global defaultCookieJar
if defaultCookieJar is None:
defaultCookieJar = cookie_jar
def addCookie(self, cookie):
global defaultCookieJar
if defaultCookieJar is None:
defaultCookieJar = cookielib.LWPCookieJar()
defaultCookieJar.set_cookie(cookie)
def open_with_retry(self, url, data=None,
timeout=mechanize._sockettimeout._GLOBAL_DEFAULT_TIMEOUT,
retry=0):
retry_count = 0
if retry == 0 and self._config is not None:
retry = self._config.retry
while True:
try:
return self.open(url, data, timeout)
except Exception as ex:
if isinstance(ex, urllib2.HTTPError):
raise
if retry_count < retry:
for t in range(1, self._config.retryWait):
print(t, end=' ')
time.sleep(1)
print('')
retry_count = retry_count + 1
else:
raise PixivException("Failed to get page: {0}, please check your internet connection/firewall/antivirus.".format(ex.message), errorCode=PixivException.SERVER_ERROR)
def getPixivPage(self, url, referer="https://www.pixiv.net", returnParsed=True):
''' get page from pixiv and return as parsed BeautifulSoup object or response object.
throw PixivException as server error
'''
url = self.fixUrl(url)
retry_count = 0
while True:
req = urllib2.Request(url)
req.add_header('Referer', referer)
try:
page = self.open_with_retry(req)
if returnParsed:
parsedPage = BeautifulSoup(page.read())
return parsedPage
else:
return page
except Exception as ex:
if isinstance(ex, urllib2.HTTPError):
if ex.code in [403, 404, 503]:
return BeautifulSoup(ex.read())
if retry_count < self._config.retry:
for t in range(1, self._config.retryWait):
print(t, end=' ')
time.sleep(1)
print('')
retry_count = retry_count + 1
else:
raise PixivException("Failed to get page: " + ex.message, errorCode=PixivException.SERVER_ERROR)
def fixUrl(self, url, useHttps=True):
# url = str(url)
if not url.startswith("http"):
if not url.startswith("/"):
url = "/" + url
if useHttps:
return "https://www.pixiv.net" + url
else:
return "http://www.pixiv.net" + url
return url
def _loadCookie(self, cookie_value):
""" Load cookie to the Browser instance """
ck = cookielib.Cookie(version=0, name='PHPSESSID', value=cookie_value, port=None,
port_specified=False, domain='pixiv.net', domain_specified=False,
domain_initial_dot=False, path='/', path_specified=True,
secure=False, expires=None, discard=True, comment=None,
comment_url=None, rest={'HttpOnly': None}, rfc2109=False)
self.addCookie(ck)
## cookies = cookie_value.split(";")
## for cookie in cookies:
## temp = cookie.split("=")
## name = temp[0].strip()
## value= temp[1] if len(temp) > 1 else ""
## ck = cookielib.Cookie(version=0, name=name, value=value, port=None,
## port_specified=False, domain='pixiv.net', domain_specified=False,
## domain_initial_dot=False, path='/', path_specified=True,
## secure=False, expires=None, discard=True, comment=None,
## comment_url=None, rest={'HttpOnly': None}, rfc2109=False)
## self.addCookie(ck)
def _getInitConfig(self, page):
init_config = page.find('input', attrs={'id': 'init-config'})
js_init_config = json.loads(init_config['value'])
return js_init_config
def loginUsingCookie(self, login_cookie=None):
""" Log in to Pixiv using saved cookie, return True if success """
if login_cookie is None or len(login_cookie) == 0:
login_cookie = self._config.cookie
if len(login_cookie) > 0:
PixivHelper.print_and_log('info', 'Trying to log in with saved cookie')
self._loadCookie(login_cookie)
res = self.open_with_retry('https://www.pixiv.net/mypage.php')
resData = res.read()
parsed = BeautifulSoup(resData)
if "logout.php" in resData:
PixivHelper.print_and_log('info', 'Login successful.')
PixivHelper.GetLogger().info('Logged in using cookie')
self.getMyId(parsed)
return True
else:
PixivHelper.GetLogger().info('Failed to log in using cookie')
PixivHelper.print_and_log('info', 'Cookie already expired/invalid.')
return False
def login(self, username, password):
try:
PixivHelper.print_and_log('info', 'Logging in...')
url = "https://accounts.pixiv.net/login"
page = self.open_with_retry(url)
# get the post key
parsed = BeautifulSoup(page)
js_init_config = self._getInitConfig(parsed)
data = {}
data['pixiv_id'] = username
data['password'] = password
data['captcha'] = ''
data['g_recaptcha_response'] = ''
data['return_to'] = 'https://www.pixiv.net'
data['lang'] = 'en'
data['post_key'] = js_init_config["pixivAccount.postKey"]
data['source'] = "accounts"
data['ref'] = ''
request = urllib2.Request("https://accounts.pixiv.net/api/login?lang=en", urllib.urlencode(data))
response = self.open_with_retry(request)
return self.processLoginResult(response, username, password)
except BaseException:
PixivHelper.print_and_log('error', 'Error at login(): {0}'.format(sys.exc_info()))
raise
def processLoginResult(self, response, username, password):
PixivHelper.GetLogger().info('Logging in, return url: %s', response.geturl())
# check the returned json
js = response.read()
PixivHelper.GetLogger().info(str(js))
result = json.loads(js)
# Fix Issue #181
if result["body"] is not None and result["body"].has_key("success"):
# cookie.value = self._ua_handlers['_cookies']
# PixivHelper.print_and_log('info', 'new cookie value: ' + str(cookie.value))
# self._config.cookie = cookie.value
# self._config.writeConfig(path=self._config.configFileLocation)
for cookie in self._ua_handlers['_cookies'].cookiejar:
if cookie.name == 'PHPSESSID':
PixivHelper.print_and_log('info', 'new cookie value: ' + str(cookie.value))
self._config.cookie = cookie.value
self._config.writeConfig(path=self._config.configFileLocation)
break
# check whitecube
page = self.open_with_retry(result["body"]["success"]["return_to"])
parsed = BeautifulSoup(page)
self.getMyId(parsed)
# store the username and password in memory for oAuth login
self._config.username = username
self._config.password = password
return True
else:
if result["body"] is not None and result["body"].has_key("validation_errors"):
PixivHelper.print_and_log('info', "Server reply: " + str(result["body"]["validation_errors"]))
else:
PixivHelper.print_and_log('info', 'Unknown login issue, please use cookie login method.')
return False
def getMyId(self, parsed):
''' Assume from main page '''
# pixiv.user.id = "189816";
temp = re.findall(r"pixiv.user.id = \"(\d+)\";", unicode(parsed))
if temp is not None:
self._myId = int(temp[0])
PixivHelper.print_and_log('info', 'My User Id: {0}.'.format(self._myId))
else:
PixivHelper.print_and_log('info', 'Unable to get User Id')
def parseLoginError(self, res):
page = BeautifulSoup(res.read())
r = page.findAll('span', attrs={'class': 'error'})
return r
def getImagePage(self, image_id, parent=None, from_bookmark=False,
bookmark_count=-1, image_response_count=-1):
image = None
response = None
PixivHelper.GetLogger().debug("Getting image page: %s", image_id)
url = "https://www.pixiv.net/member_illust.php?mode=medium&illust_id={0}".format(image_id)
# response = self.open(url).read()
response = self.getPixivPage(url, returnParsed=False).read()
self.handleDebugMediumPage(response, image_id)
# Issue #355 new ui handler
image = None
try:
if response.find("globalInitData") > 0:
PixivHelper.print_and_log('debug', 'New UI Mode')
# Issue #420
_tzInfo = None
if self._config.useLocalTimezone:
_tzInfo = PixivHelper.LocalUTCOffsetTimezone()
image = PixivModelWhiteCube.PixivImage(image_id,
response,
parent,
from_bookmark,
bookmark_count,
image_response_count,
dateFormat=self._config.dateFormat,
tzInfo=_tzInfo)
if image.imageMode == "ugoira_view":
ugoira_meta_url = "https://www.pixiv.net/ajax/illust/{0}/ugoira_meta".format(image_id)
meta_response = self.open_with_retry(ugoira_meta_url).read()
image.ParseUgoira(meta_response)
if parent is None:
if from_bookmark:
image.originalArtist.reference_image_id = image_id
self.getMemberInfoWhitecube(image.originalArtist.artistId, image.originalArtist)
else:
image.artist.reference_image_id = image_id
self.getMemberInfoWhitecube(image.artist.artistId, image.artist)
except:
PixivHelper.GetLogger().error("Respose data: \r\n %s", response)
raise
return (image, response)
def handleDebugMediumPage(self, response, imageId):
if self._config.enableDump:
if self._config.dumpMediumPage:
dump_filename = "Medium Page for Image Id {0}.html".format(imageId)
PixivHelper.dumpHtml(dump_filename, response)
PixivHelper.print_and_log('info', 'Dumping html to: {0}'.format(dump_filename))
if self._config.debugHttp:
PixivHelper.safePrint(u"reply: {0}".format(PixivHelper.toUnicode(response)))
def getMemberInfoWhitecube(self, member_id, artist, bookmark=False):
''' get artist information using Ajax and AppAPI '''
try:
info = None
if artist.reference_image_id > 0:
url = "https://www.pixiv.net/rpc/get_work.php?id={0}".format(artist.reference_image_id)
PixivHelper.GetLogger().debug("using webrpc: %s", url)
info = self._get_from_cache(url)
if info is None:
request = urllib2.Request(url)
infoStr = self.open_with_retry(request).read()
info = json.loads(infoStr)
self._put_to_cache(url, info)
else:
PixivHelper.print_and_log('info', 'Using OAuth to retrieve member info for: {0}'.format(member_id))
if self._username is None or self._username is None or len(self._username) < 0 or len(self._password) < 0:
raise PixivException("Empty Username or Password, please remove the cookie value and relogin, or add username/password to config.ini.")
if self._oauth_manager is None:
proxy = None
if _config.useProxy:
proxy = self._config.proxy
self._oauth_manager = PixivOAuth(self._username, self._password, proxies=proxy, refresh_token=self._config.refresh_token)
url = 'https://app-api.pixiv.net/v1/user/detail?user_id={0}'.format(member_id)
info = self._get_from_cache(url)
if info is None:
PixivHelper.GetLogger().debug("Getting member information: %s", member_id)
login_response = self._oauth_manager.login()
if login_response.status_code == 200:
login_response = json.loads(oauth_response.text)
self._config.refresh_token = info["response"]["refresh_token"]
self._config.writeConfig(path=self._config.configFileLocation)
response = self._oauth_manager.get_user_info(member_id)
info = json.loads(response.text)
self._put_to_cache(url, info)
PixivHelper.GetLogger().debug("reply: %s", response.text)
artist.ParseInfo(info, False, bookmark=bookmark)
# will throw HTTPError if user is suspended/not logged in.
url_ajax = 'https://www.pixiv.net/ajax/user/{0}'.format(member_id)
info_ajax = self._get_from_cache(url_ajax)
if info_ajax is None:
info_ajax_str = self.open_with_retry(url_ajax).read()
info_ajax = json.loads(info_ajax_str)
self._put_to_cache(url_ajax, info_ajax)
# 2nd pass to get the background
artist.ParseBackground(info_ajax)
return artist
except urllib2.HTTPError, error:
errorCode = error.getcode()
errorMessage = error.get_data()
PixivHelper.GetLogger().error("Error data: \r\n %s", errorMessage)
payload = demjson.decode(errorMessage)
# Issue #432
if payload.has_key("message"):
msg = payload["message"]
elif payload.has_key("error") and payload["error"] is not None:
msgs = list()
msgs.append(payload["error"]["user_message"])
msgs.append(payload["error"]["message"])
msgs.append(payload["error"]["reason"])
msg = ",".join(msgs)
if errorCode == 401:
raise PixivException(msg, errorCode=PixivException.NOT_LOGGED_IN, htmlPage=errorMessage)
elif errorCode == 403:
raise PixivException(msg, errorCode=PixivException.USER_ID_SUSPENDED, htmlPage=errorMessage)
else:
raise PixivException(msg, errorCode=PixivException.OTHER_MEMBER_ERROR, htmlPage=errorMessage)
def getMemberPage(self, member_id, page=1, bookmark=False, tags=None):
artist = None
response = None
if tags is not None:
tags = PixivHelper.encode_tags(tags)
else:
tags = ''
limit = 48
offset = (page - 1) * limit
need_to_slice = False
if bookmark:
# https://www.pixiv.net/ajax/user/1039353/illusts/bookmarks?tag=&offset=0&limit=24&rest=show
url = 'https://www.pixiv.net/ajax/user/{0}/illusts/bookmarks?tag={1}&offset={2}&limit={3}&rest=show'.format(member_id, tags, offset, limit)
else:
# https://www.pixiv.net/ajax/user/1813972/illusts/tag?tag=Fate%2FGrandOrder?offset=0&limit=24
# https://www.pixiv.net/ajax/user/1813972/manga/tag?tag=%E3%83%A1%E3%82%A4%E3%82%AD%E3%83%B3%E3%82%B0?offset=0&limit=24
# https://www.pixiv.net/ajax/user/5238/illustmanga/tag?tag=R-18&offset=0&limit=48
# https://www.pixiv.net/ajax/user/1813972/profile/all
url = None
if len(tags) > 0:
url = 'https://www.pixiv.net/ajax/user/{0}/illustmanga/tag?tag={1}&offset={2}&limit={3}'.format(member_id, tags, offset, limit)
elif self._config.r18mode:
url = 'https://www.pixiv.net/ajax/user/{0}/illustmanga/tag?tag={1}&offset={2}&limit={3}'.format(member_id, 'R-18', offset, limit)
else:
url = 'https://www.pixiv.net/ajax/user/{0}/profile/all'.format(member_id)
need_to_slice = True
PixivHelper.print_and_log('info', 'Member Url: ' + url)
if url is not None:
# cache the response
response = self._get_from_cache(url)
if response is None:
try:
response = self.open_with_retry(url).read()
except urllib2.HTTPError as ex:
if ex.code == 404:
response = ex.read()
self._put_to_cache(url, response)
PixivHelper.GetLogger().debug(response)
artist = PixivModelWhiteCube.PixivArtist(member_id, response, False, offset, limit)
artist.reference_image_id = artist.imageList[0] if len(artist.imageList) > 0 else 0
self.getMemberInfoWhitecube(member_id, artist, bookmark)
if artist.haveImages and need_to_slice:
artist.imageList = artist.imageList[offset:offset + limit]
return (artist, response)
def getSearchTagPage(self, tags,
current_page,
wild_card=True,
title_caption=False,
start_date=None,
end_date=None,
member_id=None,
oldest_first=False,
start_page=1):
response = None
result = None
url = ''
if member_id is not None:
# from member id search by tags
(artist, response) = self.getMemberPage(member_id, current_page, False, tags)
# convert to PixivTags
result = PixivModelWhiteCube.PixivTags()
result.parseMemberTags(artist, member_id, tags)
else:
# search by tags
url = PixivHelper.generateSearchTagUrl(tags, current_page,
title_caption,
wild_card,
oldest_first,
start_date,
end_date,
member_id,
self._config.r18mode)
PixivHelper.print_and_log('info', 'Looping... for ' + url)
# response = self.open(url).read()
response = self.getPixivPage(url, returnParsed=False).read()
self.handleDebugTagSearchPage(response, url)
parse_search_page = BeautifulSoup(response)
result = PixivModel.PixivTags()
if member_id is not None:
result.parseMemberTags(parse_search_page, member_id, tags)
else:
try:
result.parseTags(parse_search_page, tags)
except BaseException:
PixivHelper.dumpHtml("Dump for SearchTags " + tags + ".html", response)
raise
parse_search_page.decompose()
del parse_search_page
return (result, response)
def handleDebugTagSearchPage(self, response, url):
if self._config.enableDump:
if self._config.dumpTagSearchPage:
dump_filename = "TagSearch Page for {0}.html".format(url)
PixivHelper.dumpHtml(dump_filename, response)
PixivHelper.print_and_log('info', 'Dumping html to: {0}'.format(dump_filename))
if self._config.debugHttp:
PixivHelper.safePrint(u"reply: {0}".format(PixivHelper.toUnicode(response)))
def fanboxGetSupportedUsers(self):
''' get all supported users from the list from https://www.pixiv.net/ajax/fanbox/support'''
url = 'https://www.pixiv.net/ajax/fanbox/support'
PixivHelper.print_and_log('info', 'Getting supported artists from ' + url)
# read the json response
response = self.open_with_retry(url).read()
result = Fanbox(response)
return result
def fanboxGetPostsFromArtist(self, artist_id, next_url=""):
''' get all posts from the supported user from https://www.pixiv.net/ajax/fanbox/creator?userId=15521131 '''
if next_url is None or next_url == "":
url = "https://www.pixiv.net/ajax/fanbox/creator?userId={0}".format(artist_id)
else:
url = "https://www.pixiv.net" + next_url
PixivHelper.print_and_log('info', 'Getting posts from ' + url)
response = self.open_with_retry(url).read()
# Issue #420
_tzInfo = None
if self._config.useLocalTimezone:
_tzInfo = PixivHelper.LocalUTCOffsetTimezone()
result = FanboxArtist(artist_id, response, tzInfo=_tzInfo)
pixivArtist = PixivModelWhiteCube.PixivArtist(artist_id)
self.getMemberInfoWhitecube(artist_id, pixivArtist)
result.artistName = pixivArtist.artistName
result.artistToken = pixivArtist.artistToken
return result
def getBrowser(config=None, cookieJar=None):
global defaultCookieJar
global defaultConfig
global _browser
if _browser is None:
if config is not None:
defaultConfig = config
if cookieJar is not None:
defaultCookieJar = cookieJar
if defaultCookieJar is None:
PixivHelper.GetLogger().info("No default cookie jar available, creating... ")
defaultCookieJar = cookielib.LWPCookieJar()
_browser = PixivBrowser(defaultConfig, defaultCookieJar)
elif config is not None:
defaultConfig = config
_browser._configureBrowser(config)
return _browser
def getExistingBrowser():
global _browser
if _browser is None:
raise PixivException("Browser is not initialized yet!", errorCode=PixivException.NOT_LOGGED_IN)
return _browser
# pylint: disable=W0612
def get_br():
from PixivConfig import PixivConfig
cfg = PixivConfig()
cfg.loadConfig("./config.ini")
b = getBrowser(cfg, None)
if cfg.cookie is not None and len(cfg.cookie) > 0:
success = b.loginUsingCookie(cfg.cookie)
b._username = cfg.username
b._password = cfg.password
elif not success:
success = b.login(cfg.username, cfg.password)
return (b, success)
def test():
(b, success) = get_br()
b.get_oauth_token()
refresh_token = b._oauth_reply['response']['refresh_token']
auth_token = b._oauth_reply['response']['access_token']
print("Reply = {0}".format(b._oauth_reply))
print("Auth Token = " + auth_token)
print("Refr Token = " + refresh_token)
b.get_oauth_token(refresh_token, auth_token)
if success:
def testSearchTags():
print("test search tags")
tags = "VOCALOID"
p = 1
wild_card = True
title_caption = False
start_date = "2016-11-06"
end_date = "2016-11-07"
member_id = None
oldest_first = True
start_page = 1
(resultS, page) = b.getSearchTagPage(tags, p,
wild_card,
title_caption,
start_date,
end_date,
member_id,
oldest_first,
start_page)
resultS.PrintInfo()
assert(len(resultS.itemList) > 0)
def testImage():
print("test image mode")
print(">>")
(result, page) = b.getImagePage(60040975)
print(result.PrintInfo())
assert(len(result.imageTitle) > 0)
print(result.artist.PrintInfo())
assert(len(result.artist.artistToken) > 0)
assert(not("R-18" in result.imageTags))
print(">>")
(result2, page2) = b.getImagePage(59628358)
print(result2.PrintInfo())
assert(len(result2.imageTitle) > 0)
print(result2.artist.PrintInfo())
assert(len(result2.artist.artistToken) > 0)
assert("R-18" in result2.imageTags)
print(">> ugoira")
(result3, page3) = b.getImagePage(60070169)
print(result3.PrintInfo())
assert(len(result3.imageTitle) > 0)
print(result3.artist.PrintInfo())
print(result3.ugoira_data)
assert(len(result3.artist.artistToken) > 0)
assert(result3.imageMode == 'ugoira_view')
def testMember():
print("Test member mode")
print(">>")
(result3, page3) = b.getMemberPage(1227869, page=1, bookmark=False, tags=None)
print(result3.PrintInfo())
assert(len(result3.artistToken) > 0)
assert(len(result3.imageList) > 0)
print(">>")
(result4, page4) = b.getMemberPage(1227869, page=2, bookmark=False, tags=None)
print(result4.PrintInfo())
assert(len(result4.artistToken) > 0)
assert(len(result4.imageList) > 0)
print(">>")
(result5, page5) = b.getMemberPage(4894, page=1, bookmark=False, tags=None)
print(result5.PrintInfo())
assert(len(result5.artistToken) > 0)
assert(len(result5.imageList) > 0)
print(">>")
(result6, page6) = b.getMemberPage(4894, page=3, bookmark=False, tags=None)
print(result6.PrintInfo())
assert(len(result6.artistToken) > 0)
assert(len(result6.imageList) > 0)
def testMemberBookmark():
print("Test member bookmarks mode")
print(">>")
(result5, page5) = b.getMemberPage(1227869, page=1, bookmark=True, tags=None)
print(result5.PrintInfo())
assert(len(result5.artistToken) > 0)
assert(len(result5.imageList) > 0)
print(">>")
(result6, page6) = b.getMemberPage(1227869, page=2, bookmark=True, tags=None)
print(result6.PrintInfo())
assert(len(result6.artistToken) > 0)
assert(len(result6.imageList) > 0)
print(">>")
(result6, page6) = b.getMemberPage(1227869, page=10, bookmark=True, tags=None)
if result6 is not None:
print(result6.PrintInfo())
(result6, page6) = b.getMemberPage(1227869, page=12, bookmark=True, tags=None)
if result6 is not None:
print(result6.PrintInfo())
assert(len(result6.artistToken) > 0)
assert(len(result6.imageList) == 0)
# testSearchTags()
testImage()
# testMember()
# testMemberBookmark()
else:
print("Invalid username or password")
if __name__ == '__main__':
test()
print("done")