Skip to content

Instantly share code, notes, and snippets.

@wernight
Last active August 29, 2015 14:16
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 wernight/a9c106462bb997b2a052 to your computer and use it in GitHub Desktop.
Save wernight/a9c106462bb997b2a052 to your computer and use it in GitHub Desktop.
Download and upload to Google Music Regionaljournal Zuerich Schaffhausen podcasts.
#!/usr/bin/env python
'''
Setup:
$ pip install gmusicapi==4.0.0 requests==2.5.1
$ mkdir podcasts
$ python podcast.py
'''
import requests
import bs4
import os.path
import sys
from gmusicapi import Musicmanager
DOWNLOAD_DIR = 'podcasts'
PODCAST_URL = 'http://www.srf.ch/sendungen/regionaljournal-zuerich-schaffhausen'
def retrieve_audio_urls():
print('Retrieving current list of podcasts...')
# Retrieve a list of all downloads.
r = requests.get(PODCAST_URL)
assert r.ok
soup = bs4.BeautifulSoup(r.content)
mp3_urls = [node['href'] for node in soup.find_all('a', class_='download')]
if not mp3_urls:
raise Exception('No podcast found on {}.'.format(PODCAST_URL))
print(' Found {} URLs.'.format(len(mp3_urls)))
print('')
return mp3_urls
def download_missing_files(urls, directory):
print('Downloading new podcasts...'.format(len(urls)))
# Download those not already downloaded.
files = []
for url in urls:
local_filename = os.path.join(directory, os.path.basename(url))
# If not already downloaded,
if not os.path.exists(local_filename):
# Download
print(' Downloading {} --> {}'.format(url, local_filename))
r = requests.get(url, stream=True)
assert r.ok
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
f.flush()
# Mark to upload
files.append(local_filename)
print('')
return files
def login_music_manager():
# Login (first time will require to open a page and give a code).
mm = Musicmanager()
if not mm.login():
mm.perform_oauth()
if not mm.login():
raise Exception('Could not authenticate.')
return mm
if __name__ == '__main__':
# Login first so that it asks credentials if necessary.
mm = login_music_manager()
# Retrieve current list of podcasts.
mp3_urls = retrieve_audio_urls()
# Download new podcasts
downloaded_files = download_missing_files(mp3_urls, DOWNLOAD_DIR)
if not downloaded_files:
print('No new podcast to upload.')
sys.exit(0)
# Upload to Google Music
print('Uploading {} podcast files...'.format(len(downloaded_files)))
for filename in downloaded_files:
print(' Uploading {0}'.format(os.path.basename(filename)))
mm.upload(filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment