Last active
February 24, 2017 04:41
-
-
Save McSinyx/986b926522dc33547bbd5dd1361e98e5 to your computer and use it in GitHub Desktop.
VLE Broadcast Downloader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# VOA Learning English Broadcast Downloader | |
# Copyright (C) 2016,2017 Raphael McSinyx | |
# | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
import os | |
from argparse import ArgumentParser | |
from datetime import datetime | |
from urllib.request import urlretrieve | |
def progress(block_number, read_size, total_size): | |
print('Downloading... {}%'.format( | |
block_number * read_size * 100 // total_size), | |
end='\r' | |
) | |
def download(release_date, directory, bitrate): | |
"""Download VOA Learning English Broadcast on release_date with | |
specified bitrate to the folder directory. | |
Returns the path to the downloaded broadcast. | |
""" | |
broadcast = "{}-003000-VLE122-program{}.mp3".format( | |
release_date, | |
{64: '', 128: '_hq', 256: '_original'}[bitrate] | |
) | |
url = 'http://av.voanews.com/clips/VLE/{}/{}/{}/{}'.format( | |
release_date[:4], | |
release_date[4:6], | |
release_date[6:8], | |
broadcast | |
) | |
print(url) | |
os.makedirs(directory, exist_ok=True) | |
filename = os.path.join(directory, broadcast) | |
print('Saving to:', filename) | |
urlretrieve(url, filename, progress) | |
print('Downloading... Done') | |
print(filename, 'saved') | |
if __name__ == '__main__': | |
parser = ArgumentParser(description='VOA Learning English Broadcast\ | |
Downloader.') | |
parser.add_argument('-r', '--release-date', | |
default=datetime.utcnow().strftime("%Y%m%d"), | |
required=False, | |
help='release date of the broadcast in ISO 8601 basic\ | |
format (default to UTC: %(default)s)', | |
metavar='DAY') | |
parser.add_argument('-d', '--directory', default=os.getcwd(), required=False, | |
help='directory to store the broadcast\ | |
(default: %(default)s)') | |
parser.add_argument('-b', '--bitrate', default=256, type=int, | |
choices=[64, 128, 256], required=False, | |
help='bitrate (kbit/s) of the broadcast to download\ | |
(default: %(default)s)') | |
args = parser.parse_args() | |
download(args.release_date, args.directory, args.bitrate) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment