-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
128 lines (115 loc) · 4.35 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# -*- coding: utf-8 -*-
# Sample Python code for youtube.playlistItems.insert
# See instructions for running these code samples locally:
# https://developers.google.com/explorer-help/guides/code_samples#python
import os
import os.path
from time import sleep
import configparser
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
scopes = ["https://www.googleapis.com/auth/youtube.force-ssl"]
config = configparser.ConfigParser()
def get_client_secrets_file():
config.read('config.ini')
return config["MAIN"]["CLIENT_SECRET_FILE"]
def get_dest_playlist():
config.read("config.ini")
return config["MAIN"]["DEST_PLAYLIST"].replace("https://www.youtube.com/playlist?list=", "")
def main():
# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
api_service_name = "youtube"
api_version = "v3"
# How to create cred https://stackoverflow.com/a/52222827/2138792
client_secrets_file = get_client_secrets_file()
destination_play_list = get_dest_playlist()
source_play_list = 'LM' # Replace if not using liked music
# Get credentials and create an API client
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
client_secrets_file, scopes,redirect_uri='urn:ietf:wg:oauth:2.0:oob')
# credentials = flow.run_local_server()
auth_url, _ = flow.authorization_url(prompt='consent')
print(auth_url)
code = input('Enter the authorization code: ')
flow.fetch_token(code=code)
credentials=flow.credentials
youtube = googleapiclient.discovery.build(
api_service_name, api_version, credentials=credentials)
def getplaylistvids(pid):
request = youtube.playlistItems().list(
part='contentDetails',
playlistId="LM"
)
response = request.execute()
if 'pageInfo' in response:
pageInfo = response['pageInfo']
if 'totalResults' in pageInfo:
maxResults = pageInfo['totalResults']
request = youtube.playlistItems().list(
part='contentDetails',
playlistId=pid,
maxResults=50 if maxResults > 50 else maxResults
)
response = request.execute()
print(response)
vids = []
if 'items' in response:
items = response['items']
for item in items:
if "contentDetails" in item:
vids.append(item['contentDetails']['videoId'])
while len(vids) < maxResults:
sleep(1)
if 'nextPageToken' in response:
request = youtube.playlistItems().list(
part='contentDetails',
playlistId=pid,
maxResults=50,
pageToken= response['nextPageToken'],
)
else:
request = youtube.playlistItems().list(
part='contentDetails',
playlistId=pid,
maxResults=50,
)
response = request.execute()
print(response)
if 'items' in response:
items = response['items']
for item in items:
if "contentDetails" in item:
vids.append(item['contentDetails']['videoId'])
if 'nextPageToken' in response:
nextPageToken = response['nextPageToken']
else:
break
return vids
source_vids = getplaylistvids(source_play_list)
destination_vids = getplaylistvids(destination_play_list)
vids = set(source_vids) - set(destination_vids)
print(vids)
for vid in vids:
try:
request = youtube.playlistItems().insert(
part="snippet",
body={
"snippet": {
"playlistId": destination_play_list,
"resourceId": {
"videoId": vid,
"kind": "youtube#video"
}
}
}
)
response = request.execute()
print(response)
sleep(1)
except Exception as e:
print(e)
if __name__ == "__main__":
main()