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