Skip to content

Commit

Permalink
Try fix #1084
Browse files Browse the repository at this point in the history
  • Loading branch information
Nandaka committed Feb 7, 2022
1 parent dbe3e6b commit 72bc214
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 13 deletions.
4 changes: 4 additions & 0 deletions PixivConstant.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@
HTML_TEMPLATE = '<!DOCTYPE html> <html lang="ja"> <head> <title>%artistName% - %imageTitle%</title> <meta charset="utf-8"> <style type="text/css"> *{margin:0px; padding:0px; max-width:100%; overflow:auto;} body{text-align:center; background-color:#f3f5f8;} h1, h2, h5, p{text-align:left;} h1, h2, h5, p{padding-left:4%; padding-right:4%;} p{word-break:break-word; padding-top:0.5em; padding-bottom:0.5em;} span{padding:0px;} .title{margin-top:2rem; margin-bottom:2rem;} a{margin:auto;} .root{max-width:1280px; margin:auto; background-color:#ffffff;} .caption{display:grid;} .non-article.main, .non-article.images{position:fixed; overflow-y:scroll; background-color:#ffffff;} .non-article.main{top:0px; left:0px; height:100%; width:360px;} .non-article.images{top:0px; left:360px; height:100%;} @media screen and (max-aspect-ratio:4/5){.non-article.main{height:25%; width:100%;} .non-article.images{top:25%; left:0px; height:75%; width:100%;}} </style> </head> <body> <div class="root"> <div class="main"> %coverImage% <div class="title"> <h1>%imageTitle%</h1> <h5>%worksDate%</h5> </div> %body_text(article)% %text(non-article)% </div> %images(non-article)% </div> </body> </html>'

BUFFER_SIZE = 8192

DOWNLOAD_PIXIV = "Pixiv"
DOWNLOAD_FANBOX = "Fanbox"
DOWNLOAD_SKETCH = "Sketch"
26 changes: 26 additions & 0 deletions PixivDBManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,19 @@ def updatePostUpdateDate(self, post_id, updated_date):
finally:
c.close()

def selectFanboxImageByImageIdAndPage(self, post_id, page):
try:
c = self.conn.cursor()
c.execute(
'''SELECT * FROM fanbox_post_image WHERE post_id = ? AND page = ? ''', (post_id, page))
return c.fetchone()
except BaseException:
print('Error at selectFanboxImageByImageIdAndPage():', str(sys.exc_info()))
print('failed')
raise
finally:
c.close()

def deleteFanboxPost(self, post_id, by):
post_id = int(post_id)
if by not in ["member_id", "post_id"]:
Expand Down Expand Up @@ -1069,6 +1082,19 @@ def insertSketchPostImages(self, post_id, page, save_name, created_date, last_up
finally:
c.close()

def selectSketchImageByImageIdAndPage(self, post_id, page):
try:
c = self.conn.cursor()
c.execute(
'''SELECT * FROM sketch_post_image WHERE post_id = ? AND page = ? ''', (post_id, page))
return c.fetchone()
except BaseException:
print('Error at selectSketchImageByImageIdAndPage():', str(sys.exc_info()))
print('failed')
raise
finally:
c.close()

def selectSketchPostByPostId(self, post_id):
try:
c = self.conn.cursor()
Expand Down
40 changes: 30 additions & 10 deletions PixivDownloadHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@
import codecs
import gc
import os
import shlex
import subprocess
import sys
import time
import traceback
import urllib
import shlex

import mechanize

import PixivBrowserFactory
import PixivConfig
import PixivConstant
from PixivException import PixivException
import PixivHelper
import PixivConfig
from PixivDBManager import PixivDBManager
from PixivException import PixivException


def download_image(caller,
Expand All @@ -27,11 +28,12 @@ def download_image(caller,
backup_old_file=False,
image=None,
page=None,
notifier=None):
notifier=None,
download_from=PixivConstant.DOWNLOAD_PIXIV):
'''return download result and filename if ok'''
# caller function/method
# TODO: ideally to be removed or passed as argument
db = caller.__dbManager__
db: PixivDBManager = caller.__dbManager__
config: PixivConfig = caller.__config__

if notifier is None:
Expand Down Expand Up @@ -122,15 +124,33 @@ def download_image(caller,

# check based on filename stored in DB using image id
if image is not None:
row = None
db_filename = None
if page is not None:
row = db.selectImageByImageIdAndPage(image.imageId, page)
# Issue #1084
if download_from == PixivConstant.DOWNLOAD_PIXIV:
if page is not None:
row = db.selectImageByImageIdAndPage(image.imageId, page)
if row is not None:
db_filename = row[2]
else:
row = db.selectImageByImageId(image.imageId)
if row is not None:
db_filename = row[3]
elif download_from == PixivConstant.DOWNLOAD_FANBOX:
if page is not None:
row = db.selectFanboxImageByImageIdAndPage(image.imageId, page)
else:
row = db.selectFanboxImageByImageIdAndPage(image.imageId, -1) # Cover image
if row is not None:
db_filename = row[2]
else:
row = db.selectImageByImageId(image.imageId)
elif download_from == PixivConstant.DOWNLOAD_SKETCH:
if page is not None:
row = db.selectSketchImageByImageIdAndPage(image.imageId, page)
else:
row = db.selectSketchImageByImageIdAndPage(image.imageId, 0)
if row is not None:
db_filename = row[3]
db_filename = row[2]

if db_filename is not None and os.path.isfile(db_filename):
old_size = os.path.getsize(db_filename)
# if file_size < 0:
Expand Down
6 changes: 4 additions & 2 deletions PixivFanboxHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ def process_fanbox_post(caller, config, post: PixivModelFanbox.FanboxPost, artis
config.overwrite,
config.retry,
config.backupOldFile,
image=post)
image=post,
download_from=PixivConstant.DOWNLOAD_FANBOX)
post_files.append((post.imageId, -1, filename))
PixivHelper.get_logger().debug("Download %s result: %s", filename, result)
else:
Expand Down Expand Up @@ -192,7 +193,8 @@ def process_fanbox_post(caller, config, post: PixivModelFanbox.FanboxPost, artis
False, # config.overwrite somehow unable to get remote filesize
config.retry,
config.backupOldFile,
image=post)
image=post,
download_from=PixivConstant.DOWNLOAD_FANBOX)
if result == PixivConstant.PIXIVUTIL_ABORTED:
raise KeyboardInterrupt()
post_files.append((post.imageId, current_page, filename))
Expand Down
3 changes: 2 additions & 1 deletion PixivSketchHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ def download_post(caller, config, post):
config.overwrite,
config.retry,
config.backupOldFile,
image=post)
image=post,
download_from=PixivConstant.DOWNLOAD_SKETCH)
if result == PixivConstant.PIXIVUTIL_OK:
db.insertSketchPost(post)
db.insertSketchPostImages(post.imageId,
Expand Down

0 comments on commit 72bc214

Please sign in to comment.