-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·96 lines (87 loc) · 3.07 KB
/
main.py
File metadata and controls
executable file
·96 lines (87 loc) · 3.07 KB
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
#!/usr/bin/env python
import coloredlogs, logging, logging.config
from datetime import datetime
from functools import partial
import click
from youtube_downloader_cli.downloader import YTDownloader
from youtube_downloader_cli.models import setup_database
from youtube_downloader_cli.config import DATABASE_FILE
# Refer to
# 1. https://stackoverflow.com/a/7507842/1677041
# 2. https://stackoverflow.com/a/49400680/1677041
# 3. https://docs.python.org/2/library/logging.config.html
LOGGING_CONFIG = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'standard': {
'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
},
'colored': {
'()': 'coloredlogs.ColoredFormatter',
'format': "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
'datefmt': '%H:%M:%S',
}
},
'handlers': {
'default': {
'level': 'DEBUG' if __debug__ else 'INFO',
'formatter': 'standard',
'class': 'logging.StreamHandler',
'stream': 'ext://sys.stdout', # Default is stderr
},
'console': {
'level': 'DEBUG' if __debug__ else 'INFO',
'class': 'logging.StreamHandler',
'formatter': 'colored',
'stream': 'ext://sys.stdout'
},
'file': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'formatter': 'standard',
'filename': 'main.log',
'maxBytes': 1024 * 1024,
'backupCount': 10
},
},
'loggers': {
'': { # root logger
'handlers': ['console', 'file'],
'level': 'DEBUG',
'propagate': False
},
'__main__': { # if __name__ == '__main__'
'handlers': ['console', 'file'],
'level': 'DEBUG',
'propagate': False
},
'youtube_downloader_cli': {
'handlers': ['console', 'file'],
'level': 'DEBUG',
'propagate': False
},
}
}
logging.config.dictConfig(LOGGING_CONFIG)
logger = logging.getLogger(__name__)
click.option = partial(click.option, show_default=True)
@click.command()
@click.option('--download', '-d', default=False, is_flag=True, type=click.BOOL,
help='Whether download video or not.')
@click.argument('urls', type=click.STRING, nargs=-1)
def main(download, urls):
"""This tool is used to parse and download the youtube videos."""
logger.info('Debug is %s', 'on' if __debug__ else 'off')
setup_database(DATABASE_FILE)
def filter_video(video):
'Filter the uploaded date less than last 3 years.'
upload_date = datetime.strptime(video.upload_date, '%Y%m%d')
date_delta = upload_date - datetime.now()
return date_delta.days <= 365 * 3
for url in urls:
downloader = YTDownloader(download=download, filter_func=filter_video)
videos = downloader.parse(url)
[logger.debug('Result: %s' % video) for video in videos]
if __name__ == '__main__':
main()