-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbcthumb.py
113 lines (111 loc) · 5.12 KB
/
bbcthumb.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import argparse
import webparser
import downloader
import sys
import urllib.request
import configparser
import logging as log
config = configparser.ConfigParser()
try:
with open('config.ini') as c:
config.read('config.ini')
except IOError:
res = input('Input the wanted default resolution: ')
dest = input('Input the wanted default destination: ')
nonames = bool(int(input('Do you want to skip episode name fetching (1 for yes, 0 for no): ')))
config = configparser.ConfigParser()
config['DEFAULT'] = {'resolution': res,
'destination': dest,
'nonames': nonames}
with open('config.ini', 'w') as configfile:
config.write(configfile)
sys.exit('Saved config! Closing.')
parser = argparse.ArgumentParser(description='Download BBC programme thumbnails.')
parser.add_argument('pid', type=str, nargs='*',
help='programme pid')
parser.add_argument('--res', metavar='resolution', type=str, action='store',
default=config['DEFAULT']['resolution'],
help='define the thumbnail resolution (Default: {})'.format(config['DEFAULT']['resolution']))
parser.add_argument('--dest', metavar='destination', type=str, action='store',
default=config['DEFAULT']['destination'],
help='define the destination folder (Default: {})'.format(config['DEFAULT']['destination']))
parser.add_argument('-s', '--series', action='store_const',
const=True,
default=False,
help='whole series thumbnail download (currently limited to one series)')
parser.add_argument('--nonames', action='store_const',
const='True',
default=config['DEFAULT']['nonames'],
help='skip fetching episode names (Default: {})'.format(config['DEFAULT']['nonames']))
parser.add_argument('--config', action='store_const',
const=True,
default=False,
help='run configuration of default values')
parser.add_argument('-v', '--verbose', action='store_const',
const=True,
default=False,
help='run program with debug verbosity')
args = parser.parse_args()
if args.config:
res = input('Input the wanted default resolution: ')
dest = input('Input the wanted default destination: ')
nonames = bool(int(input('Do you want to skip episode name fetching (1 for yes, 0 for no): ')))
config = configparser.ConfigParser()
config['DEFAULT'] = {'resolution': res,
'destination': dest,
'nonames': nonames}
with open('config.ini', 'w') as configfile:
config.write(configfile)
sys.exit('Saved config! Closing.')
if args.verbose:
log.basicConfig(format="[%(levelname)s] %(message)s", level=log.DEBUG)
log.info("Verbose output enabled.")
else:
log.basicConfig(format="[%(levelname)s] %(message)s")
if args.dest == '':
log.critical('Default destination missing! Please update the defaults with --config.')
sys.exit()
if not args.pid:
log.critical('No PIDs entered, closing program.')
sys.exit()
log.info('Downloading PIDs {} at resolution {}'.format(args.pid, args.res))
urls = []
for pid in args.pid:
if args.series:
if len(args.pid) > 1:
sys.exit('[CRITICAL] Multiple series pid. Not supported yet!')
error = False
try:
httpcode = urllib.request.urlopen('https://www.bbc.co.uk/programmes/' + pid + '/episodes/guide').getcode()
seriesurl = ('https://www.bbc.co.uk/programmes/' + pid + '/episodes/guide')
except urllib.request.HTTPError as e:
log.critical('Website inaccessible ({})'.format(e))
sys.exit()
log.info('Found matching websites: {}'.format(seriesurl))
imgurls, names = webparser.series(seriesurl, args.res, args.nonames)
log.info('Adjusted resolution image urls: {}'.format(imgurls))
else:
try:
httpcode = urllib.request.urlopen('https://www.bbc.co.uk/programmes/' + pid).getcode()
urls.append('https://www.bbc.co.uk/programmes/' + pid)
except urllib.request.HTTPError as e:
log.critical('Website inaccessible ({})'.format(e))
sys.exit()
adddir = ''
if not args.series:
log.info('Found matching websites: {}'.format(urls))
imgurls, names, error = webparser.images(urls, args.res, args.nonames)
log.info('Adjusted resolution image urls: {}'.format(imgurls))
if args.nonames == 'False' and error == False:
log.info('Found matching episode names: {}'.format(names))
elif error:
log.error('Skipped fetching episode names due to error ({}).'.format(error))
names = None
else:
log.info('Skipped fetching episode names')
names = None
if args.series:
adddir = '/' + names[0][1] + '/'
log.info('Added {} directory to file destination'.format(adddir))
if downloader.imgdown(imgurls, args.dest, names, adddir):
log.info('Success!')