Skip to content

Commit

Permalink
Reformat arg parse
Browse files Browse the repository at this point in the history
  • Loading branch information
wujiawei committed Aug 3, 2017
1 parent 8916093 commit 6385c0f
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 47 deletions.
16 changes: 9 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
douban album downloader
douban downloader
=======================

A simple python script to download douban albums.
A simple python script to download douban albums and celebrity


installation
------------

::

$ pip install douban-album-dl
$ pip install douban-dl


usage
-----

::

$ douban-album-dl album_id [location]
$ douban-dl url [path]

``album_id`` is the last number of the douban album url.
i.e. "https://www.douban.com/photos/album/**<ALBUM_ID>**"
``url`` should be like this:

``location`` is the folder where images saved, defaults to ``./album``.
https://www.douban.com/photos/album/<album_id>
https://movie.douban.com/celebrity/<celebrity_id>

``path`` is the folder where images saved, defaults to ``./douban``.

license
-------
Expand Down
58 changes: 58 additions & 0 deletions douban/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import re

from douban.album import Album
from douban.celebrity import Celebrity
from douban.douban_album_dl import get_album
from douban.douban_celebrity_dl import get_celebrity


def get_args():
"""
argparse 选项的默认语法是基于 Unix 约定, `-` 来表示命令行开关
:return:
"""
parser = argparse.ArgumentParser(prog='Douban downloader',
description="""
A tiny tool to download douban resources
""")
parser.add_argument("url", help="start url of album or any other pages")
parser.add_argument('path', default='./douban', nargs='?', help='the path to store all resources') # 可选
# parser.add_argument('-a', '--album', help='album id', action='store')
# parser.add_argument('-c', '--celebrity', help='celebrity id', action='store')
return parser.parse_args()


def parse_url(url, path):
"""
https://www.douban.com/photos/album/<album_id>
https://movie.douban.com/celebrity/<celebrity_id>
:param url:
:return:
"""
match = re.match(r'https://www.douban.com/photos/album/(\d+)', url)
if match:
album_id = match.group(1)
album = Album(album_id)
get_album(album, path)
return
match = re.match(r'https://movie.douban.com/celebrity/(\d+)', url)
if match:
celebrity_id = match.group(1)
celebrity = Celebrity(celebrity_id)
get_celebrity(celebrity, path)
return
print "Not support this url yet"


def main():
"""Main entry point"""
args = get_args()
if args.url is not None:
parse_url(args.url, args.path)


if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions douban/album.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from bs4 import BeautifulSoup
import requests


class Album:
BASE_URL = "https://www.douban.com/photos/album/"

Expand Down
29 changes: 5 additions & 24 deletions bin/douban-album-dl → douban/douban_album_dl.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/env python

from douban.album import Album
import requests
import sys
import os

import requests

from utils import file_utils

headers = {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"accept-encoding": "gzip, deflate, br",
Expand All @@ -14,20 +15,9 @@
}


def mayday():
h = """douban-album-dl album_id [location=./album]"""

print(h)


def mkdir(path):
if not os.path.exists(path):
os.makedirs(path)


def get_album(album, path):
idx = 0
mkdir(path)
file_utils.mkdir(path)
os.chdir(path)
for photo_url in album.photos():
name = os.path.basename(photo_url)
Expand All @@ -37,12 +27,3 @@ def get_album(album, path):
f.write(r.content)
idx += 1
print("saving album to {}, total {} images".format(path, idx))


if __name__ == "__main__":
if len(sys.argv) == 1:
mayday()
else:
album = Album(sys.argv[1])
path = "./album" if len(sys.argv) == 2 else sys.argv[2]
get_album(album, path)
File renamed without changes.
38 changes: 22 additions & 16 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
"""Douban Album Downloader"""


from setuptools import setup, find_packages

requirements = [
"bs4",
"requests"
]

setup(
name = "douban album dl",
version = "0.0.1",
description = "douban album downloader",
long_description = "douban album downloader",
url = "https://github.com/delta4d/douban-album-dl",
author = "delta",
email = "delta4d@gmail.com",
license = "MIT",
scripts = ["bin/douban-album-dl"],
keywords = "douban dl",
packages = find_packages(exclude = ["tests"]),
install_requires = [
"bs4",
"requests"
]
name="douban_dl",
version="0.0.1",
description="douban downloader, include album, celebrity",
long_description="douban album downloader",
url="https://github.com/einverne/douban-dl",
author="einverne",
email="einverne@gmail.com",
license="MIT",
# scripts=["bin/douban-album-dl"],
# not use scripts while use entry_points instead https://packaging.python.org/tutorials/distributing-packages/#scripts
entry_points={
'console_scripts': [
'douban_dl = douban.__main__:main',
]},
keywords="douban dl",
packages=find_packages(exclude=["tests"]),
install_requires=requirements,
)
2 changes: 2 additions & 0 deletions utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
8 changes: 8 additions & 0 deletions utils/file_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os


def mkdir(path):
if not os.path.exists(path):
os.makedirs(path)

0 comments on commit 6385c0f

Please sign in to comment.