- Goto https://console.cloud.google.com and create a new project
- Goto project and create an API service
- Choose Youtube API Data v3 from the inventory marketplace
- Create API key credentials
- Create OAuth 2.0 Client-ID (type other)
- Remark: For access public API data only the API key is require, for read/write access to your account you need the OAuth 2.0 thing
- Browse to the OAuth 2.0 project url: https://accounts.google.com/o/oauth2/auth?client_id=[my_client_id]&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https://www.googleapis.com/auth/youtube&response_type=code
- You'll get a code for your Youtube account. Together with the OAuth 2.0 Client-ID and Client-Secret you can make a curl request to get a token
- curl --request POST --data "code=[my_code]&client_id=[my_client_id]&client_secret=[my_client_secret]&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code" https://accounts.google.com/o/oauth2/token
- Put the API Key and the OAuth Token into the keys.yml. The token is valid for one hour
- Create the other files below, in list.yml you can change regionCode=DE to your region if required
- Run (on a monthly base): "ansible-playbook list.yml [email protected]"
- Remark: In an unspecific timeframe are only 10 playlists allowed to create (https://issuetracker.google.com/issues/79222309)
Last active
December 2, 2018 23:09
-
-
Save eumel8/6711676fc11322d9480f00995d8b17a3 to your computer and use it in GitHub Desktop.
Create Youtube Playlist montly Top 50 Music
This file contains 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
key: "AIzaSyDk4sBCGt5eg9jkRXfk6orwMV_2GXY_Abc" | |
token: "ya29.GltmBk3-OaRPxPwj7hsMr2Nkz60noRq9PsRVTxFFgU5n_if5bzfl6WkAJbVaCI3lb7aVOefltmUeL4QS50enjW3r3-tb_QgiOikD07bpphDQbTFCUiD4lLRCtlQc" |
This file contains 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
--- | |
- hosts: localhost | |
gather_facts: no | |
connection: local | |
tasks: | |
- name: Send request to API (list popular videos or all time high) | |
uri: | |
url: "https://content.googleapis.com/youtube/v3/videos?regionCode=DE&chart=mostPopular&part=snippet,contentDetails,statistics&maxResults=50&videoCategoryId=10&key={{ key }}" | |
# url: "https://content.googleapis.com/youtube/v3/search?part=snippet&q=song&type=video&order=viewCount&maxResults=50®ionCode=de&key={{ key }}" | |
method: GET | |
return_content: yes | |
headers: | |
Content-Type: "application/json" | |
register: videolist | |
- name: Set fact toplist and datelist | |
set_fact: | |
toplist: "{{ videolist.json|json_query('sort_by(items, &to_number(statistics.viewCount))[*].id') }}" | |
# toplist: "{{ videolist.json|json_query('items[].id.videoId') }}" | |
datelist: "{{ lookup('pipe','date +%m_%Y -d \"last month\"') }}" | |
- name: Send request to API (create playlist) | |
uri: | |
url: "https://www.googleapis.com/youtube/v3/playlists?part=snippet%2Cstatus&key={{ key }}" | |
method: POST | |
body_format: raw | |
return_content: yes | |
headers: | |
Content-Type: "application/json" | |
Authorization: "Bearer {{ token }}" | |
body: "{{ lookup('template', 'playlist_create.json.j2')|to_json }}" | |
register: create_listresult | |
- name: Set fact playlistid | |
set_fact: | |
playlistid: "{{ create_listresult.json|json_query('id') }}" | |
- name: Send request to API (insert playlist) | |
uri: | |
url: "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&key={{ key }}" | |
method: POST | |
body_format: raw | |
return_content: yes | |
headers: | |
Content-Type: "application/json" | |
Authorization: "Bearer {{ token }}" | |
body: "{{ lookup('template', 'playlist.json.j2')|to_json }}" | |
register: listresult | |
with_items: | |
- "{{ toplist }}" |
This file contains 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
{ | |
"snippet": { | |
"playlistId": "{{ playlistid }}", | |
"resourceId": { | |
"kind": "youtube#video", | |
"videoId": "{{ item }}" | |
} | |
} | |
} |
This file contains 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
{ | |
"snippet": { | |
"title": "Youtube Music TOP50 Region DE {{ datelist }}" | |
}, | |
"status": { | |
"privacyStatus": "public" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment