Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@avyfain
Created October 28, 2016 01:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avyfain/0fbd34232ece3ec2c38cc7083ce4198f to your computer and use it in GitHub Desktop.
Save avyfain/0fbd34232ece3ec2c38cc7083ce4198f to your computer and use it in GitHub Desktop.
Scrape econtalk episodes, and speed them up!
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