Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _get_bookmark_artworks(self, limit=None):
params = dict()
params['id'] = self.id
curr_page = 0
last_page = 1 # a number more than curr_page, change later in loop
bookmark_ids = []
try:
while curr_page < last_page:
curr_page += 1
params['p'] = curr_page
res = util.req(session=self.session, url=self._bookmarks_url, params=params)
res_json = util.json_loads(res.content)
bookmark_ids += [illust['id'] for illust in res_json['bookmarks']]
if limit is not None:
if len(bookmark_ids) > limit:
bookmark_ids = util.trim_to_limit(items=bookmark_ids, limit=limit)
break
last_page = res_json['lastPage']
except (ReqException, json.JSONDecodeError, KeyError) as e:
util.log(str(e), error=True)
util.log('Failed to get bookmarks from id:', self.id)
bookmarks = util.generate_artworks_from_ids(bookmark_ids)
return bookmarks
raise UserError('Failed to retrieve r18/r18g settings')
# save user token, for changing r18 and r18g
self._token = self._get_token()
# if username and password is provided, this is a logined user
self._login = True
# if user id is not given and failed to retrieve user id
if not user_id:
raise UserError('Failed to get user id')
# get information from user id
self.data = dict()
params = dict({'id': user_id})
try:
data = util.req(session=self.session, url=self._user_details_url, params=params)
data_json = util.json_loads(data.text)
except (ReqException, json.JSONDecodeError) as e:
util.log(str(e), error=True, save=True)
raise UserError('Failed to load user information')
# save user information, not used yet, for filter in the future
data_json = data_json['user_details']
self.id = data_json['user_id']
self.account = data_json['user_account']
self.name = data_json['user_name']
self.title = data_json['meta']['title']
self.description = data_json['meta']['description']
self.pixiv_url = data_json['meta']['canonical']
self.follows = data_json['follows']
self.background_url = data_json['bg_url']
def _rank(self, params, limit):
ids = []
page_num = 0
# exception_count = 0
while True:
page_num += 1
params['p'] = page_num
try:
res = util.req(type='get', session=self._session, url=self.url, params=params)
res = util.json_loads(res.content)
except (ReqException, json.JSONDecodeError) as e:
util.log(str(e), error=True, save=True)
util.log('End of rank at page:', page_num, inform=True, save=True)
break
if 'error' in res:
util.log('End of page while searching', str(params) + '. Finished')
break
else:
ids += [content['illust_id'] for content in res['contents']]
# check if number of ids reached requirement
if limit:
num_of_ids_found = len(ids)
if limit == num_of_ids_found:
break
def _get_token(self):
try:
res = util.req(url=self._settings_url, session=self.session)
tt_result = re.search(r'name="tt" value="(.*?)"', res.text)
if tt_result:
tt = tt_result.group(1)
util.log('successfully retrieved user token:', tt)
return tt
else:
raise UserError('Failed to find user token in respond')
except ReqException as e:
raise UserError('Failed to retrieve user token')
def _search(self, params, keyword, popularity, limit):
curr_page = 1
ids_sofar = []
while True:
# get a page's ids
params['p'] = curr_page
params['word'] = str(keyword)
if popularity != None:
params['word'] += ' ' + str(popularity) + self._search_popularity_postfix
util.log('Searching id for params:', params, 'at page:', curr_page)
try:
err_msg = 'Failed getting ids from params ' + str(params) + ' page: ' + str(curr_page)
results = util.req(type='get', session=self._session, url=self._search_url, params=params,
err_msg=err_msg, log_req=False)
except ReqException as e:
util.log(str(e), error=True, save=True)
if curr_page == 1:
util.log('Theres no result found for input', inform=True, save=True)
else:
util.log('End of search at page: ' + str(curr_page), inform=True, save=True)
return ids_sofar
ids = re.findall(self._search_regex, results.text)
# set length of old ids and new ids,
# use later to check if reached end of all pages
old_len = len(ids_sofar)
ids_sofar += ids
ids_sofar = list(set(ids_sofar))
def __init__(self, id):
self.id = id
_ajax_url = self._ajax_url + str(self.id)
try:
respond = util.req(type='get', url=_ajax_url, log_req=False)
image_data = util.json_loads(respond.content)
image_data = image_data['body']
self.original_url = image_data['urls']['original']
self.views = image_data['viewCount']
self.bookmarks = image_data['bookmarkCount']
self.likes = image_data['likeCount']
self.comments = image_data['commentCount']
self.title = image_data['illustTitle']
self.author = image_data['userName']
self.page_count = image_data['pageCount']
self.original_url = re.sub(r'(?<=_p)\d', '{page_num}', self.original_url)
except ReqException as e:
util.log(str(e), error=True, save=True)
raise ArtworkError('Artwork id:', str(self.id), 'has been deleted or does not exists')
# login session
self.session = session
# for login user
self._r18 = None
self._r18g = None
self._lang = None
self._login = False
# find pixiv id from username and password and save r18 & r18g settings
if username and password:
try:
login_page = LoginPage()
self.session = login_page.login(username=username, password=password)
status_data = util.req(session=self.session, url=self._self_details_url)
status_data_json = util.json_loads(status_data.text)
user_id = status_data_json['body']['user_status']['user_id']
except ReqException as e:
util.log(str(e), error=True, save=True)
raise UserError('Failed to load user id')
# save user's settings for r18 and r18g
try:
res = util.req(url=self._settings_url, session=self.session)
self._r18 = re.search(r'name="r18" value="show" checked>', res.text) is not None
self._r18g = re.search(r'name="r18g" value="2" checked>', res.text) is not None
lang_search_res = re.search(r'option value="(\w\w)" selected>', res.text)
if lang_search_res:
self._lang = lang_search_res.group(1)
else:
raise UserError('Failed to find user language in respond')
form['user_language'] = self._lang
form['submit'] = self._user_lang_dict[self._lang]
form['tt'] = self._token if self._token else self._get_token()
if r18g is not None: # if want to change r18g
form['r18g'] = '2' if r18g else '1'
else:
form['r18g'] = '2' if self._r18g else '1'
if r18 is not None: # if want to change r18
form['r18'] = 'show' if r18 else 'hide'
else:
form['r18'] = 'show' if self._r18 else 'hide'
# submit change request
util.req(type='post', url=self._settings_url, session=self.session, data=form)
def _check_is_logged(self):
status_json = util.req(url=self._login_check_url, session=self._session).json()
return status_json['body']['user_status']['is_logged_in']