Created
October 28, 2016 01:55
-
-
Save avyfain/0fbd34232ece3ec2c38cc7083ce4198f to your computer and use it in GitHub Desktop.
Scrape econtalk episodes, and speed them up!
This file contains hidden or 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
import os | |
import re | |
import shlex | |
import requests | |
import subprocess | |
from multiprocessing import Pool | |
from bs4 import BeautifulSoup | |
reg = re.compile("http:\/\/files.*.mp3") | |
def get_mp3_info(url): | |
req = requests.get(url.strip()) | |
data = req.text | |
soup = BeautifulSoup(data, "html.parser") | |
href = soup.find(href=reg).get('href') | |
title = soup.title.string | |
title = title.split('|')[0].strip() | |
return href, title | |
def download_file(url): | |
local_filename = url.split('/')[-1] | |
r = requests.get(url, stream=True) | |
with open(local_filename, 'wb') as f: | |
for chunk in r.iter_content(chunk_size=1024): | |
if chunk: | |
f.write(chunk) | |
return local_filename | |
def speedup(filename, title): | |
with open(os.devnull, 'w') as devnull: | |
command_line = "ffmpeg -i {} -filter:a \"atempo=1.25\" -vn \"{}.mp3\"".format(filename, title) | |
args = shlex.split(command_line) | |
subprocess.call(args, stdout=devnull, stderr=devnull) | |
def process(url): | |
print("Finding file for {}".format(url)) | |
loc, title = get_mp3_info(url) | |
filename = download_file(loc) | |
print("Shortening {}".format(title)) | |
speedup(filename, title) | |
os.remove(filename) | |
def main(): | |
pool = Pool(10) | |
with open('econtalk.txt') as f: | |
episodes = f.readlines() | |
pool.map(process, episodes) | |
pool.close() | |
pool.join() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment