Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

partial implementation of #949 and #930 enhancement adjustments #951

Merged
merged 3 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions PixivFanboxHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ def process_fanbox_post(caller, config, post, artist):
html_template = reader.read()
reader.close()
post.WriteHtml(html_template, config.useAbsolutePathsInHtml, filename + ".html")

if config.writeUrlInDescription:
PixivHelper.write_url_in_description(post, config.urlBlacklistRegex, config.urlDumpFilename)
finally:
if len(post_files) > 0:
db.insertPostImages(post_files)
Expand Down
44 changes: 28 additions & 16 deletions PixivModelFanbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from PixivException import PixivException

_re_fanbox_cover = re.compile(r"c\/.*\/fanbox")

_url_pattern = re.compile("(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]")

class FanboxArtist(object):
artistId = 0
Expand Down Expand Up @@ -94,7 +94,6 @@ class FanboxPost(object):
imageId = 0
imageTitle = ""
coverImageUrl = ""
fullCoverSizeUrl = ""
worksDate = ""
worksDateDateTime = None
updatedDate = ""
Expand Down Expand Up @@ -125,14 +124,18 @@ class FanboxPost(object):

embeddedFiles = None
provider = None
# 949
descriptionUrlList = None


def __init__(self, post_id, parent, page, tzInfo=None):
self.images = list()
self.embeddedFiles = list()
self.imageId = int(post_id)
self.parent = parent
self._tzInfo = tzInfo

# 949
self.descriptionUrlList = list()
self.linkToFile = dict()

self.parsePost(page)
Expand All @@ -159,14 +162,11 @@ def __str__(self):
def parsePost(self, jsPost):
self.imageTitle = jsPost["title"]

self.coverImageUrl = jsPost["coverImageUrl"]
coverUrl = jsPost["coverImageUrl"]
# Issue #930
if self.coverImageUrl is not None:
original_url = jsPost["coverImageUrl"]
self.fullCoverSizeUrl = _re_fanbox_cover.sub("fanbox", original_url)
self.coverImageUrl = self.fullCoverSizeUrl
# self.try_add(self.fullCoverSizeUrl, self.images)
# self.try_add(self.fullCoverSizeUrl, self.embeddedFiles)
if not self.coverImageUrl and coverUrl:
Nandaka marked this conversation as resolved.
Show resolved Hide resolved
self.coverImageUrl = _re_fanbox_cover.sub("fanbox", coverUrl)
self.try_add(coverUrl, self.embeddedFiles)

self.worksDate = jsPost["publishedDatetime"]
self.worksDateDateTime = datetime_z.parse_datetime(self.worksDate)
Expand Down Expand Up @@ -205,6 +205,9 @@ def parseBody(self, jsPost):
if link["href"].find("//fanbox.pixiv.net/images/entry/") > 0 or link["href"].find("//downloads.fanbox.cc/") > 0:
self.try_add(link["href"], self.embeddedFiles)
self.try_add(link["href"], self.images)
# 949
else:
self.try_add(link["href"], self.descriptionUrlList)
images = parsed.findAll('img')
for image in images:
if "data-src-original" in image.attrs:
Expand Down Expand Up @@ -240,6 +243,12 @@ def parseBody(self, jsPost):
links = sorted(block.get("links", []), key=lambda x: x["offset"])
styles = sorted(block.get("styles", []), key=lambda x: x["offset"])

# 949
for link in links:
self.try_add(link["url"], self.descriptionUrlList)
for match in _url_pattern.finditer(block_text_raw):
self.try_add(match.group(), self.descriptionUrlList)

for i in range(0, len(block_text_raw)):
for link in links:
link_offset = link["offset"]
Expand Down Expand Up @@ -321,6 +330,13 @@ def parseBody(self, jsPost):
if embedId in jsPost["body"]["embedMap"]:
embedStr = self.getEmbedData(jsPost["body"]["embedMap"][embedId], jsPost)
self.body_text += f"<p>{embedStr}</p>"
# 949
# links = re.finditer("<a.*?href=(?P<mark>'|\")(.+?)(?P=mark)", embedStr)
# for link in links:
# self.try_add(link.group(2), self.descriptionUrlList)
links = _url_pattern.findall(embedStr)
for link in links:
self.try_add(link, self.descriptionUrlList)
else:
PixivHelper.print_and_log("warn", f"Found missing embedId: {embedId} for {self.imageId}")

Expand Down Expand Up @@ -434,11 +450,7 @@ def WriteHtml(self, html_template, useAbsolutePaths, filename):

cover_image = ""
if self.coverImageUrl:
# Issue #930
if self.fullCoverSizeUrl:
cover_image = f'<div class="cover"><a href="{self.fullCoverSizeUrl}" target="_blank"><img style="max-width: 1200px; height:auto;" src="{self.coverImageUrl}"/></a></div>'
else:
cover_image = f'<div class="cover"><img src="{self.coverImageUrl}"/></div>'
cover_image = f'<div class="cover"><img src="{self.coverImageUrl}"/></div>'
Nandaka marked this conversation as resolved.
Show resolved Hide resolved
page = html_template.replace("%coverImage%", cover_image)
page = page.replace("%coverImageUrl%", self.coverImageUrl or "")
page = page.replace("%artistName%", self.parent.artistName)
Expand All @@ -453,7 +465,7 @@ def WriteHtml(self, html_template, useAbsolutePaths, filename):
else:
token_images = '<div class="non-article images">{0}</div>'.format(
"".join(['<a href="{0}">{1}</a>'.format(x,
f'<img scr="{0}"/>' if x[x.rindex(".") + 1:].lower() in ["jpg", "jpeg", "png", "bmp"] else x)for x in self.images]))
f'<img scr="{0}"/>' if x[x.rindex(".") + 1:].lower() in ["jpg", "jpeg", "png", "bmp", "gif"] else x) for x in self.images]))
token_text = '<div class="non-article caption">{0}</div>'.format(
"".join(['<p>{0}</p>'.format(x.rstrip()) for x in self.body_text.split("\n")]))

Expand Down
12 changes: 6 additions & 6 deletions test_PixivModel_fanbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ def testFanboxArtistPosts(self):
# post-132919 text
self.assertEqual(result[2].imageId, 132919)
self.assertTrue(len(result[2].imageTitle) > 0)
self.assertIsNone(result[2].coverImageUrl)
self.assertIsNotNone(result[2].coverImageUrl)
self.assertEqual(result[2].type, "text")
self.assertEqual(len(result[2].images), 0)

# post-79695 image
self.assertEqual(result[3].imageId, 79695)
self.assertTrue(len(result[3].imageTitle) > 0)
self.assertIsNone(result[3].coverImageUrl)
self.assertIsNotNone(result[3].coverImageUrl)
self.assertEqual(result[3].type, "image")
self.assertEqual(len(result[3].images), 4)

Expand All @@ -88,7 +88,7 @@ def testFanboxArtistArticle(self):
# post-201946 article
self.assertEqual(result[0].imageId, 201946)
self.assertTrue(len(result[0].imageTitle) > 0)
self.assertIsNone(result[0].coverImageUrl)
self.assertIsNotNone(result[0].coverImageUrl)
self.assertEqual(result[0].type, "article")
self.assertEqual(len(result[0].images), 5)
self.assertEqual(len(result[0].body_text), 1312)
Expand All @@ -111,7 +111,7 @@ def testFanboxArtistArticleFileMap(self):
# post-201946 article
self.assertEqual(result[0].imageId, 210980)
self.assertTrue(len(result[0].imageTitle) > 0)
self.assertIsNone(result[0].coverImageUrl)
self.assertIsNotNone(result[0].coverImageUrl)
self.assertEqual(result[0].type, "article")
self.assertEqual(len(result[0].images), 15)
self.assertEqual(len(result[0].body_text), 3038)
Expand Down Expand Up @@ -261,7 +261,7 @@ def testFanboxNewApi2_MultiImages(self):

self.assertEqual(result.imageId, 855025)
self.assertEqual(len(result.images), 2)
self.assertEqual(len(result.embeddedFiles), 2)
self.assertEqual(len(result.embeddedFiles), 3)
self.assertIsNotNone(result.coverImageUrl)
self.assertFalse(result.coverImageUrl in result.images)

Expand All @@ -275,7 +275,7 @@ def testFanboxNewApi2_Files(self):

self.assertEqual(result.imageId, 685832)
self.assertEqual(len(result.images), 1)
self.assertEqual(len(result.embeddedFiles), 1)
self.assertEqual(len(result.embeddedFiles), 2)
self.assertIsNotNone(result.coverImageUrl)
self.assertFalse(result.coverImageUrl in result.images)

Expand Down