|
@@ -5,6 +5,7 @@ from django.db import models
|
|
from googleapiclient.discovery import build
|
|
from googleapiclient.discovery import build
|
|
import googleapiclient.errors
|
|
import googleapiclient.errors
|
|
from django.db.models import Q, Sum
|
|
from django.db.models import Q, Sum
|
|
|
|
+from ..general.utils.misc import print_
|
|
|
|
|
|
|
|
|
|
def get_message_from_httperror(e):
|
|
def get_message_from_httperror(e):
|
|
@@ -58,10 +59,10 @@ class PlaylistManager(models.Manager):
|
|
|
|
|
|
pl_response = pl_request.execute()
|
|
pl_response = pl_request.execute()
|
|
|
|
|
|
- print(pl_response)
|
|
+ print_(pl_response)
|
|
|
|
|
|
if pl_response['pageInfo']['totalResults'] == 0:
|
|
if pl_response['pageInfo']['totalResults'] == 0:
|
|
- print("Looks like do not have a channel on youtube. Create one to import all of your playlists. Retry?")
|
|
+ print_("Looks like do not have a channel on youtube. Create one to import all of your playlists. Retry?")
|
|
return -1
|
|
return -1
|
|
else:
|
|
else:
|
|
user_profile.yt_channel_id = pl_response['items'][0]['id']
|
|
user_profile.yt_channel_id = pl_response['items'][0]['id']
|
|
@@ -97,7 +98,7 @@ class PlaylistManager(models.Manager):
|
|
maxResults=50
|
|
maxResults=50
|
|
)
|
|
)
|
|
else:
|
|
else:
|
|
- print("GETTING ALL USER AUTH PLAYLISTS")
|
|
+ print_("GETTING ALL USER AUTH PLAYLISTS")
|
|
pl_request = youtube.playlists().list(
|
|
pl_request = youtube.playlists().list(
|
|
part='contentDetails, snippet, id, player, status',
|
|
part='contentDetails, snippet, id, player, status',
|
|
mine=True,
|
|
mine=True,
|
|
@@ -108,15 +109,15 @@ class PlaylistManager(models.Manager):
|
|
try:
|
|
try:
|
|
pl_response = pl_request.execute()
|
|
pl_response = pl_request.execute()
|
|
except googleapiclient.errors.HttpError as e:
|
|
except googleapiclient.errors.HttpError as e:
|
|
- print("YouTube channel not found if mine=True")
|
|
+ print_("YouTube channel not found if mine=True")
|
|
- print("YouTube playlist not found if id=playlist_id")
|
|
+ print_("YouTube playlist not found if id=playlist_id")
|
|
result["status"] = -1
|
|
result["status"] = -1
|
|
result["error_message"] = get_message_from_httperror(e)
|
|
result["error_message"] = get_message_from_httperror(e)
|
|
return result
|
|
return result
|
|
|
|
|
|
- print(pl_response)
|
|
+ print_(pl_response)
|
|
if pl_response["pageInfo"]["totalResults"] == 0:
|
|
if pl_response["pageInfo"]["totalResults"] == 0:
|
|
- print("No playlists created yet on youtube.")
|
|
+ print_("No playlists created yet on youtube.")
|
|
result["status"] = -2
|
|
result["status"] = -2
|
|
return result
|
|
return result
|
|
|
|
|
|
@@ -145,7 +146,7 @@ class PlaylistManager(models.Manager):
|
|
|
|
|
|
if user.playlists.filter(Q(playlist_id=playlist_id) & Q(is_in_db=True)).exists():
|
|
if user.playlists.filter(Q(playlist_id=playlist_id) & Q(is_in_db=True)).exists():
|
|
playlist = user.playlists.get(playlist_id=playlist_id)
|
|
playlist = user.playlists.get(playlist_id=playlist_id)
|
|
- print(f"PLAYLIST {playlist.name} ({playlist_id}) ALREADY EXISTS IN DB")
|
|
+ print_(f"PLAYLIST {playlist.name} ({playlist_id}) ALREADY EXISTS IN DB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -159,7 +160,7 @@ class PlaylistManager(models.Manager):
|
|
result["status"] = -3
|
|
result["status"] = -3
|
|
return result
|
|
return result
|
|
else:
|
|
else:
|
|
- print(f"CREATING {item['snippet']['title']} ({playlist_id})")
|
|
+ print_(f"CREATING {item['snippet']['title']} ({playlist_id})")
|
|
if user.playlists.filter(Q(playlist_id=playlist_id) & Q(is_in_db=False)).exists():
|
|
if user.playlists.filter(Q(playlist_id=playlist_id) & Q(is_in_db=False)).exists():
|
|
unimported_playlist = user.playlists.filter(Q(playlist_id=playlist_id) & Q(is_in_db=False)).first()
|
|
unimported_playlist = user.playlists.filter(Q(playlist_id=playlist_id) & Q(is_in_db=False)).first()
|
|
unimported_playlist.delete()
|
|
unimported_playlist.delete()
|
|
@@ -260,7 +261,7 @@ class PlaylistManager(models.Manager):
|
|
|
|
|
|
else:
|
|
else:
|
|
if playlist.playlist_items.filter(playlist_item_id=playlist_item_id).exists():
|
|
if playlist.playlist_items.filter(playlist_item_id=playlist_item_id).exists():
|
|
- print("PLAYLIST ITEM ALREADY EXISTS")
|
|
+ print_("PLAYLIST ITEM ALREADY EXISTS")
|
|
continue
|
|
continue
|
|
|
|
|
|
video = user.videos.get(video_id=video_id)
|
|
video = user.videos.get(video_id=video_id)
|
|
@@ -465,7 +466,7 @@ class PlaylistManager(models.Manager):
|
|
|
|
|
|
|
|
|
|
if playlist.last_full_scan_at + datetime.timedelta(minutes=1) < datetime.datetime.now(pytz.utc):
|
|
if playlist.last_full_scan_at + datetime.timedelta(minutes=1) < datetime.datetime.now(pytz.utc):
|
|
- print("DOING A FULL SCAN")
|
|
+ print_("DOING A FULL SCAN")
|
|
current_playlist_item_ids = [playlist_item.playlist_item_id for playlist_item in
|
|
current_playlist_item_ids = [playlist_item.playlist_item_id for playlist_item in
|
|
playlist.playlist_items.all()]
|
|
playlist.playlist_items.all()]
|
|
|
|
|
|
@@ -546,10 +547,10 @@ class PlaylistManager(models.Manager):
|
|
|
|
|
|
return [1, deleted_videos, unavailable_videos, added_videos]
|
|
return [1, deleted_videos, unavailable_videos, added_videos]
|
|
else:
|
|
else:
|
|
- print("YOU CAN DO A FULL SCAN AGAIN IN",
|
|
+ print_("YOU CAN DO A FULL SCAN AGAIN IN",
|
|
str(datetime.datetime.now(pytz.utc) - (playlist.last_full_scan_at + datetime.timedelta(minutes=1))))
|
|
str(datetime.datetime.now(pytz.utc) - (playlist.last_full_scan_at + datetime.timedelta(minutes=1))))
|
|
"""
|
|
"""
|
|
- print("DOING A SMOL SCAN")
|
|
+ print_("DOING A SMOL SCAN")
|
|
|
|
|
|
with build('youtube', 'v3', credentials=credentials) as youtube:
|
|
with build('youtube', 'v3', credentials=credentials) as youtube:
|
|
pl_request = youtube.playlists().list(
|
|
pl_request = youtube.playlists().list(
|
|
@@ -562,11 +563,11 @@ class PlaylistManager(models.Manager):
|
|
try:
|
|
try:
|
|
pl_response = pl_request.execute()
|
|
pl_response = pl_request.execute()
|
|
except googleapiclient.errors.HttpError:
|
|
except googleapiclient.errors.HttpError:
|
|
- print("YouTube channel not found if mine=True")
|
|
+ print_("YouTube channel not found if mine=True")
|
|
- print("YouTube playlist not found if id=playlist_id")
|
|
+ print_("YouTube playlist not found if id=playlist_id")
|
|
return -1
|
|
return -1
|
|
|
|
|
|
- print("PLAYLIST", pl_response)
|
|
+ print_("PLAYLIST", pl_response)
|
|
|
|
|
|
playlist_items = []
|
|
playlist_items = []
|
|
|
|
|
|
@@ -588,7 +589,7 @@ class PlaylistManager(models.Manager):
|
|
|
|
|
|
if user.playlists.filter(playlist_id=playlist_id).exists():
|
|
if user.playlists.filter(playlist_id=playlist_id).exists():
|
|
playlist = user.playlists.get(playlist_id__exact=playlist_id)
|
|
playlist = user.playlists.get(playlist_id__exact=playlist_id)
|
|
- print(f"PLAYLIST {playlist.name} ALREADY EXISTS IN DB")
|
|
+ print_(f"PLAYLIST {playlist.name} ALREADY EXISTS IN DB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -627,10 +628,10 @@ class PlaylistManager(models.Manager):
|
|
try:
|
|
try:
|
|
pl_response = pl_request.execute()
|
|
pl_response = pl_request.execute()
|
|
except googleapiclient.errors.HttpError:
|
|
except googleapiclient.errors.HttpError:
|
|
- print("Playist was deleted on YouTube")
|
|
+ print_("Playist was deleted on YouTube")
|
|
return [-1, [], [], []]
|
|
return [-1, [], [], []]
|
|
|
|
|
|
- print("ESTIMATED VIDEO IDS FROM RESPONSE", len(pl_response["items"]))
|
|
+ print_("ESTIMATED VIDEO IDS FROM RESPONSE", len(pl_response["items"]))
|
|
updated_playlist_video_count += len(pl_response["items"])
|
|
updated_playlist_video_count += len(pl_response["items"])
|
|
for item in pl_response['items']:
|
|
for item in pl_response['items']:
|
|
playlist_item_id = item["id"]
|
|
playlist_item_id = item["id"]
|
|
@@ -920,13 +921,13 @@ class PlaylistManager(models.Manager):
|
|
)
|
|
)
|
|
try:
|
|
try:
|
|
pl_response = pl_request.execute()
|
|
pl_response = pl_request.execute()
|
|
- print(pl_response)
|
|
+ print_(pl_response)
|
|
except googleapiclient.errors.HttpError as e:
|
|
except googleapiclient.errors.HttpError as e:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- print(e.error_details, e.status_code)
|
|
+ print_(e.error_details, e.status_code)
|
|
return [-1, get_message_from_httperror(e), e.status_code]
|
|
return [-1, get_message_from_httperror(e), e.status_code]
|
|
|
|
|
|
|
|
|
|
@@ -955,16 +956,16 @@ class PlaylistManager(models.Manager):
|
|
pl_request = youtube.playlistItems().delete(
|
|
pl_request = youtube.playlistItems().delete(
|
|
id=playlist_item.playlist_item_id
|
|
id=playlist_item.playlist_item_id
|
|
)
|
|
)
|
|
- print(pl_request)
|
|
+ print_(pl_request)
|
|
try:
|
|
try:
|
|
pl_response = pl_request.execute()
|
|
pl_response = pl_request.execute()
|
|
- print(pl_response)
|
|
+ print_(pl_response)
|
|
except googleapiclient.errors.HttpError as e:
|
|
except googleapiclient.errors.HttpError as e:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- print(e, e.error_details, e.status_code)
|
|
+ print_(e, e.error_details, e.status_code)
|
|
continue
|
|
continue
|
|
|
|
|
|
|
|
|
|
@@ -1049,7 +1050,7 @@ class PlaylistManager(models.Manager):
|
|
try:
|
|
try:
|
|
pl_response = pl_request.execute()
|
|
pl_response = pl_request.execute()
|
|
except googleapiclient.errors.HttpError as e:
|
|
except googleapiclient.errors.HttpError as e:
|
|
- print(e.status_code, e.error_details)
|
|
+ print_(e.status_code, e.error_details)
|
|
if e.status_code == 400:
|
|
if e.status_code == 400:
|
|
result["status"] = 400
|
|
result["status"] = 400
|
|
result["status"] = -1
|
|
result["status"] = -1
|
|
@@ -1079,7 +1080,7 @@ class PlaylistManager(models.Manager):
|
|
},
|
|
},
|
|
)
|
|
)
|
|
|
|
|
|
- print(details["description"])
|
|
+ print_(details["description"])
|
|
try:
|
|
try:
|
|
pl_response = pl_request.execute()
|
|
pl_response = pl_request.execute()
|
|
except googleapiclient.errors.HttpError as e:
|
|
except googleapiclient.errors.HttpError as e:
|
|
@@ -1089,10 +1090,10 @@ class PlaylistManager(models.Manager):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- print("ERROR UPDATING PLAYLIST DETAILS", e, e.status_code, e.error_details)
|
|
+ print_("ERROR UPDATING PLAYLIST DETAILS", e, e.status_code, e.error_details)
|
|
return -1
|
|
return -1
|
|
|
|
|
|
- print(pl_response)
|
|
+ print_(pl_response)
|
|
playlist.name = pl_response['snippet']['title']
|
|
playlist.name = pl_response['snippet']['title']
|
|
playlist.description = pl_response['snippet']['description']
|
|
playlist.description = pl_response['snippet']['description']
|
|
playlist.is_private_on_yt = True if pl_response['status']['privacyStatus'] == "private" else False
|
|
playlist.is_private_on_yt = True if pl_response['status']['privacyStatus'] == "private" else False
|
|
@@ -1140,7 +1141,7 @@ class PlaylistManager(models.Manager):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- print("ERROR UPDATING PLAYLIST DETAILS", e.status_code, e.error_details)
|
|
+ print_("ERROR UPDATING PLAYLIST DETAILS", e.status_code, e.error_details)
|
|
if e.status_code == 400:
|
|
if e.status_code == 400:
|
|
pl_request = youtube.playlistItems().insert(
|
|
pl_request = youtube.playlistItems().insert(
|
|
part="snippet",
|
|
part="snippet",
|
|
@@ -1202,7 +1203,7 @@ class PlaylistManager(models.Manager):
|
|
try:
|
|
try:
|
|
pl_response = pl_request.execute()
|
|
pl_response = pl_request.execute()
|
|
except googleapiclient.errors.HttpError as e:
|
|
except googleapiclient.errors.HttpError as e:
|
|
- print("ERROR ADDDING VIDEOS TO PLAYLIST", e.status_code, e.error_details)
|
|
+ print_("ERROR ADDDING VIDEOS TO PLAYLIST", e.status_code, e.error_details)
|
|
if e.status_code == 400:
|
|
if e.status_code == 400:
|
|
pl_request = youtube.playlistItems().insert(
|
|
pl_request = youtube.playlistItems().insert(
|
|
part="snippet",
|
|
part="snippet",
|
|
@@ -1402,7 +1403,7 @@ class Playlist(models.Model):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
|