From 39a89b51a2d6926ae72f8bb2f579aba2c93a99d2 Mon Sep 17 00:00:00 2001 From: Zhou Hao Date: Sat, 6 Aug 2016 12:11:13 +0800 Subject: [PATCH] Rename download(filename -> path), add download demo for rankings --- download_illusts.py | 42 ++++++++++++++++++++++++++++++++++++++++++ pixivpy3/api.py | 8 ++++---- 2 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 download_illusts.py diff --git a/download_illusts.py b/download_illusts.py new file mode 100644 index 00000000..5d5cb90b --- /dev/null +++ b/download_illusts.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import os +import sys +if sys.version_info >= (3, 0): + import imp + imp.reload(sys) +else: + reload(sys) + sys.setdefaultencoding('utf8') +sys.dont_write_bytecode = True + +from pixivpy3 import * + +_REQUESTS_KWARGS = { + # 'proxies': { + # 'https': 'http://127.0.0.1:8888', + # }, + # 'verify': False, # PAPI use https, an easy way is disable requests SSL verify +} + +def main(): + aapi = AppPixivAPI(**_REQUESTS_KWARGS) + json_result = aapi.illust_ranking('day', date='2016-08-01') + + directory = "dl" + if not os.path.exists(directory): + os.makedirs(directory) + + # download top3 day rankings to 'dl' dir + for illust in json_result.illusts[:3]: + image_url = illust.meta_single_page.get('original_image_url', illust.image_urls.large) + print("%s: %s" % (illust.title, image_url)) + + filename = os.path.basename(image_url) + extension = os.path.splitext(filename)[1] + path = os.path.join(directory, "illust_id_%d_%s%s" % (illust.id, illust.title, extension)) + aapi.download(illust.image_urls.large, path=path) + +if __name__ == '__main__': + main() diff --git a/pixivpy3/api.py b/pixivpy3/api.py index e1cc2059..4622c16b 100644 --- a/pixivpy3/api.py +++ b/pixivpy3/api.py @@ -101,13 +101,13 @@ def auth(self, username=None, password=None, refresh_token=None): # return auth/token response return token - def download(self, url, filename=None, referer='https://app-api.pixiv.net/'): + def download(self, url, prefix='', path=None, referer='https://app-api.pixiv.net/'): """Download image to file (use 6.0 app-api)""" - if (not filename): - filename = os.path.basename(url) + if (not path): + path = prefix + os.path.basename(url) # Write stream to file response = self.requests_call('GET', url, headers={ 'Referer': referer }, stream=True) - with open(filename, 'wb') as out_file: + with open(path, 'wb') as out_file: shutil.copyfileobj(response.raw, out_file) del response