forked from Nandaka/PixivUtil2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PixivHelper.py
1485 lines (1240 loc) · 55.7 KB
/
PixivHelper.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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# pylint: disable=W0603
import codecs
import html
import json
import logging
import logging.handlers
import os
import platform
import random
import re
import shlex
import shutil
import subprocess
import sys
import tempfile
import time
import traceback
import unicodedata
import urllib
import urllib.parse
import webbrowser
import zipfile
from datetime import date, datetime, timedelta, tzinfo
from hashlib import md5, sha1, sha256
from mmap import ACCESS_READ, mmap
from pathlib import Path
from typing import Union
import mechanize
from colorama import Fore, Style
from PIL import Image, ImageFile
import PixivArtist
import PixivConstant
from PixivException import PixivException
from PixivImage import PixivImage
from PixivModelFanbox import FanboxArtist, FanboxPost
__logger = None
_config = None
__re_manga_index = re.compile(r'_p(\d+)')
__badchars__ = None
if platform.system() == 'Windows':
__badchars__ = re.compile(r'''
^$
|\?
|:
|<
|>
|\|
|\*
|\"
''', re.VERBOSE)
else:
__badchars__ = re.compile(r'''
^$
''', re.VERBOSE)
__custom_sanitizer_dic__ = {}
__ansi_color = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
def set_config(config):
global _config
_config = config
def get_logger(level=None, reload=False):
'''Set up logging'''
global __logger
if reload:
__logger = None
if __logger is None:
script_path = module_path()
__logger = logging.getLogger('PixivUtil' + PixivConstant.PIXIVUTIL_VERSION)
if _config is None or _config.disableLog:
logging.disable()
if _config is not None and _config.disableLog:
print(f"{Fore.RED}Log Disabled!{Style.RESET_ALL}")
else:
logging.disable(logging.NOTSET)
if level is None:
level = logging.DEBUG
if _config is not None:
level = _config.logLevel
__logger.setLevel(level)
__logHandler__ = logging.handlers.RotatingFileHandler(script_path + os.sep + PixivConstant.PIXIVUTIL_LOG_FILE,
maxBytes=PixivConstant.PIXIVUTIL_LOG_SIZE,
backupCount=PixivConstant.PIXIVUTIL_LOG_COUNT,
encoding="utf-8")
__formatter__ = logging.Formatter(PixivConstant.PIXIVUTIL_LOG_FORMAT)
__logHandler__.setFormatter(__formatter__)
__logger.addHandler(__logHandler__)
return __logger
def set_log_level(level):
get_logger(logging.INFO).info("Setting log level to: %s", level)
get_logger(level).setLevel(level)
def sanitize_filename(name, rootDir=None):
'''Replace reserved character/name with underscore (windows), rootDir is not sanitized.'''
# get the absolute rootdir
if rootDir is not None:
rootDir = os.path.abspath(rootDir)
# Unescape '&', '<', and '>'
name = html.unescape(name)
name = __badchars__.sub("_", name)
for key, value in __custom_sanitizer_dic__.items():
name = value["regex"].sub(value["replace"], name)
# Remove unicode control characters
name = "".join(c for c in name if unicodedata.category(c) != "Cc")
# Strip leading/trailing space for each directory
# Issue #627: remove trailing '.'
# Ensure Windows reserved filenames are prefixed with _
stripped_name = list()
for item in name.split(os.sep):
if Path(item).is_reserved():
item = '_' + item
stripped_name.append(item.strip(" .\t\r\n"))
name = os.sep.join(stripped_name)
if platform.system() == 'Windows':
# cut whole path to 255 char
# TODO: check for Windows long path extensions being enabled
if rootDir is not None:
# need to remove \\ from name prefix
tname = name[1:] if name[0] == os.sep else name
full_name = os.path.abspath(os.path.join(rootDir, tname))
else:
full_name = os.path.abspath(name)
if len(full_name) > 255:
filename, extname = os.path.splitext(name) # NOT full_name, to avoid clobbering paths
# don't trim the extension
name = filename[:255 - len(extname)] + extname
if name == extname: # we have no file name left
raise OSError(None, "Path name too long", full_name, 0x000000A1) # 0xA1 is "invalid path"
else:
# Unix: cut filename to <= 249 bytes
# TODO: allow macOS higher limits, HFS+ allows 255 UTF-16 chars, and APFS 255 UTF-8 chars
while len(name.encode('utf-8')) > 249:
filename, extname = os.path.splitext(name)
name = filename[:len(filename) - 1] + extname
name = name.replace('\\', '/')
if rootDir is not None:
name = name[1:] if name[0] == os.sep else name
name = os.path.abspath(os.path.join(rootDir, name))
get_logger().debug("Sanitized Filename: %s", name)
return name
# Issue #277: always replace '/' and '\' with '_' for %artist%, %title%, %searchTags%, %tags%, and %original_artist%.
def replace_path_separator(s, replacement='_'):
return s.replace('/', replacement).replace('\\', replacement)
def make_filename(nameFormat: str,
imageInfo: Union[PixivImage, FanboxPost],
artistInfo: Union[PixivArtist.PixivArtist, FanboxArtist] = None,
tagsSeparator=' ',
tagsLimit=-1,
fileUrl='',
appendExtension=True,
bookmark=False,
searchTags='',
useTranslatedTag=False,
tagTranslationLocale="en") -> str:
'''Build the filename from given info to the given format.'''
global _config
if artistInfo is None:
artistInfo = imageInfo.artist
# Get the image extension
fileUrl = os.path.basename(fileUrl)
imageExtension = ""
imageFile = fileUrl
if fileUrl.find(".") > 0:
splittedUrl = fileUrl.split('.')
imageFile = splittedUrl[0]
imageExtension = splittedUrl[1]
imageExtension = imageExtension.split('?')[0]
# Issue #940
if nameFormat.find('%force_extension') > -1:
to_replace_ext = re.findall("(%force_extension{.*}%)", nameFormat)
forced_ext = re.findall("{(.*)}", to_replace_ext[0])
nameFormat = nameFormat.replace(to_replace_ext[0], "")
imageExtension = forced_ext[0]
# artist related
nameFormat = nameFormat.replace('%artist%', replace_path_separator(artistInfo.artistName))
nameFormat = nameFormat.replace('%member_id%', str(artistInfo.artistId))
nameFormat = nameFormat.replace('%member_token%', artistInfo.artistToken)
# sketch related
if hasattr(artistInfo, "sketchArtistId"):
nameFormat = nameFormat.replace('%sketch_member_id%', str(artistInfo.sketchArtistId))
# Issue #1117
if hasattr(artistInfo, "fanbox_name"):
nameFormat = nameFormat.replace('%fanbox_name%', str(artistInfo.fanbox_name))
# image related
nameFormat = nameFormat.replace('%title%', replace_path_separator(imageInfo.imageTitle))
nameFormat = nameFormat.replace('%image_id%', str(imageInfo.imageId))
nameFormat = nameFormat.replace('%works_date%', imageInfo.worksDate)
nameFormat = nameFormat.replace('%works_date_only%', imageInfo.worksDate.split(' ')[0])
# Issue #1064
if hasattr(imageInfo, "translated_work_title") and len(imageInfo.translated_work_title) > 0:
nameFormat = nameFormat.replace('%translated_title%', replace_path_separator(imageInfo.translated_work_title))
else:
nameFormat = nameFormat.replace('%translated_title%', replace_path_separator(imageInfo.imageTitle))
# formatted works date/time, ex. %works_date_fmt{%Y-%m-%d}%
if nameFormat.find("%works_date_fmt") > -1:
to_replace = re.findall("(%works_date_fmt{.*}%)", nameFormat)
date_format = re.findall("{(.*)}", to_replace[0])
nameFormat = nameFormat.replace(to_replace[0], imageInfo.worksDateDateTime.strftime(date_format[0]))
nameFormat = nameFormat.replace('%works_res%', imageInfo.worksResolution)
nameFormat = nameFormat.replace('%urlFilename%', imageFile)
nameFormat = nameFormat.replace('%searchTags%', replace_path_separator(searchTags))
# date
nameFormat = nameFormat.replace('%date%', date.today().strftime('%Y%m%d'))
# formatted date/time, ex. %date_fmt{%Y-%m-%d}%
if nameFormat.find("%date_fmt") > -1:
to_replace2 = re.findall("(%date_fmt{.*}%)", nameFormat)
date_format2 = re.findall("{(.*)}", to_replace2[0])
nameFormat = nameFormat.replace(to_replace2[0], datetime.today().strftime(date_format2[0]))
# get the page index & big mode if manga
page_index = ''
page_number = ''
page_big = ''
if imageInfo.imageMode == 'manga':
# not working for fanbox due to url filename doesn't have _p0
idx = __re_manga_index.findall(fileUrl)
if len(idx) > 0:
page_index = idx[0]
page_number = str(int(page_index) + 1)
padding = len(str(imageInfo.imageCount)) or 1
page_number = str(page_number)
page_number = page_number.zfill(padding)
if fileUrl.find('_big') > -1 or fileUrl.find('_m') <= -1:
page_big = 'big'
nameFormat = nameFormat.replace('%page_big%', page_big)
nameFormat = nameFormat.replace('%page_index%', page_index)
nameFormat = nameFormat.replace('%page_number%', page_number)
# Manga Series related
if hasattr(imageInfo, "seriesNavData") and imageInfo.seriesNavData:
nameFormat = nameFormat.replace('%manga_series_order%', str(imageInfo.seriesNavData['order']))
nameFormat = nameFormat.replace('%manga_series_id%', str(imageInfo.seriesNavData['seriesId']))
nameFormat = nameFormat.replace('%manga_series_title%', imageInfo.seriesNavData['title'])
else:
nameFormat = nameFormat.replace('%manga_series_order%', '')
nameFormat = nameFormat.replace('%manga_series_id%', '')
nameFormat = nameFormat.replace('%manga_series_title%', '')
if tagsSeparator == '%space%':
tagsSeparator = ' '
if tagsSeparator == '%ideo_space%':
tagsSeparator = u'\u3000'
image_tags = imageInfo.imageTags
if tagsLimit != -1:
tagsLimit = tagsLimit if tagsLimit < len(imageInfo.imageTags) else len(imageInfo.imageTags)
image_tags = image_tags[0:tagsLimit]
# 701
if useTranslatedTag:
for idx, tag in enumerate(image_tags):
for translated_tags in imageInfo.tags: # type: PixivImage.PixivTagData
if translated_tags.tag == tag:
image_tags[idx] = translated_tags.get_translation(tagTranslationLocale)
break
tags = tagsSeparator.join(image_tags)
# Issue #1226
if hasattr(imageInfo, "ai_type") and imageInfo.ai_type == 2:
nameFormat = nameFormat.replace('%AI%', 'AI')
else:
nameFormat = nameFormat.replace('%AI%', '')
r18Dir = ""
if "R-18G" in imageInfo.imageTags:
r18Dir = "R-18G"
elif "R-18" in imageInfo.imageTags:
r18Dir = "R-18"
nameFormat = nameFormat.replace('%R-18%', r18Dir)
nameFormat = nameFormat.replace('%tags%', replace_path_separator(tags))
nameFormat = nameFormat.replace(''', '\'') # Yavos: added html-code for "'" - works only when ' is excluded from __badchars__
if bookmark: # from member bookmarks
nameFormat = nameFormat.replace('%bookmark%', 'Bookmarks')
nameFormat = nameFormat.replace('%original_member_id%', str(imageInfo.originalArtist.artistId))
nameFormat = nameFormat.replace('%original_member_token%', imageInfo.originalArtist.artistToken)
nameFormat = nameFormat.replace('%original_artist%', replace_path_separator(imageInfo.originalArtist.artistName))
else:
nameFormat = nameFormat.replace('%bookmark%', '')
nameFormat = nameFormat.replace('%original_member_id%', str(artistInfo.artistId))
nameFormat = nameFormat.replace('%original_member_token%', artistInfo.artistToken)
nameFormat = nameFormat.replace('%original_artist%', replace_path_separator(artistInfo.artistName))
if imageInfo.bookmark_count > 0:
nameFormat = nameFormat.replace('%bookmark_count%', str(imageInfo.bookmark_count))
if '%bookmarks_group%' in nameFormat:
nameFormat = nameFormat.replace('%bookmarks_group%', calculate_group(imageInfo.bookmark_count))
else:
nameFormat = nameFormat.replace('%bookmark_count%', '')
nameFormat = nameFormat.replace('%bookmarks_group%', '')
if imageInfo.image_response_count > 0:
nameFormat = nameFormat.replace('%image_response_count%', str(imageInfo.image_response_count))
else:
nameFormat = nameFormat.replace('%image_response_count%', '')
# clean up double space
while nameFormat.find(' ') > -1:
nameFormat = nameFormat.replace(' ', ' ')
# clean up double slash
while nameFormat.find('//') > -1 or nameFormat.find('\\\\') > -1:
nameFormat = nameFormat.replace('//', '/').replace('\\\\', '\\')
if appendExtension:
nameFormat = nameFormat.strip() + '.' + imageExtension
if _config and len(_config.customCleanUpRe) > 0:
nameFormat = re.sub(_config.customCleanUpRe, '', nameFormat)
return nameFormat.strip()
# Issue #956
def get_hash(path: str, method="md5") -> str:
hash_str = ""
hash_method = None
if method == "md5":
hash_method = md5
elif method == "sha1":
hash_method = sha1
elif method == "sha256":
hash_method = sha256
else:
raise PixivException(msg=f"Invalid hash function {method}")
with open(path) as file, mmap(file.fileno(), 0, access=ACCESS_READ) as file:
hash_str = hash_method(file).hexdigest()
return hash_str
def calculate_group(count):
# follow rules from https://dic.pixiv.net/a/users%E5%85%A5%E3%82%8A
if count >= 100 and count < 250:
return '100'
elif count >= 250 and count < 500:
return '250'
elif count >= 500 and count < 1000:
return '500'
elif count >= 1000 and count < 5000:
return '1000'
elif count >= 5000 and count < 10000:
return '5000'
elif count >= 10000:
return '10000'
else:
return ''
def safePrint(msg, newline=True, end=None):
"""Print empty string if UnicodeError raised."""
if not isinstance(msg, str):
print(f"{msg}", end=' ')
for msgToken in msg.split(' '):
try:
print(msgToken, end=' ')
except UnicodeError:
print(('?' * len(msgToken)), end=' ')
if end is not None:
print("", end=end)
elif newline:
print("")
def set_console_title(title):
try:
if platform.system() == "Windows":
subprocess.call('title' + ' ' + title, shell=True)
else:
sys.stdout.write(f'\33]0;{title}\a')
sys.stdout.flush()
except FileNotFoundError:
print_and_log("error", f"Cannot set console title to {title}")
except AttributeError:
# Issue #1065
pass
def clearScreen():
if _config.disableScreenClear: # Implement #1162
return
if platform.system() == "Windows":
subprocess.call('cls', shell=True)
else:
subprocess.call('clear', shell=True)
def start_irfanview(dfilename, irfanViewPath, start_irfan_slide=False, start_irfan_view=False):
print_and_log('info', 'starting IrfanView...')
if os.path.exists(dfilename):
ivpath = irfanViewPath + os.sep + 'i_view32.exe' # get first part from config.ini
ivpath = ivpath.replace('\\\\', '\\')
ivpath = ivpath.replace('\\', os.sep)
info = None
if start_irfan_slide:
info = subprocess.STARTUPINFO()
info.dwFlags = 1
info.wShowWindow = 6 # start minimized in background (6)
ivcommand = ivpath + ' /slideshow=' + dfilename
get_logger().info(ivcommand)
subprocess.Popen(ivcommand)
elif start_irfan_view:
ivcommand = ivpath + ' /filelist=' + dfilename
get_logger().info(ivcommand)
subprocess.Popen(ivcommand, startupinfo=info)
else:
print_and_log('error', u'could not load' + dfilename)
def open_text_file(filename, mode='r', encoding='utf-8'):
''' taken from: http://www.velocityreviews.com/forums/t328920-remove-bom-from-string-read-from-utf-8-file.html'''
hasBOM = False
if os.path.isfile(filename):
f = open(filename, 'rb')
header = f.read(4)
f.close()
# Don't change this to a map, because it is ordered
encodings = [(codecs.BOM_UTF32, 'utf-32'),
(codecs.BOM_UTF16, 'utf-16'),
(codecs.BOM_UTF8, 'utf-8')]
for h, e in encodings:
if header.startswith(h):
encoding = e
hasBOM = True
break
f = codecs.open(filename, mode, encoding)
# Eat the byte order mark
if hasBOM:
f.read(1)
return f
def create_avabg_filename(artistModel, targetDir, format_src):
filename_avatar = ""
filename_bg = ""
image = PixivImage(parent=artistModel)
# Issue #795
if artistModel.artistAvatar.find('no_profile') == -1:
# Download avatar using custom name, refer issue #174
if format_src.avatarNameFormat != "":
tmpfilename = make_filename(format_src.avatarNameFormat,
image,
tagsSeparator=_config.tagsSeparator,
tagsLimit=_config.tagsLimit,
fileUrl=artistModel.artistAvatar,
appendExtension=True)
filename_avatar = sanitize_filename(tmpfilename, targetDir)
else:
filenameFormat = format_src.filenameFormat
if filenameFormat.find(os.sep) == -1:
filenameFormat = os.sep + filenameFormat
filenameFormat = os.sep.join(filenameFormat.split(os.sep)[:-1])
tmpfilename = make_filename(filenameFormat,
image,
tagsSeparator=_config.tagsSeparator,
tagsLimit=_config.tagsLimit,
fileUrl=artistModel.artistAvatar,
appendExtension=False)
filename_avatar = sanitize_filename(tmpfilename + os.sep + 'folder.' + artistModel.artistAvatar.rsplit(".", 1)[1], targetDir)
if artistModel.artistBackground is not None and artistModel.artistBackground.startswith("http"):
if format_src.backgroundNameFormat != "" and format_src.avatarNameFormat != format_src.backgroundNameFormat:
tmpfilename = make_filename(format_src.backgroundNameFormat,
image,
tagsSeparator=_config.tagsSeparator,
tagsLimit=_config.tagsLimit,
fileUrl=artistModel.artistBackground,
appendExtension=True)
filename_bg = sanitize_filename(tmpfilename, targetDir)
else:
if format_src.avatarNameFormat != "":
tmpfilename = make_filename(format_src.avatarNameFormat,
image,
tagsSeparator=_config.tagsSeparator,
tagsLimit=_config.tagsLimit,
fileUrl=artistModel.artistBackground,
appendExtension=True)
tmpfilename = tmpfilename.split(os.sep)
tmpfilename[-1] = "bg_" + tmpfilename[-1]
filename_bg = sanitize_filename(os.sep.join(tmpfilename), targetDir)
else:
filenameFormat = format_src.filenameFormat
if filenameFormat.find(os.sep) == -1:
filenameFormat = os.sep + filenameFormat
filenameFormat = os.sep.join(filenameFormat.split(os.sep)[:-1])
tmpfilename = make_filename(filenameFormat,
image,
tagsSeparator=_config.tagsSeparator,
tagsLimit=_config.tagsLimit,
fileUrl=artistModel.artistBackground,
appendExtension=False)
filename_bg = sanitize_filename(tmpfilename + os.sep + 'bg_folder.' + artistModel.artistBackground.rsplit(".", 1)[1], targetDir)
return (filename_avatar, filename_bg)
def we_are_frozen():
# """Returns whether we are frozen via py2exe.
# This will affect how we find out where we are located.
# Get actual script directory
# http://www.py2exe.org/index.cgi/WhereAmI"""
# return hasattr(sys, "frozen")
''' updated for PyInstaller from https://pyinstaller.org/en/stable/runtime-information.html'''
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
# print('running in a PyInstaller bundle')
return True
else:
# print('running in a normal Python process')
return False
def module_path():
""" This will get us the program's directory,
even if we are frozen using py2exe"""
if we_are_frozen():
return os.path.dirname(sys.executable)
return os.path.dirname(__file__)
def speed_in_str(totalSize, totalTime):
if totalTime > 0:
speed = totalSize / totalTime
if speed < 1024:
return "{0:.0f} B/s".format(speed)
speed = speed / 1024
if speed < 1024:
return "{0:.2f} KiB/s".format(speed)
speed = speed / 1024
if speed < 1024:
return "{0:.2f} MiB/s".format(speed)
speed = speed / 1024
return "{0:.2f} GiB/s".format(speed)
else:
return " infinity B/s"
def size_in_str(totalSize):
totalSize = float(totalSize)
if totalSize < 1024:
return "{0:.0f} B".format(totalSize)
totalSize = totalSize / 1024
if totalSize < 1024:
return "{0:.2f} KiB".format(totalSize)
totalSize = totalSize / 1024
if totalSize < 1024:
return "{0:.2f} MiB".format(totalSize)
totalSize = totalSize / 1024
return "{0:.2f} GiB".format(totalSize)
def dump_html(filename, html_text):
isDumpEnabled = True
filename = sanitize_filename(filename)
if _config is not None:
isDumpEnabled = _config.enableDump
if _config.enableDump:
if len(_config.skipDumpFilter) > 0:
matchResult = re.findall(_config.skipDumpFilter, filename)
if matchResult is not None and len(matchResult) > 0:
isDumpEnabled = False
if html_text is not None and len(html_text) == 0:
print_and_log('info', 'Empty Html.')
return ""
if isDumpEnabled:
if not isinstance(html_text, str):
html_text = str(html_text)
if isinstance(html_text, str):
html_text = html_text.encode()
try:
dump = open(filename, 'wb')
dump.write(html_text)
dump.close()
return filename
except IOError as ex:
print_and_log('error', str(ex))
print_and_log("info", "Dump File created: {0}".format(filename))
else:
print_and_log('info', 'Dump not enabled.')
return ""
def print_and_log(level, msg, exception: Exception = None, newline=True, end=None):
if level is None:
safePrint(msg, newline, end)
return
msg_no_color = __ansi_color.sub('', msg)
if level == 'debug':
get_logger().debug(msg_no_color)
elif level == 'info':
safePrint(msg, newline, end)
get_logger().info(msg_no_color)
elif level == 'warn':
safePrint(Fore.YELLOW + f"{msg}" + Style.RESET_ALL, newline, end)
get_logger().warning(msg_no_color)
elif level == 'error':
safePrint(Fore.RED + f"{msg}" + Style.RESET_ALL, newline, end)
if exception is None or not isinstance(exception, Exception) or exception.__traceback__ is None:
get_logger().error(msg_no_color)
else:
get_logger().error(msg_no_color)
get_logger().error(traceback.format_exc())
def have_strings(page, strings):
for string in strings:
pattern = re.compile(string)
test_2 = pattern.findall(str(page))
if len(test_2) > 0:
if len(test_2[-1]) > 0:
return True
return False
def get_ids_from_csv(ids_str, is_string=False):
ids = []
if is_string:
ids = re.findall(r"(?:@|^|https:\/\/(?!www|sketch\.)|\s|,)(?!https:)(\d+|\S[\S]*\S)", ids_str)
if not ids:
print_and_log('error', u"Input: {0} is not valid".format(ids_str))
else:
ids = re.findall(r"(?:series|users|\s|,|^|artworks|posts)\/?(\d+)", ids_str)
if not ids:
print_and_log('error', u"Input: {0} is not valid".format(ids_str))
if len(ids) > 1:
print_and_log('info', u"Found {0} ids".format(len(ids)))
return ids
def clear_all():
all_vars = [var for var in globals() if (var[:2], var[-2:]) != ("__", "__") and var != "clear_all"]
for var in all_vars:
del globals()[var]
# # pylint: disable=W0612
# def unescape_charref(data, encoding):
# ''' Replace default mechanize method in _html.py'''
# try:
# name, base = data, 10
# if name.lower().startswith("x"):
# name, base = name[1:], 16
# try:
# int(name, base)
# except ValueError:
# base = 16
# uc = chr(int(name, base))
# if encoding is None:
# return uc
# try:
# repl = uc.encode(encoding)
# except UnicodeError:
# repl = "&#%s;" % data
# return repl
# except BaseException:
# return data
def get_ugoira_size(ugoName):
size = 0
try:
with zipfile.ZipFile(ugoName) as z:
animJson = z.read("animation.json")
size = json.loads(animJson)['zipSize']
z.close()
except zipfile.BadZipFile:
print_and_log('error', u'Failed to read ugoira size from json data: {0}, using filesize.'.format(ugoName))
size = os.path.getsize(ugoName)
return size
def check_file_exists(overwrite, filename, file_size, old_size, backup_old_file):
if not overwrite and int(file_size) == old_size:
print_and_log('warn', f"\tFile exist! (Identical Size) ==> {filename}.")
return PixivConstant.PIXIVUTIL_SKIP_DUPLICATE
else:
if backup_old_file:
split_name = filename.rsplit(".", 1)
new_name = filename + "." + str(int(time.time()))
if len(split_name) == 2:
new_name = split_name[0] + "." + str(int(time.time())) + "." + split_name[1]
print_and_log('warn', f"\t Found file with different file size ==> {filename}, backing up to: {new_name}.")
os.rename(filename, new_name)
else:
print_and_log('warn', f"\tFound file with different file size ==> {filename}, removing old file (old: {old_size} vs new: {file_size})")
os.remove(filename)
return PixivConstant.PIXIVUTIL_OK
def print_delay(retry_wait):
repeat = range(1, retry_wait + 1)
for t in repeat:
print_and_log(None, f"\r{t} of {retry_wait}s.", newline=False)
time.sleep(1)
print_and_log(None, "")
def create_custom_request(url, config, referer='https://www.pixiv.net', head=False):
if config.useProxy:
proxy = urllib.request.ProxyHandler(config.proxy)
opener = urllib.request.build_opener(proxy)
urllib.request.install_opener(opener)
req = mechanize.Request(url)
req.add_header('Referer', referer)
# print_and_log('info', u"Using Referer: " + str(referer))
get_logger().info(f"Using Referer: {referer}")
if head:
req.get_method = lambda: 'HEAD'
else:
req.get_method = lambda: 'GET'
return req
def makeSubdirs(filename):
directory = os.path.dirname(filename)
if not os.path.exists(directory) and len(directory) > 0:
print_and_log('info', u'Creating directory: ' + directory)
os.makedirs(directory)
def download_image(url, filename, res, file_size, overwrite):
''' Actual download, return the downloaded filesize and saved filename.'''
start_time = datetime.now()
global _config
BUFFER_SIZE = _config.downloadBuffer * 1024
# try to save to the given filename + .pixiv extension if possible
try:
makeSubdirs(filename)
save = open(filename + '.pixiv', 'wb+', 4096)
except IOError as ex:
print_and_log('error', f"Error at download_image(): Cannot save {url} to {filename}: {sys.exc_info()}", exception=ex)
input("Press enter to continue or Ctrl+C to abort.") # Issue #1187
# get the actual server filename and use it as the filename for saving to current app dir
filename = os.path.split(url)[1]
filename = filename.split("?")[0]
filename = sanitize_filename(filename)
save = open(filename + '.pixiv', 'wb+', 4096)
print_and_log('info', f'File is saved to {filename}')
# download the file
prev = 0
curr = 0
msg_len = 0
try:
while True:
save.write(res.read(BUFFER_SIZE))
curr = save.tell()
msg_len = print_progress(curr, file_size, msg_len)
# check if downloaded file is complete
if file_size > 0 and curr == file_size:
total_time = (datetime.now() - start_time).total_seconds()
print_and_log(None, f' Completed in {Fore.CYAN}{total_time}{Style.RESET_ALL}s ({Fore.RED}{speed_in_str(file_size, total_time)}{Style.RESET_ALL})')
break
elif curr == prev: # no file size info
total_time = (datetime.now() - start_time).total_seconds()
print_and_log(None, f' Completed in {Fore.CYAN}{total_time}{Style.RESET_ALL}s ({Fore.RED}{speed_in_str(curr, total_time)}{Style.RESET_ALL})')
break
prev = curr
except OSError as ex:
print_and_log('error', f"Error at download_image(): Cannot save {url} to {filename}: {sys.exc_info()}", exception=ex)
input("Press enter to continue or Ctrl+C to abort.") # Issue #1187
raise
finally:
if save is not None:
save.close()
completed = True
if file_size > 0 and curr < file_size:
# File size is known and downloaded file is smaller
print_and_log('error', f'Downloaded file incomplete! {curr:9} of {file_size:9} Bytes')
print_and_log('error', f'Filename = {filename}')
print_and_log('error', f'URL = {url}')
completed = False
elif curr == 0:
# No data received.
print_and_log('error', 'No data received!')
print_and_log('error', f'Filename = {filename}')
print_and_log('error', f'URL = {url}')
completed = False
if completed:
if overwrite and os.path.exists(filename):
os.remove(filename)
os.rename(filename + '.pixiv', filename)
else:
os.remove(filename + '.pixiv')
del save
return (curr, filename)
def print_progress(curr, total, max_msg_length=80):
# [12345678901234567890]
# [████████------------]
# [━╸ ]
animBarLen = 40
if total > 0:
complete = int((curr * animBarLen) / total)
remainder = (((curr * animBarLen) % total) / total)
use_half_block = (remainder <= 0.5) and remainder > 0.1
color = f"{Fore.GREEN}{Style.BRIGHT}" if complete == animBarLen else Fore.RED
if use_half_block:
with_half_block = f"{'━' * (complete - 1)}╸"
msg = f"\r{color}[{with_half_block:{animBarLen}}]{Style.RESET_ALL} {size_in_str(curr)} of {size_in_str(total)}"
elif complete == animBarLen:
msg = f"\r{color}[{'━' * complete:{animBarLen}}]{Style.RESET_ALL} {size_in_str(total)}"
else:
msg = f"\r{color}[{'━' * complete:{animBarLen}}]{Style.RESET_ALL} {size_in_str(curr)} of {size_in_str(total)}"
else:
# indeterminite
pos = curr % (animBarLen + 3) # 3 corresponds to the length of the '███' below
anim = '.' * animBarLen + '███' + '.' * animBarLen
# Use nested replacement field to specify the precision value. This limits the maximum print
# length of the progress bar. As pos changes, the starting print position of the anim string
# also changes, thus producing the scrolling effect.
msg = f'\r{Fore.YELLOW}[{anim[animBarLen + 3 - pos:]:.{animBarLen}}]{Style.RESET_ALL} {size_in_str(curr)}'
curr_msg_length = len(msg)
print_and_log(None, msg.ljust(max_msg_length, " "), newline=False)
return curr_msg_length if curr_msg_length > max_msg_length else max_msg_length
def generate_search_tag_url(tags,
page,
title_caption=False,
wild_card=False,
sort_order='date_d',
start_date=None,
end_date=None,
member_id=None,
r18mode=False,
blt=0,
type_mode="a",
locale=""):
url = ""
date_param = ""
page_param = ""
if start_date is not None:
date_param = f"{date_param}&scd={start_date}"
if end_date is not None:
date_param = f"{date_param}&ecd={end_date}"
if page is not None and int(page) > 1:
page_param = f"&p={page}"
mode = '&mode=all'
if r18mode:
mode = '&mode=r18'
order = ''
if sort_order in ('date', 'date_d', 'popular_d', 'popular_male_d', 'popular_female_d'):
order = f'&order={sort_order}'
if locale != "":
if locale.startswith("/"):
locale = locale[1:]
locale = f"&lang={locale}"
if member_id is not None:
url = f'https://www.pixiv.net/member_illust.php?id={member_id}&tag={tags}&p={page}{mode}{order}'
else:
root_url = 'https://www.pixiv.net/ajax/search/artworks'
search_mode = ""
if title_caption:
search_mode = '&s_mode=s_tc'
print_and_log(None, "Using Title Match (s_tc)")
elif wild_card:
# partial match
search_mode = '&s_mode=s_tag'
print_and_log(None, "Using Partial Match (s_tag)")
else:
search_mode = '&s_mode=s_tag_full'
print_and_log(None, "Using Full Match (s_tag_full)")
bookmark_limit_premium = ""
if blt is not None and blt > 0:
bookmark_limit_premium = f'&blt={blt}'
if type_mode == "i":
type_mode = "illust_and_ugoira"
elif type_mode == "m":
type_mode = "manga"
else:
type_mode = "all"
type_mode = f"&type={type_mode}"
# https://www.pixiv.net/ajax/search/artworks/k-on?word=k-on&order=date_d&mode=all&p=1&s_mode=s_tag_full&type=all&lang=en
# https://www.pixiv.net/ajax/search/artworks/GuP or ガルパン or ガールズ&パンツァー or garupan?word=GuP or ガルパン or ガールズ&パンツァー or garupan
# &order=date&mode=all&scd=2022-01-25&p=1&s_mode=s_tag&type=all&lang=en
url = f"{root_url}/{tags}?word={tags}{order}{mode}{date_param}{page_param}{search_mode}{bookmark_limit_premium}{type_mode}{locale}"
# encode to ascii
# url = url.encode('iso_8859_1')
return url
def write_url_in_description(image: Union[PixivImage, FanboxPost], blacklistRegex, filenamePattern):
valid_url = list()
if len(image.descriptionUrlList) > 0:
# filter first
if len(blacklistRegex) > 0:
for link in image.descriptionUrlList:
res = re.findall(blacklistRegex, link)
if len(res) == 0:
valid_url.append(link)
else:
valid_url = image.descriptionUrlList
# then write
if len(valid_url) > 0:
if len(filenamePattern) == 0:
filenamePattern = "url_list_%Y%m%d"
filename = date.today().strftime(filenamePattern) + ".txt"
makeSubdirs(filename)
info = codecs.open(filename, 'a', encoding='utf-8')
# implement #1002
if isinstance(image, FanboxPost):
info.write(f"# Fanbox Author ID: {image.parent.artistId} Post ID: {image.imageId}\r\n")
else:
info.write(f"# Pixiv Author ID: {image.artist.artistId} Image ID: {image.imageId}\r\n")
for link in valid_url:
info.write(link + "\r\n")
info.close()
def ugoira2gif(ugoira_file, exportname, fmt='gif', image=None):
print_and_log('info', 'Processing ugoira to animated gif...')
# Issue #802 use ffmpeg to convert to gif
if len(_config.gifParam) == 0:
_config.gifParam = "-filter_complex [0:v]split[a][b];[a]palettegen=stats_mode=diff[p];[b][p]paletteuse=dither=bayer:bayer_scale=5:diff_mode=rectangle -vsync 0"
convert_ugoira(ugoira_file,
exportname,
ffmpeg=_config.ffmpeg,
codec=None,
param=_config.gifParam,