-
-
Save eldorplus/d6206d0eaf949ab0b89997d8092f8d87 to your computer and use it in GitHub Desktop.
Convert YouTube channel videos to M3U playlist
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import urllib\n", | |
"import json" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 59, | |
"metadata": { | |
"collapsed": false, | |
"run_control": { | |
"read_only": false | |
} | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"https://www.googleapis.com/youtube/v3/search?key=KEYKEYKEY&channelId=UCM9KEEuzacwVlkt9JfJad7g&type=video&part=snippet,id&order=date&maxResults=50\n" | |
] | |
} | |
], | |
"source": [ | |
"# Build request url\n", | |
"api_url = 'https://www.googleapis.com/youtube/v3/search?key=%s&channelId=%s'\\\n", | |
" '&type=video&part=snippet,id&order=date&maxResults=50'\n", | |
"\n", | |
"key = 'KEYKEYKEY'\n", | |
"channelId = 'UCM9KEEuzacwVlkt9JfJad7g'\n", | |
"\n", | |
"request_url = (api_url % (key, channelId))\n", | |
"print(request_url)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 53, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"('youtube.json', <http.client.HTTPMessage at 0xafcef74c>)" | |
] | |
}, | |
"execution_count": 53, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Save json from youtube\n", | |
"urllib.request.urlretrieve(request_url, \"youtube.json\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 54, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# Parse json\n", | |
"with open('youtube.json', encoding='utf-8') as json_file:\n", | |
" data = json.load(json_file)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 55, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'etag': '\"m2yskBQFythfE4irbTIeOgYYfBU/sm2jcJhqRoeVad-Z6Y7cDPwINhM\"',\n", | |
" 'id': {'kind': 'youtube#video', 'videoId': 'xcKPyEy5quE'},\n", | |
" 'kind': 'youtube#searchResult',\n", | |
" 'snippet': {'channelId': 'UCM9KEEuzacwVlkt9JfJad7g',\n", | |
" 'channelTitle': 'Chill Nation',\n", | |
" 'description': \"Stream 'Krewella - Parachute' • http://open.spotify.com/album/2Ar8jEA2FlEofQbzLZAcXz Follow us on Spotify • http://bit.ly/allchillnation ♫ Support Chill Nation ...\",\n", | |
" 'liveBroadcastContent': 'none',\n", | |
" 'publishedAt': '2017-06-08T12:00:05.000Z',\n", | |
" 'thumbnails': {'default': {'height': 90,\n", | |
" 'url': 'https://i.ytimg.com/vi/xcKPyEy5quE/default.jpg',\n", | |
" 'width': 120},\n", | |
" 'high': {'height': 360,\n", | |
" 'url': 'https://i.ytimg.com/vi/xcKPyEy5quE/hqdefault.jpg',\n", | |
" 'width': 480},\n", | |
" 'medium': {'height': 180,\n", | |
" 'url': 'https://i.ytimg.com/vi/xcKPyEy5quE/mqdefault.jpg',\n", | |
" 'width': 320}},\n", | |
" 'title': 'Krewella - Parachute'}}" | |
] | |
}, | |
"execution_count": 55, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"data['items'][0]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"M3U item format:\n", | |
" #EXTINF:{track len in seconds},{title}\n", | |
" {url to track}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 57, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"#EXTM3U\r\n", | |
"#EXTINF:0,Krewella - Parachute\r\n", | |
"http://www.youtube.com/watch?v=xcKPyEy5quE\r\n", | |
"#EXTINF:0,Shallou - Heights\r\n", | |
"http://www.youtube.com/watch?v=gbJC4KXr0go\r\n", | |
"#EXTINF:0,Lostboycrow - Real Name\r\n", | |
"http://www.youtube.com/watch?v=HKybVaHuW0A\r\n", | |
"#EXTINF:0,Jasmine Thompson - Someone's Somebody\r\n", | |
"http://www.youtube.com/watch?v=rIh8IhCC8HY\r\n", | |
"#EXTINF:0,Zedd & Alessia Cara - Stay (Petit Biscuit Remix)\r\n", | |
"http://www.youtube.com/watch?v=IdiHNIpWlSI\r\n", | |
"#EXTINF:0,Terror Jr - Caramel\r\n", | |
"http://www.youtube.com/watch?v=vb-iiiU2fXE\r\n", | |
"#EXTINF:0,Astrid S - Party's Over\r\n", | |
"http://www.youtube.com/watch?v=DiIBG4Oh0Ac\r\n", | |
"#EXTINF:0,Chelsea Cutler - Sixteen\r\n", | |
"http://www.youtube.com/watch?v=ly3vHcM3Exg\r\n", | |
"#EXTINF:0,Astrid S - Breathe (Lauv Remix)\r\n", | |
"http://www.youtube.com/watch?v=xFMsSpP5Cnw\r\n", | |
"#EXTINF:0,Galantis - Hunter\r\n", | |
"http://www.youtube.com/watch?v=Yotjv4h0Hgs\r\n", | |
"#EXTINF:0,Miley Cyrus - Malibu (Kiso & Yvette Remix)\r\n", | |
"http://www.youtube.com/watch?v=Jehbbi6JEpg\r\n", | |
"#EXTINF:0,Lauv - I Like Me Better\r\n", | |
"http://www.youtube.com/watch?v=VBFiq2iCEvg\r\n", | |
"#EXTINF:0,Severo & Ellusive - Oceans Away (feat. Loé)\r\n", | |
"http://www.youtube.com/watch?v=AYrN3xUcGXo\r\n", | |
"#EXTINF:0,MagnusTheMagnus - Keep On Lovin' (feat. Seinabo Sey)\r\n", | |
"http://www.youtube.com/watch?v=CxsQlOvjB78\r\n", | |
"#EXTINF:0,LissA - Not\r\n", | |
"http://www.youtube.com/watch?v=tuOuJguHALw\r\n", | |
"#EXTINF:0,Maroon 5 - Cold ft.Future (R3hab & Khrebto Remix)\r\n", | |
"http://www.youtube.com/watch?v=5UqJjcozCcc\r\n", | |
"#EXTINF:0,Haux - Youth\r\n", | |
"http://www.youtube.com/watch?v=G0g_lx4XmrI\r\n", | |
"#EXTINF:0,Jubel - The Break Up Song\r\n", | |
"http://www.youtube.com/watch?v=jtOlrgekpsQ\r\n", | |
"#EXTINF:0,Shallou - Truth\r\n", | |
"http://www.youtube.com/watch?v=NGhyTYZ3SXo\r\n", | |
"#EXTINF:0,Shawn Mendes - There's Nothing Holding Me Back (NOTD Remix)\r\n", | |
"http://www.youtube.com/watch?v=aWCZcevSxEU\r\n", | |
"#EXTINF:0,Dillistone & LILI N - Don't Know Why\r\n", | |
"http://www.youtube.com/watch?v=Bs7yv3G2bSo\r\n", | |
"#EXTINF:0,Steve Kroeger - Coastline (feat. Skye Holland)\r\n", | |
"http://www.youtube.com/watch?v=U-VVCFn0AOU\r\n", | |
"#EXTINF:0,Tarro & PLVTINUM Champagne & Sunshine (Ellusive Remix)\r\n", | |
"http://www.youtube.com/watch?v=XJjwUTPJA3s\r\n", | |
"#EXTINF:0,Loote - High Without Your Love\r\n", | |
"http://www.youtube.com/watch?v=Qte_hRoZ5j8\r\n", | |
"#EXTINF:0,Holmsey - Wilder\r\n", | |
"http://www.youtube.com/watch?v=soAODZ_gJFI\r\n", | |
"#EXTINF:0,LIOHN & Filip - Bonfire\r\n", | |
"http://www.youtube.com/watch?v=sM-l0FYJPwA\r\n", | |
"#EXTINF:0,Gill Chang - Story (feat. Cimo Fränkel)\r\n", | |
"http://www.youtube.com/watch?v=cvke4zrRnH0\r\n", | |
"#EXTINF:0,ODESZA - Line of Sight (feat. WYNNE & Mansionair)\r\n", | |
"http://www.youtube.com/watch?v=cPJIJrJOv7o\r\n", | |
"#EXTINF:0,The Chainsmokers & Coldplay - Something Just Like This (R3hab Remix)\r\n", | |
"http://www.youtube.com/watch?v=YJYrobBKruA\r\n", | |
"#EXTINF:0,Emmit Fenn - 1995\r\n", | |
"http://www.youtube.com/watch?v=vFADcQ6pLGE\r\n", | |
"#EXTINF:0,Pham - Talk To Me (feat. Anuka)\r\n", | |
"http://www.youtube.com/watch?v=CRApnKLiPCs\r\n", | |
"#EXTINF:0,Shallou - Fictions\r\n", | |
"http://www.youtube.com/watch?v=ljuYP6a5EgI\r\n", | |
"#EXTINF:0,Linkin Park - Heavy ft. Kiiara (Nicky Romero Remix)\r\n", | |
"http://www.youtube.com/watch?v=XjjY-slC_nI\r\n", | |
"#EXTINF:0,Clean Bandit - Symphony ft. Zara Larsson (Steve Reece Remix)\r\n", | |
"http://www.youtube.com/watch?v=YTAZlaugKvg\r\n", | |
"#EXTINF:0,Kiiara - Whippin (feat. Felix Snow)\r\n", | |
"http://www.youtube.com/watch?v=XGz0SIdj3GA\r\n", | |
"#EXTINF:0,Whethan - Savage (NOTD Remix)\r\n", | |
"http://www.youtube.com/watch?v=6yFg_SH16ys\r\n", | |
"#EXTINF:0,XYLØ - Get Closer (Joe Mason Remix)\r\n", | |
"http://www.youtube.com/watch?v=BfVKMB25lc4\r\n", | |
"#EXTINF:0,Mansionz - Gorgeous\r\n", | |
"http://www.youtube.com/watch?v=yDSt9hRniRg\r\n", | |
"#EXTINF:0,Galantis - Rich Boy (Said The Sky Remix)\r\n", | |
"http://www.youtube.com/watch?v=rObfQcv2F8s\r\n", | |
"#EXTINF:0,Lukas Graham - You're Not There (Grey Remix)\r\n", | |
"http://www.youtube.com/watch?v=tPFuEwEdltk\r\n", | |
"#EXTINF:0,The Chainsmokers - Bloodstream\r\n", | |
"http://www.youtube.com/watch?v=5JE-JrPFdik\r\n", | |
"#EXTINF:0,Lauv - The Other (Matisse & Sadko Remix)\r\n", | |
"http://www.youtube.com/watch?v=CSYg4CfvWyY\r\n", | |
"#EXTINF:0,Kasbo - Found You (feat. Chelsea Cutler)\r\n", | |
"http://www.youtube.com/watch?v=1KGXZVK3AEM\r\n", | |
"#EXTINF:0,Y.V.E. 48 - Nothing To Say (feat. LissA)\r\n", | |
"http://www.youtube.com/watch?v=MqK0wZrvd5Q\r\n", | |
"#EXTINF:0,Maria Lynn & Toniia - Diamonds\r\n", | |
"http://www.youtube.com/watch?v=UKnACrpPleQ\r\n", | |
"#EXTINF:0,Kiso & Loé - Circles\r\n", | |
"http://www.youtube.com/watch?v=Hzox8YSOdKg\r\n", | |
"#EXTINF:0,Jason Derulo, Nicki Minaj & Ty Dolla $ign - Swalla (Vince Remix)\r\n", | |
"http://www.youtube.com/watch?v=FFFiFGzi1IQ\r\n", | |
"#EXTINF:0,Lucian - Close to You (feat. Jasmine Sokko)\r\n", | |
"http://www.youtube.com/watch?v=Za1WNHC1qSo\r\n", | |
"#EXTINF:0,Witt Lowry - Let Me Know (feat. Tori Solkowski)\r\n", | |
"http://www.youtube.com/watch?v=mqB48tswoOo\r\n", | |
"#EXTINF:0,Mura Masa & Charli XCX - 1 Night\r\n", | |
"http://www.youtube.com/watch?v=0J523eTGrLc\n" | |
] | |
} | |
], | |
"source": [ | |
"# Compile playlist\n", | |
"str_list = []\n", | |
"str_list.append('#EXTM3U')\n", | |
"for item in data['items']:\n", | |
" title = item['snippet']['title']\n", | |
" videoId = item['id']['videoId']\n", | |
" str_list.append('#EXTINF:0,%s' % (title))\n", | |
" str_list.append('http://www.youtube.com/watch?v=%s' % (videoId))\n", | |
"\n", | |
"print('\\r\\n'.join(str_list))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 58, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# Save to file\n", | |
"with open('youtube.m3u', 'w', encoding='utf-8') as playlist_file:\n", | |
" playlist_file.write('\\r\\n'.join(str_list))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.4.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment