-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
download all images from user #236
Labels
Comments
Not sure the purpose of this thread, but here is mine. It utilized Codeimport os
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
import requests
from tqdm import tqdm
from pixivpy3 import AppPixivAPI
USER_ID = '15919563'
DOWNLOAD_DIR = Path(r'SOME-WHERE')
REFRESH_TOKEN_FILE = Path(r'SOME-WHERE\refresh-token.txt')
def auth_pixiv_api(api: AppPixivAPI, refresh_token_file: Path):
with refresh_token_file.open('rt') as f:
refresh_token = f.read().strip()
api.auth(refresh_token=refresh_token)
with refresh_token_file.open('wt') as f:
print(api.refresh_token, file=f)
def download(url: str, file: Path, headers=None, force=False):
if file.exists() and not force:
return
with requests.get(url, headers=headers, stream=True) as response:
response.raise_for_status()
with tqdm(
total=int(response.headers.get('Content-Length', 0)),
desc=f'Download: {file.name}',
unit='B', unit_scale=True, unit_divisor=1024,
leave=False,
) as progress:
file.parent.mkdir(exist_ok=True)
with file.open('wb') as f:
for chunk in response.iter_content(chunk_size=1024):
if not chunk:
continue
f.write(chunk)
progress.update(len(chunk))
def main():
api = AppPixivAPI()
auth_pixiv_api(api, REFRESH_TOKEN_FILE)
with ThreadPoolExecutor(
max_workers=5,
initializer=tqdm.set_lock, initargs=(tqdm.get_lock(),),
) as executor:
qs = {'user_id': USER_ID}
root = DOWNLOAD_DIR / USER_ID
while qs:
json_result = api.user_illusts(**qs)
qs = api.parse_qs(json_result.next_url)
for illust in json_result.illusts:
if illust.type == 'ugoira':
img_urls = [] # Skip ugoira
elif illust.page_count == 1:
img_urls = [illust.meta_single_page.original_image_url]
else:
img_urls = [
page.image_urls.original
for page in illust.meta_pages
]
for url in img_urls:
executor.submit(
download,
url,
root / os.path.basename(url),
headers={'Referer': 'https://app-api.pixiv.net/'},
force=True,
)
if __name__ == '__main__':
main() I used to have a complex crawler that can even convert ugoira to gif, but now I don't use it anymore, so I don't continue to maintain it. |
@compwron Did you want a method to "download all images from user", or did you want to know how to implement it with pixivpy? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is some messy code which uses this library to download all images for a particular user.
The text was updated successfully, but these errors were encountered: