-
Notifications
You must be signed in to change notification settings - Fork 0
/
tumbl-down.py
executable file
·73 lines (64 loc) · 2.77 KB
/
tumbl-down.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""Main module; provides user-facing interface to the software"""
import argparse
import csv
from pathlib import Path
from random import sample
import common
import service
from common import debug_out
from common.downloader import download_from_feed
SERVICES = service.index_services()
def cmd_main(args):
"""
tumbl-down main function; program entry point
:param args: parsed arguments by ArgumentParser
"""
service_type = args.service
debug_out(f"Service type: {service_type}")
common.DEBUG = args.verbose
if service_type == 'update':
update_main()
return
if not args.account_id:
print("No accounts specified. Add account handles to download.")
for i, account in enumerate(args.account_id):
print(f"Fetching feed of {account}..."
f" ({i+1}/{len(args.account_id)})")
download_from_feed(SERVICES[service_type], account, f"downloads/{account}",
post_count=args.posts, redownload=args.force)
def _init_argparser():
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", help="increase output verbosity",
action="store_true")
parser.add_argument("-f", "--force", help="re-download already downloaded"
" images", action="store_true")
parser.add_argument("-n", "--posts", type=int, default=20,
help="specify number of posts to query")
parser.add_argument("service", help="type of service to use")
parser.add_argument("account_id",
help="the account ID(s) to download images from",
nargs='*')
parser.add_argument("-b", "--reblogs", type=bool, default=True, help="specify whether to include reblogged content")
return parser
def _read_config(filename: str = "config.csv", path: str = '.') -> list[list]:
filepath = Path(path)/filename
if filepath.exists():
config_list = []
with filepath.open(newline='') as f:
for row in csv.reader(f, delimiter=','):
config_list.append(row)
return sample(config_list, len(config_list))
print("config.csv not found. Please create the file first.")
return []
def update_main():
"""Function that executes pre-programmed list of downloads."""
configs = _read_config()
for i, config in enumerate(configs):
account, service, dl_path, incl_reblog = [x.strip() for x in config]
print(f"Fetching feed of {account}... ({i+1}/{len(configs)})")
download_from_feed(SERVICES[service], account, dl_path,
post_count=-1, redownload=args.force, incl_reblog=(incl_reblog == "1"))
if __name__ == "__main__":
parser = _init_argparser()
args = parser.parse_args()
cmd_main(args=args)