Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sesh
Created March 9, 2017 04:10
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 sesh/6b8d52046455aba6ec8d8dd3edd81f3d to your computer and use it in GitHub Desktop.
Save sesh/6b8d52046455aba6ec8d8dd3edd81f3d to your computer and use it in GitHub Desktop.
Download audio files from youtube links in a reddit thread
#!/usr/bin/env python3
"""
Download audio files from youtube links in a reddit thread.
Works a treat on /r/music/
Usage:
./TUNE.py <url>
Requirements: youtube-dl, beautifulsoup4, python>=3.5
"""
import sys
import requests
from bs4 import BeautifulSoup
from subprocess import run
YOUTUBE_URLS = ['youtube.com', 'youtu.be']
def tunes(url):
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
commentarea = soup.find_all(attrs={'class': 'commentarea'})
if len(commentarea) == 1:
comments = commentarea[0]
links = comments.find_all('a')
for l in links:
href = l.attrs.get('href')
if href and any([x in href for x in YOUTUBE_URLS]):
print('Downloading:', href)
run(['youtube-dl', '-f', 'm4a', href])
else:
print("commentarea not found, you've probably been rate limited (wait 2 seconds and try again)")
print("Status code:", response.content)
if __name__ == '__main__':
url = sys.argv[-1]
if url.startswith('http'):
tunes(url)
else:
print("Usage:")
print(" ./TUNE.py <url>")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment