Skip to content

Commit

Permalink
#706 implement filename format for novel
Browse files Browse the repository at this point in the history
  • Loading branch information
Nandaka committed Feb 15, 2021
1 parent 55efc78 commit 8598ce3
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 21 deletions.
12 changes: 11 additions & 1 deletion PixivBrowserFactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,17 @@ def getNovelPage(self, novel_id) -> PixivNovel:
locale = f"&lang={self._locale}"
url = f"https://www.pixiv.net/ajax/novel/{novel_id}?{locale}"
response = self.getPixivPage(url, returnParsed=False, enable_cache=True)
novel = PixivNovel(novel_id, response)

_tzInfo = None
if self._config.useLocalTimezone:
_tzInfo = PixivHelper.LocalUTCOffsetTimezone()
novel = PixivNovel(novel_id,
response,
tzInfo=_tzInfo,
dateFormat=self._config.dateFormat)

(artist, _) = self.getMemberPage(novel.artist_id)
novel.artist = artist
return novel

def getNovelSeries(self, novel_series_id) -> NovelSeries:
Expand Down
2 changes: 2 additions & 0 deletions PixivConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ class PixivConfig():
restriction=lambda x: x is not None and len(x) > 0),
ConfigItem("Filename", "filenameFormatSketch", "%artist% (%member_id%)" + os.sep + "%urlFilename% - %title%",
restriction=lambda x: x is not None and len(x) > 0),
ConfigItem("Filename", "filenameFormatNovel", "%artist% (%member_id%)" + os.sep + "%manga_series_id% %manga_series_order% %urlFilename% - %title%",
restriction=lambda x: x is not None and len(x) > 0),
ConfigItem("Filename", "avatarNameFormat", ""),
ConfigItem("Filename", "backgroundNameFormat", ""),
ConfigItem("Filename", "tagsSeparator", ", "),
Expand Down
80 changes: 74 additions & 6 deletions PixivNovel.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import codecs
import json
from datetime import datetime
import datetime_z

import PixivHelper
from PixivException import PixivException
from PixivImage import PixivTagData

MAX_LIMIT = 10

Expand All @@ -11,11 +14,34 @@ class PixivNovel:
novel_id = 0
novel_json_str = ""
content = ""
title = ""

def __init__(self, novel_id, novel_json) -> None:
self.novel_id = novel_id
# compatibility
artist = None
artist_id = 0
imageTitle = ""
imageId = 0
worksDate = ""
worksDateDateTime = datetime.fromordinal(1)
imageTags = None
tags = None
bookmark_count = 0
image_response_count = 0

# series info
seriesNavData = None

# doesn't apply
worksResolution = ""
imageMode = "Novel"

_tzInfo = None
dateFormat = None

def __init__(self, novel_id, novel_json, tzInfo=None, dateFormat=None) -> None:
self.novel_id = self.imageId = novel_id
self.novel_json_str = novel_json
self._tzInfo = tzInfo
self.dateFormat = dateFormat
self.parse()

def parse(self):
Expand All @@ -25,8 +51,47 @@ def parse(self):
errorCode=PixivException.UNKNOWN_IMAGE_ERROR,
htmlPage=self.novel_json_str)

self.title = js["body"]["title"]
self.content = js["body"]["content"]
root = js["body"]

self.imageTitle = root["title"]
self.content = root["content"]
self.artist_id = root["userId"]
self.bookmark_count = root["bookmarkCount"]
self.image_response_count = root["imageResponseCount"]
self.seriesNavData = root["seriesNavData"]

# datetime
self.worksDateDateTime = datetime_z.parse_datetime(root["createDate"])
self.js_createDate = root["createDate"] # store for json file
if self._tzInfo is not None:
self.worksDateDateTime = self.worksDateDateTime.astimezone(self._tzInfo)

tempDateFormat = self.dateFormat or "%Y-%m-%d" # 2018-07-22, else configured in config.ini
self.worksDate = self.worksDateDateTime.strftime(tempDateFormat)

# tags
self.imageTags = list()
self.tags = list()
tags = root["tags"]
if tags is not None:
tags = root["tags"]["tags"]
for tag in tags:
self.imageTags.append(tag["tag"])
self.tags.append(PixivTagData(tag["tag"], tag))

# append original tag
if root["isOriginal"]:
self.imageTags.append("オリジナル")
tag = {"tag": "オリジナル",
"locked": True,
"deletable": False,
"userId": "",
"romaji": "original",
"translation": {
"en": "original"
}
}
self.tags.append(PixivTagData(tag["tag"], tag))

def write_content(self, filename):
ft = open("novel_template.html")
Expand All @@ -42,7 +107,7 @@ def write_content(self, filename):
PixivHelper.get_logger().exception("Error when saving novel: %s, file is saved to: %s.html", filename, str(self.novel_id))

if fh is not None:
content_str = template_str.replace("%title%", self.title)
content_str = template_str.replace("%title%", self.imageTitle)
content_str = content_str.replace("%novel_json_str%", self.novel_json_str)
fh.write(content_str)
fh.close()
Expand All @@ -54,6 +119,7 @@ class NovelSeries:
series_list = list()
series_list_str = dict()
total = 0
series_name = ""

def __init__(self, series_id, series_json) -> None:
self.series_id = series_id
Expand All @@ -67,7 +133,9 @@ def parse(self):
raise PixivException("Cannot get novel series content details",
errorCode=PixivException.UNKNOWN_IMAGE_ERROR,
htmlPage=self.series_str)
# from publishedContentCount or total or displaySeriesContentCount ????
self.total = js["body"]["total"]
self.series_name = js["body"]["title"]

def parse_series_content(self, page_info, current_page):
js = json.loads(page_info)
Expand Down
51 changes: 41 additions & 10 deletions PixivNovelHandler.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from colorama.ansi import Fore, Style
import PixivHelper
import PixivNovel

Expand All @@ -9,13 +10,38 @@ def process_novel(caller,
if notifier is None:
notifier = PixivHelper.dummy_notifier

PixivHelper.print_and_log(None, f"Processing Novel details = {novel_id}")
msg = Fore.YELLOW + Style.BRIGHT + f'Processing Novel details: {novel_id}' + Style.RESET_ALL
PixivHelper.print_and_log('info', msg)

novel = caller.__br__.getNovelPage(novel_id)
PixivHelper.print_and_log(None, f"Title = {novel.title}")
filename = f"novel-{novel_id}.html"
PixivHelper.print_and_log(None, f"Saving Novel details to = {filename}")
PixivHelper.print_and_log(None, f"Title : {novel.imageTitle}")
PixivHelper.print_and_log(None, f'Member Name : {novel.artist.artistName}')
PixivHelper.print_and_log(None, f'Member Avatar: {novel.artist.artistAvatar}')
PixivHelper.print_and_log(None, f'Member Token : {novel.artist.artistToken}')
PixivHelper.print_and_log(None, f'Member Background : {novel.artist.artistBackground}')
tags_str = ', '.join(novel.imageTags)
PixivHelper.print_and_log(None, f"Tags : {tags_str}")
PixivHelper.print_and_log(None, f"Date : {novel.worksDateDateTime}")
PixivHelper.print_and_log(None, f"Mode : {novel.imageMode}")
PixivHelper.print_and_log(None, f"Bookmark Count : {novel.bookmark_count}")

# fake the fileUrl
fileUrl = f"https://www.pixiv.net/ajax/novel/{novel_id}.html"
filename = PixivHelper.make_filename(config.filenameFormatNovel,
novel,
tagsSeparator=config.tagsSeparator,
tagsLimit=config.tagsLimit,
fileUrl=fileUrl,
bookmark=False,
searchTags="",
useTranslatedTag=config.useTranslatedTag,
tagTranslationLocale=config.tagTranslationLocale)
filename = PixivHelper.sanitize_filename(filename, config.rootDirectory)
PixivHelper.print_and_log(None, f"Filename : {filename}")
novel.write_content(filename)

print()


def process_novel_series(caller,
config,
Expand All @@ -25,9 +51,13 @@ def process_novel_series(caller,
notifier=None):
if notifier is None:
notifier = PixivHelper.dummy_notifier
PixivHelper.print_and_log(None, f"Processing Novel Series = {series_id}")
msg = Fore.YELLOW + Style.BRIGHT + f'Processing Novel Series: {series_id}' + Style.RESET_ALL
PixivHelper.print_and_log('info', msg)

novel_series = caller.__br__.getNovelSeries(series_id)
PixivHelper.print_and_log(None, f'Series Name : {novel_series.series_name}')
PixivHelper.print_and_log(None, f'Total Novel : {novel_series.total}')

page = start_page
flag = True
while(flag):
Expand All @@ -42,8 +72,9 @@ def process_novel_series(caller,
flag = False

for novel in novel_series.series_list:
print(novel["id"])
# process_novel(caller,
# config,
# novel_id=novel["id"],
# notifier=notifier)
process_novel(caller,
config,
novel_id=novel["id"],
notifier=notifier)

print()
10 changes: 6 additions & 4 deletions PixivUtil2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1392,10 +1392,12 @@ def main():
PixivHelper.print_and_log('error', pex.message)
ERROR_CODE = pex.errorCode
except Exception as ex:
# exc_type, exc_value, exc_traceback = sys.exc_info()
# traceback.print_exception(exc_type, exc_value, exc_traceback)
# __log__.exception('Unknown Error: %s', str(exc_value))
PixivHelper.print_and_log("error", f"Unknown Error: {sys.exc_info()}")
if __config__.logLevel == "DEBUG":
import traceback
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(exc_type, exc_value, exc_traceback)
__log__.exception('Unknown Error: %s', str(exc_value))
PixivHelper.print_and_log("error", f"Unknown Error, please check the log file: {sys.exc_info()}")
ERROR_CODE = getattr(ex, 'errorCode', -1)
finally:
__dbManager__.close()
Expand Down

0 comments on commit 8598ce3

Please sign in to comment.