-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape_pixiv.py
162 lines (129 loc) · 4.75 KB
/
scrape_pixiv.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
import abc
import Utility as utl
from Network import Network
import requests
import csv
from AbstractScraper import AbstractScraper
class PixivScraper(AbstractScraper):
def __init__(self, config, network):
self.urlPostBase = 'http://www.pixiv.net/member_illust.php?mode=medium&illust_id='
self.network = network
#self.network.setupAuth('donmai.us', config)
self.localFile = ''
self.headers = {
'Referer': 'http://spapi.pixiv.net/',
'User-Agent': 'PixivIOSApp/5.1.1',
'Content-Type': 'application/x-www-form-urlencoded',
}
url = 'https://oauth.secure.pixiv.net/auth/token'
data = {
'username': config['username'],
'password': config['password'],
'grant_type': 'password',
'client_id': 'bYGKuGVw91e0NMfPGp44euvGt59s',
'client_secret': 'HP3RmkgAmEGro0gn1x9ioawQE8WMfvLXDz3ZqxpK',
}
r = requests.post(url, headers=self.headers, data=data)
if (not r.status_code in [200, 301, 302]):
print('Error logging into pixiv!')
token = None
try:
# get access_token
token = r.json()
self.access_token = token['response']['access_token']
self.user_id = token['response']['user']['id']
#print("AccessToken:", self.access_token)
except:
print('Get access_token error! Response: %s' % (token))
try:
# get PHPSESSID
raw_cookie = r.headers.get('Set-Cookie')
for cookie_str in raw_cookie.split('; '):
if 'PHPSESSID=' in cookie_str:
self.session = cookie_str.split('=')[1]
#print("Session:", self.session)
except:
print('Get PHPSESSID error! Set-Cookie: %s')
# return auth/token response
self.token = token
self.authHeaders = {
'Authorization': 'Bearer %s' % self.access_token,
'Cookie': 'PHPSESSID=%s' % self.session,
}
#print(self.session)
def setLocalFile(self, target):
self.localFile = target
def findPostByFileName(self, filename):
partName = utl.fileName(filename)
header = {
'User-Agent': 'pixiv-ios-app(ver4.0.0)',
}
params = {
'illust_id': partName,
'PHPSESSID': self.session
}
url = 'http://spapi.pixiv.net/iphone/illust.php'
r = requests.get(url, headers=header, params=params)
if r.status_code == 200 and partName in r.text:
return partName
return 0
def findPostByMD5(self, filename):
return 0
def postExists(self, postID):
url = self.urlPostBase + str(postID)
response, data = self.network.urlRequest(url, 'html')
if response == 200:
return True
else:
return False
def generateRawData(self, postID):
header = {
'User-Agent': 'pixiv-ios-app(ver4.0.0)',
}
params = {
'illust_id': postID,
'PHPSESSID': self.session
}
url = 'http://spapi.pixiv.net/iphone/illust.php'
r = requests.get(url, headers=header, params=params)
try:
if r.status_code == 200 and postID in r.text:
# Here is the data.
return r.text
except:
return 0
return 0
def extractPostInfo(self, rawData):
res = []
for row in csv.reader(rawData):
if len(row) > 0:
res.append(row[0])
#print(res)
temp = {}
temp['id'] = res[0]
temp['extension'] = res[4]
temp['title'] = res[6]
temp['tag_string_artist_japanese'] = res[10]
temp['tag_string_artist'] = res[41]
if len(temp['tag_string_artist']) < 2:
temp['tag_string_artist'] = temp['tag_string_artist_japanese']
temp['source'] = 'http://www.pixiv.net/member_illust.php?mode=medium&illust_id=' + temp['id']
temp['md5'] = utl.md5(self.localFile)
temp['tag_string_general'] = res[22] + ' tag_me'
temp['tag_string'] = res[22] + ' tag_me'
temp['description'] = res[32]
temp['tag_string_character'] = ''
temp['tag_string_copyright'] = ''
temp['rating'] = 's'
if res[44] == '1':
temp['rating'] = 'q'
elif res[44] == '2':
temp['rating'] = 'e'
# These three values are not given.
# So, set them to -1 to indicate we don't want QC to care
temp['width'] = -1
temp['height'] = -1
temp['file_size'] = -1
temp['large_loc'] = res[16]
temp['local_file'] = self.localFile
return temp