Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def access(self, user_id):
"""Given a pixiv user id and returns a User Object
"""
util.log('access:', user_id, 'with user:', self.user)
return User(user_id=user_id, session=self.user.session if self.user else None)
def __sub__(self, other):
self_len = len(self.artworks)
other_len = len(other.artworks)
artworks = list(item for item in self.artworks if item not in other.artworks)
new_len = len(artworks)
folder = util.clean_filename(str(self.folder) + '-' + str(other.folder))
result = PixivResult(artworks, folder)
util.log('Subbed {self_len} - {other_len} => {sum}'.format(self_len=self_len, other_len=other_len, sum=new_len),
start='\r\n', inform=True)
return result
def _handle_failed_login(self):
util.log('Login with local cookies and username and password failed, please enter cookies manually', error=True)
cookies = self._get_cookies_from_user()
return self.login_with_cookies(cookies)
def illusts(self, limit=None):
"""Returns illustrations uploaded by this user
**Parameters**
:param limit:
limit the amount of illustrations found, if exceed
:rank_type limit:
int or None
:return: the results of attempting to retrieve this user's uploaded illustrations
:rtype: PixivResult Object
"""
util.log('Getting illustrations from id:', self.id)
if self.has_illusts:
self.illust_artworks = self._get_illust_artworks(limit=limit)
else:
util.log('User with id:', self.id, 'has no illustrations')
self.illust_artworks = []
util.log('Found', len(self.illust_artworks), 'illustrations')
folder = settings.USER_ILLUSTS_DOWNLOAD_FOLDER.format(title=self.title)
result = PixivResult(self.illust_artworks, folder=folder)
return result
def __lt__(self, value):
util.log('Filtering {name} < {value}'.format(name=self.name, value=value), start='\r\n', inform=True)
old_len = len(self.outer_instance.artworks)
artworks = list(filter(lambda item: getattr(item, self.name) < value, self.outer_instance.artworks))
new_len = len(artworks)
folder = util.clean_filename(str(self.outer_instance.folder) + 'lt' + str(value))
result = PixivResult(artworks, folder)
util.log('done {} => {}'.format(old_len, new_len), inform=True)
return result
def search(self, keyword, limit=None, type=None, dimension=None, match=None, popularity=None, order=None,
mode=None):
"""Search Pixiv and returns PixivResult Object
**Description**
Invoke search method in SearchPage and create a PixivResult Object using default folder in settings
**Returns**
:return: result of the search in SearchPage with default search folder as in settings.py
:rtype: PixivResult Object
"""
util.log('Searching:', keyword)
artworks = self.search_page.search(keyword=keyword, type=type, dimension=dimension, match=match,
popularity=popularity, limit=limit, order=order, mode=mode)
folder = settings.SEARCH_RESULTS_FOLDER.format(keyword=keyword, type=type, dimension=dimension, mode=mode,
popularity=popularity, limit=limit)
results = PixivResult(artworks, folder)
return results
if os.path.isfile(file_name):
util.log(pic_detail, 'skipped.', 'Reason:', file_name, 'already exists or access not granted')
skipped = True
curr_page += 1
continue
try:
err_msg = pic_detail + ' Failed'
original_pic_respond = util.req(type='get', url=url, headers=self._headers, err_msg=err_msg,
log_req=False)
with open(file_name, 'wb') as file:
file.write(original_pic_respond.content)
util.log(pic_detail + ' OK', inform=True, start=settings.CLEAR_LINE, end='\r')
except ReqException as e:
util.log(str(e), error=True, save=True)
util.log(pic_detail + ' FAILED', inform=True, save=True, start=settings.CLEAR_LINE)
success = False
curr_page += 1
# log result
if results_dict:
if skipped:
results_dict['skipped'] += 1
elif success:
results_dict['success'] += 1
else:
results_dict['failed'] += 1
results_dict['total pages'] += self.page_count
def _handle_success_login(self):
util.log('Login successfully into Pixiv', inform=True)
cookies_file = settings.COOKIES_FILE
if os.path.isfile(cookies_file):
util.log('Rewriting local cookies: {}'.format(cookies_file))
else:
util.log('Saving cookies to file: {}'.format(cookies_file))
with open(cookies_file, 'wb') as file:
# request cookies is pickle-able
pickle.dump(self._session.cookies, file)
return self._session
def factory(id):
"""return a Artwork Object given its id, None if failed
**Returns**
:return: Artwork of that id
:rtype: Artwork
"""
try:
return Artwork(id)
except ArtworkError as e:
util.log(str(e), error=True, save=True)
return None
# login with local cookie if exists
if os.path.isfile(cookie_file):
util.log(f'Cookie file found: {cookie_file}, attempt login with local cookie')
try:
with open(cookie_file, 'rb') as f:
self._session.cookies = pickle.load(f)
if self._check_is_logged():
util.log('Logged in successfully with local cookies', inform=True)
return self._session
else:
os.remove(cookie_file)
util.log('Removed outdated cookies', inform=True)
except pickle.UnpicklingError as e:
os.remove(cookie_file)
util.log('Removed corrupted cookies file, message: {}'.format(e))
# local cookies does not exists or outdated
self.password = password
self.username = username
try:
post_key = self._get_post_key_from_pixiv()
self.post_key = post_key
data = {
'pixiv_id': username,
'password': password,
'post_key': post_key,
}
params = {
'lang': 'en'