Skip to content

Commit

Permalink
Fix #1352 #1361 (#1363)
Browse files Browse the repository at this point in the history
* Update PixivBrowserFactory.py

* Update PixivModelFanbox.py

* Update PixivBrowserFactory.py
  • Loading branch information
marierose147 authored Aug 22, 2024
1 parent 92e7275 commit e23e29c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
23 changes: 20 additions & 3 deletions PixivBrowserFactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def loginUsingCookie(self, login_cookie=None):
PixivHelper.print_and_log('info', 'Trying to log in with saved cookie')
self.clearCookie()
self._loadCookie(login_cookie, "pixiv.net")
res = self.open_with_retry('https://www.pixiv.net/en') # + self._locale)
res = self.open_with_retry('https://www.pixiv.net') # + self._locale)
parsed = BeautifulSoup(res, features="html5lib")
parsed_str = str(parsed.decode('utf-8'))
PixivHelper.print_and_log("info", f'Logging in, return url: {res.geturl()}')
Expand All @@ -369,7 +369,7 @@ def loginUsingCookie(self, login_cookie=None):
PixivHelper.print_and_log('info', 'Login successful.')
PixivHelper.get_logger().info('Logged in using cookie')
self.getMyId(parsed_str)
temp_locale = str(res.geturl()).replace('https://www.pixiv.net/', '').replace('/', '')
temp_locale = str(res.geturl()).replace('https://www.pixiv.net', '').replace('/', '')
if len(temp_locale) > 0:
self._locale = '/' + temp_locale
PixivHelper.get_logger().info('Locale = %s', self._locale)
Expand Down Expand Up @@ -1023,10 +1023,27 @@ def fanboxGetPostsFromArtist(self, artist: FanboxArtist = None, next_url="") ->

# Issue #641
if next_url is None or next_url == "":
url = f"https://api.fanbox.cc/post.paginateCreator?creatorId={artist.creatorId}"
PixivHelper.print_and_log('info', 'Getting Pages from ' + url)
referer = "https://www.fanbox.cc/"
req = mechanize.Request(url)
req.add_header('Accept', 'application/json, text/plain, */*')
req.add_header('Referer', referer)
req.add_header('Origin', 'https://www.fanbox.cc')

res = self.open_with_retry(req)
response = res.read()
PixivHelper.get_logger().debug(response.decode('utf8'))
res.close()

artist.setPages(response)

# url = f"https://api.fanbox.cc/post.listCreator?userId={artist.artistId}&limit=10"
# Issue #1094
# https://api.fanbox.cc/post.listCreator?creatorId=onartworks&maxPublishedDatetime=2022-02-26%2015%3A57%3A17&maxId=3468213&limit=10
url = f"https://api.fanbox.cc/post.listCreator?creatorId={artist.creatorId}&limit=10"
#url = f"https://api.fanbox.cc/post.listCreator?creatorId={artist.creatorId}&limit=10"

url = artist.Pages[artist.PageIndex]
elif next_url.startswith("https://"):
url = next_url
else:
Expand Down
26 changes: 23 additions & 3 deletions PixivModelFanbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,9 @@ class FanboxArtist(object):
FOLLOWING = 1
CUSTOM = 2

Pages = None
PageIndex = 0

@classmethod
def parseArtistIds(cls, page):
ids = list()
Expand Down Expand Up @@ -550,6 +553,16 @@ def __init__(self, artist_id, artist_name, creator_id, tzInfo=None):
def __str__(self):
return f"FanboxArtist({self.artistId}, {self.creatorId}, {self.artistName})"

def setPages(self, page):
js = demjson3.decode(page)

if "error" in js and js["error"]:
raise PixivException(f"Error when requesting Fanbox artist pages: {self.artistId}", 9999, page)

if js["body"] is not None:
js_body = js["body"]
self.Pages = js_body

def parsePosts(self, page) -> List[FanboxPost]:
js = demjson3.decode(page)

Expand All @@ -558,7 +571,7 @@ def parsePosts(self, page) -> List[FanboxPost]:

if js["body"] is not None:
js_body = js["body"]

posts = list()

if "creator" in js_body:
Expand All @@ -571,15 +584,22 @@ def parsePosts(self, page) -> List[FanboxPost]:
# https://www.pixiv.net/ajax/fanbox/post?postId={0}
# or old api
post_root = js_body


for jsPost in post_root["items"]:
#for jsPost in post_root["items"]:
for jsPost in post_root:
post_id = int(jsPost["id"])
post = FanboxPost(post_id, self, jsPost, tzInfo=self._tzInfo)
posts.append(post)
# sanity check
assert (self.artistId == int(jsPost["user"]["userId"])), "Different user id from constructor!"

self.nextUrl = post_root["nextUrl"]
#self.nextUrl = post_root["nextUrl"]
self.PageIndex += 1
if self.PageIndex < len(self.Pages):
self.nextUrl = self.Pages[self.PageIndex]
else:
self.nextUrl = None
if self.nextUrl is not None and len(self.nextUrl) > 0:
self.hasNextPage = True

Expand Down

1 comment on commit e23e29c

@AvocadoFlour
Copy link

@AvocadoFlour AvocadoFlour commented on e23e29c Aug 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pulled this for the purpose of downloading an artist's fanbox;
The tool does not succesfully run in this state. I don't know what the problem is, I'll just paste what the window outputs:

┌──────────────────────────────────────────────────────────┐
│ PixivDownloader2 version 20240703 │
https://github.com/Nandaka/PixivUtil2/releases
│ Donate at https://bit.ly/PixivUtilDonation
└──────────────────────────────────────────────────────────┘
Reading I:\pixiutilRepo\PixivUtil2\config.ini ...
Configuration loaded.
Using default DB Path: I:\pixiutilRepo\PixivUtil2\db.sqlite
Creating database... done.
Importing list... Found 6941 items done.
Updated 6941 items.
Only process members where the last update is >= 14 days ago
Username login is broken, use Cookies to log in.
See Q3. at https://github.com/Nandaka/PixivUtil2?tab=readme-ov-file#a-usage
Trying to log in with saved cookie
Error at doLogin(): (<class 'mechanize._response.get_seek_wrapper_class..httperror_seek_wrapper'>, <httperror_seek_wrapper (urllib.error.HTTPError instance) at 0x1edd575af30 whose wrapped object = <HTTPError 403: 'Forbidden'>>, <traceback object at 0x000001EDD57634C0>)
Traceback (most recent call last):
File "I:\pixiutilRepo\PixivUtil2\PixivUtil2.py", line 1527, in doLogin
result = br.loginUsingCookie()
^^^^^^^^^^^^^^^^^^^^^^^^^
File "I:\pixiutilRepo\PixivUtil2\PixivBrowserFactory.py", line 351, in loginUsingCookie
res = self.open_with_retry('https://www.pixiv.net') # + self._locale)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "I:\pixiutilRepo\PixivUtil2\PixivBrowserFactory.py", line 207, in open_with_retry
res = self.open(url, data, timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\username\AppData\Local\Programs\Python\Python312\Lib\site-packages\mechanize_mechanize.py", line 257, in open
return self._mech_open(url_or_request, data, timeout=timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\username\AppData\Local\Programs\Python\Python312\Lib\site-packages\mechanize_mechanize.py", line 313, in _mech_open
raise response
mechanize._response.get_seek_wrapper_class..httperror_seek_wrapper: HTTP Error 403: Forbidden

Cannot Login!
press enter to exit.

Please sign in to comment.