Skip to content

Instantly share code, notes, and snippets.

@eumel8
Last active December 2, 2018 23:09
Show Gist options
  • Save eumel8/6711676fc11322d9480f00995d8b17a3 to your computer and use it in GitHub Desktop.
Save eumel8/6711676fc11322d9480f00995d8b17a3 to your computer and use it in GitHub Desktop.
Create Youtube Playlist montly Top 50 Music
key: "AIzaSyDk4sBCGt5eg9jkRXfk6orwMV_2GXY_Abc"
token: "ya29.GltmBk3-OaRPxPwj7hsMr2Nkz60noRq9PsRVTxFFgU5n_if5bzfl6WkAJbVaCI3lb7aVOefltmUeL4QS50enjW3r3-tb_QgiOikD07bpphDQbTFCUiD4lLRCtlQc"
---
- 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&regionCode=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 }}"
{
"snippet": {
"playlistId": "{{ playlistid }}",
"resourceId": {
"kind": "youtube#video",
"videoId": "{{ item }}"
}
}
}
{
"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