Skip to content

Commit

Permalink
Rename download(filename -> path), add download demo for rankings
Browse files Browse the repository at this point in the history
  • Loading branch information
upbit committed Aug 6, 2016
1 parent 370e33a commit 39a89b5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
42 changes: 42 additions & 0 deletions download_illusts.py
Original file line number Diff line number Diff line change
@@ -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()
8 changes: 4 additions & 4 deletions pixivpy3/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 39a89b5

Please sign in to comment.