Completed
Push — master ( 49ff98...8f356f )
by Makoto
01:10
created

tumdlr.commands.cli()   B

Complexity

Conditions 3

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 32
rs 8.8571
cc 3
1
import logging
2
import urllib
3
4
import click
5
from requests import Session
6
7
from tumdlr.main import pass_context
8
from tumdlr.api import TumblrBlog
9
from tumdlr.containers import TumblrPost
10
11
12
# noinspection PyIncorrectDocstring,PyUnusedLocal
13
@click.command('download', short_help='Download posts from a Tumblr account')
14
@click.argument('URL')
15
@click.option('--images/--skip-images', help='Toggles the downloading of image posts', default=True, envvar='IMAGES')
16
@click.option('--videos/--skip-videos', help='Toggles the downloading of video posts', default=True, envvar='VIDEOS')
17
@pass_context
18
def cli(ctx, url, images, videos):
19
    """
20
    Download posts from a Tumblr account.
21
    """
22
    log = logging.getLogger('tumdlr.commands.downloader')
23
    log.info('Starting a new download session for %s', url)
24
25
    # Get our post information
26
    tumblr = TumblrBlog(url)
27
    progress = 0
28
29
    for post in tumblr.posts():  # type: TumblrPost
30
        # Generic data
31
        progress_data = {
32
            'Progress': '{cur} / {total} posts processed'.format(cur=progress, total=tumblr.post_count),
33
            'Type': post.type.title(),
34
            'Post Date': post.post_date,
35
            'Tags': post.tags
36
        }
37
38
        session = Session()
39
        session.headers.update({'referer': urllib.parse.quote(post.url.as_string())})
40
41
        for file in post.files:
42
            file.download(ctx, session=session, progress_data=progress_data)
43
44
        progress += 1
45