forked from Nandaka/PixivUtil2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PixivTags.py
111 lines (92 loc) · 3.75 KB
/
PixivTags.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
# -*- coding: utf-8 -*-
# pylint: disable=I0011, C, C0302
import json
import os
import re
import PixivHelper
from PixivException import PixivException
class PixivTagsItem:
imageId = 0
bookmarkCount = 0
imageResponse = 0
def __init__(self, image_id, bookmark_count, image_response_count):
self.imageId = image_id
self.bookmarkCount = bookmark_count
self.imageResponse = image_response_count
class PixivTags:
'''Class for parsing tags search page'''
itemList = None
haveImage = None
isLastPage = None
availableImages = 0
__re_illust = re.compile(r'member_illust.*illust_id=(\d*)')
__re_imageItemClass = re.compile(r".*\bimage-item\b.*")
query = ""
memberId = 0
POSTS_PER_PAGE = 60
def parseMemberTags(self, artist, memberId, query=""):
'''process artist result and return the image list'''
self.itemList = list()
self.memberId = memberId
self.query = query
self.haveImage = artist.haveImages
self.isLastPage = artist.isLastPage
for image in artist.imageList:
self.itemList.append(PixivTagsItem(int(image), 0, 0))
def parseTags(self, page, query="", curr_page=1):
payload = json.loads(page)
self.query = query
# check error
if payload["error"]:
raise PixivException('Image Error: ' + payload["message"], errorCode=PixivException.SERVER_ERROR)
# parse images information
self.itemList = list()
ad_container_count = 0
for item in payload["body"]["illustManga"]["data"]:
if "isAdContainer" in item and item["isAdContainer"]:
ad_container_count = ad_container_count + 1
continue
image_id = item["id"]
# like count not available anymore, need to call separate request...
bookmarkCount = 0
imageResponse = 0
tag_item = PixivTagsItem(int(image_id), int(bookmarkCount), int(imageResponse))
self.itemList.append(tag_item)
self.haveImage = False
if len(self.itemList) > 0:
self.haveImage = True
# search page info
self.availableImages = int(payload["body"]["illustManga"]["total"])
# assume it always return 60 images, including the advert
if len(self.itemList) + ad_container_count == PixivTags.POSTS_PER_PAGE:
self.isLastPage = False
else:
self.isLastPage = True
return self.itemList
def PrintInfo(self):
PixivHelper.safePrint('Search Result')
if self.memberId > 0:
PixivHelper.safePrint('Member Id: {0}'.format(self.memberId))
PixivHelper.safePrint('Query: {0}'.format(self.query))
PixivHelper.safePrint('haveImage : {0}'.format(self.haveImage))
PixivHelper.safePrint('urls : {0}'.format(len(self.itemList)))
for item in self.itemList:
print("\tImage Id: {0}\tFav Count:{1}".format(item.imageId, item.bookmarkCount))
PixivHelper.safePrint('total : {0}'.format(self.availableImages))
PixivHelper.safePrint('last? : {0}'.format(self.isLastPage))
@staticmethod
def parseTagsList(filename):
'''read tags.txt and return the tags list'''
tags = list()
if not os.path.exists(filename):
raise PixivException("File doesn't exists or no permission to read: " + filename,
PixivException.FILE_NOT_EXISTS_OR_NO_READ_PERMISSION)
reader = PixivHelper.open_text_file(filename)
for line in reader:
if line.startswith('#') or len(line) < 1:
continue
line = line.strip()
if len(line) > 0:
tags.append(line)
reader.close()
return tags