Skip to content

Instantly share code, notes, and snippets.

@volpefoxx
Last active September 21, 2023 10:05
Show Gist options
  • Save volpefoxx/1d8a6832e588f3ffa6c9c680f9dada38 to your computer and use it in GitHub Desktop.
Save volpefoxx/1d8a6832e588f3ffa6c9c680f9dada38 to your computer and use it in GitHub Desktop.
Gets a user's Discogs collection and places all videos from release pages into a YouTube playlist
import requests
import json
import re
from more_itertools import unique_everseen
import time
from oauth2client import client, GOOGLE_TOKEN_URI, GOOGLE_REVOKE_URI
import httplib2
# INPUTS
# get your Discogs API token from https://www.discogs.com/settings/developers
username = "YOUR_DISCOGS_USERNAME"
token = "YOUR_DISCOGS_API_TOKEN"
# must be larger than your discogs collection
items_per_page = 1000
# choose a unique app name for the user agent
app_name = "YOUR_APP_NAME"
# playlist ID can be found in your playlist URL
playlist_id = "YOUR_PLAYLIST_ID"
# this info can be obtained from the client_secrets file from the Google API console
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
refresh_token = "YOUR_REFRESH_TOKEN"
path = "https://api.discogs.com"
headers = {"User-Agent": "{app_name}/1.0".format(app_name=app_name)}
sleep = 2
collection_path = "/users/{username}/collection/folders/0/releases".format(username=username)
collection_url = "{path}{collection_path}?token={token}&per_page={per_page}".format(
path=path,
collection_path=collection_path,
token=token,
per_page=items_per_page)
releases = requests.get(collection_url).json()["releases"]
with open("full_collection.json", "w") as fp:
json.dump(releases, fp)
release_ids = [release["id"] for release in releases]
full_collection_details = []
video_urls = []
for release_id in release_ids:
print("Getting videos for {id}".format(id=release_id))
release_url = "{path}/releases/{id}?token={token}".format(
path=path,
id=release_id,
token=token)
try:
release_json = requests.get(release_url).json()
release_videos = release_json["videos"]
release_urls = [video["uri"] for video in release_videos]
video_urls += release_urls
except KeyError:
None
time.sleep(sleep)
# serialise full collection info
with open("full_collection_details.json", "w") as fp:
json.dump(full_collection_details, fp)
def get_id(url):
if "youtube" in url:
if "attribution" in url:
try:
video_id = re.search("%3D(.*)\%26", url).group(1)
except Exception:
print(url)
video_id = ""
else:
try:
video_id = re.search("(\?|\&)(v|ci)=(.*)", url).group(3)
except Exception:
print(url)
video_id = ""
else:
try:
video_id = re.search("\.be\/(.*)", url).group(1)
except Exception:
print(url)
video_id = ""
return video_id
ids = [get_id(url) for url in video_urls]
ids = list(filter(lambda x: len(x) > 0, ids))
ids = list(unique_everseen(ids))
credentials = client.OAuth2Credentials(
None, client_id, client_secret, refresh_token, None, GOOGLE_TOKEN_URI,
None, revoke_uri=GOOGLE_REVOKE_URI)
credentials.refresh(httplib2.Http())
access_token = credentials.get_access_token()[0]
path = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&access_token={access_token}".format(access_token=access_token)
for video_id in ids:
payload = {
"snippet": {
"playlistId": playlist_id,
"resourceId": {
"videoId": video_id,
"kind": "youtube#video"
}
}
}
request = requests.post(
path,
data=json.dumps(payload),
headers={'Content-Type': 'application/json'})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment