views.py 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. import datetime
  2. import json
  3. import random
  4. from django.core import serializers
  5. import bleach
  6. import pytz
  7. from django.db.models import Q, Count
  8. from django.http import HttpResponse
  9. from django.shortcuts import render, redirect, get_object_or_404
  10. from django.utils.html import strip_tags
  11. import apps
  12. from apps.main.models import Playlist, Tag, Video
  13. from django.contrib.auth.decorators import login_required # redirects user to settings.LOGIN_URL
  14. from allauth.socialaccount.models import SocialToken
  15. from django.views.decorators.http import require_POST
  16. from django.contrib import messages
  17. from django.template import loader
  18. from .util import *
  19. # Create your views here.
  20. @login_required
  21. def home(request):
  22. user_profile = request.user
  23. watching = user_profile.playlists.filter(Q(marked_as="watching") & Q(is_in_db=True)).order_by("-num_of_accesses")
  24. recently_accessed_playlists = user_profile.playlists.filter(is_in_db=True).order_by("-updated_at")[:6]
  25. recently_added_playlists = user_profile.playlists.filter(is_in_db=True).order_by("-created_at")[:6]
  26. #### FOR NEWLY JOINED USERS ######
  27. channel_found = True
  28. if user_profile.profile.show_import_page:
  29. """
  30. Logic:
  31. show_import_page is True by default. When a user logs in for the first time (infact anytime), google
  32. redirects them to 'home' url. Since, show_import_page is True by default, the user is then redirected
  33. from 'home' to 'import_in_progress' url
  34. show_import_page is only set false in the import_in_progress.html page, i.e when user cancels YT import
  35. """
  36. # user_profile.show_import_page = False
  37. if user_profile.profile.access_token.strip() == "" or user_profile.profile.refresh_token.strip() == "":
  38. user_social_token = SocialToken.objects.get(account__user=request.user)
  39. user_profile.profile.access_token = user_social_token.token
  40. user_profile.profile.refresh_token = user_social_token.token_secret
  41. user_profile.profile.expires_at = user_social_token.expires_at
  42. user_profile.save()
  43. Playlist.objects.getUserYTChannelID(request.user)
  44. if user_profile.profile.imported_yt_playlists:
  45. user_profile.profile.show_import_page = False # after user imports all their YT playlists no need to show_import_page again
  46. user_profile.profile.save(update_fields=['show_import_page'])
  47. imported_playlists_count = request.user.playlists.filter(Q(is_user_owned=True) & Q(is_in_db=True)).exclude(
  48. playlist_id="LL").count()
  49. return render(request, "home.html",
  50. {"import_successful": True, "imported_playlists_count": imported_playlists_count})
  51. return render(request, "import_in_progress.html")
  52. ##################################
  53. playlist_tags = request.user.playlist_tags.filter(times_viewed_per_week__gte=1).order_by('-times_viewed_per_week')
  54. videos = request.user.videos.filter(Q(is_unavailable_on_yt=False) & Q(was_deleted_on_yt=False))
  55. channels = videos.values(
  56. 'channel_name').annotate(channel_videos_count=Count('video_id'))
  57. return render(request, 'home.html', {"channel_found": channel_found,
  58. "playlist_tags": playlist_tags,
  59. "watching": watching,
  60. "recently_accessed_playlists": recently_accessed_playlists,
  61. "recently_added_playlists": recently_added_playlists,
  62. "videos": videos,
  63. "channels": channels})
  64. @login_required
  65. def favorites(request):
  66. favorite_playlists = request.user.playlists.filter(Q(is_favorite=True) & Q(is_in_db=True)).order_by(
  67. '-last_accessed_on')
  68. favorite_videos = request.user.videos.filter(is_favorite=True).order_by('-num_of_accesses')
  69. return render(request, 'favorites.html', {"playlists": favorite_playlists,
  70. "videos": favorite_videos})
  71. @login_required
  72. def planned_to_watch(request):
  73. planned_to_watch_playlists = request.user.playlists.filter(
  74. Q(marked_as='plan-to-watch') & Q(is_in_db=True)).order_by(
  75. '-last_accessed_on')
  76. planned_to_watch_videos = request.user.videos.filter(is_planned_to_watch=True).order_by('-num_of_accesses')
  77. return render(request, 'planned_to_watch.html', {"playlists": planned_to_watch_playlists,
  78. "videos": planned_to_watch_videos})
  79. @login_required
  80. def view_video(request, video_id):
  81. if request.user.videos.filter(video_id=video_id).exists():
  82. video = request.user.videos.get(video_id=video_id)
  83. if video.is_unavailable_on_yt:
  84. messages.error(request, "Video went private/deleted on YouTube!")
  85. return redirect('home')
  86. video.num_of_accesses += 1
  87. video.save(update_fields=['num_of_accesses'])
  88. return render(request, 'view_video.html', {"video": video})
  89. else:
  90. messages.error(request, "No such video in your UnTube collection!")
  91. return redirect('home')
  92. @login_required
  93. @require_POST
  94. def video_notes(request, video_id):
  95. print(request.POST)
  96. if request.user.videos.filter(video_id=video_id).exists():
  97. video = request.user.videos.get(video_id=video_id)
  98. if 'video-notes-text-area' in request.POST:
  99. video.user_notes = bleach.clean(request.POST['video-notes-text-area'], tags=['br'])
  100. video.save(update_fields=['user_notes', 'user_label'])
  101. # messages.success(request, 'Saved!')
  102. return HttpResponse("""
  103. <div hx-ext="class-tools">
  104. <div classes="add visually-hidden:2s">Saved!</div>
  105. </div>
  106. """)
  107. else:
  108. return HttpResponse('No such video in your UnTube collection!')
  109. @login_required
  110. def view_playlist(request, playlist_id):
  111. user_profile = request.user
  112. user_owned_playlists = user_profile.playlists.filter(Q(is_user_owned=True) & Q(is_in_db=True))
  113. # specific playlist requested
  114. if user_profile.playlists.filter(Q(playlist_id=playlist_id) & Q(is_in_db=True)).exists():
  115. playlist = user_profile.playlists.get(playlist_id__exact=playlist_id)
  116. playlist_tags = playlist.tags.all()
  117. # if its been 1 days since the last full scan, force refresh the playlist
  118. if playlist.last_full_scan_at + datetime.timedelta(days=2) < datetime.datetime.now(pytz.utc):
  119. playlist.has_playlist_changed = True
  120. print("ITS BEEN 15 DAYS, FORCE REFRESHING PLAYLIST")
  121. # only note down that the playlist as been viewed when 30s has passed since the last access
  122. if playlist.last_accessed_on + datetime.timedelta(seconds=30) < datetime.datetime.now(pytz.utc):
  123. playlist.last_accessed_on = datetime.datetime.now(pytz.utc)
  124. playlist.num_of_accesses += 1
  125. increment_tag_views(playlist_tags)
  126. playlist.save(update_fields=['num_of_accesses', 'last_accessed_on', 'has_playlist_changed'])
  127. else:
  128. if playlist_id == "LL": # liked videos playlist hasnt been imported yet
  129. return render(request, 'view_playlist.html', {"not_imported_LL": True})
  130. messages.error(request, "No such playlist found!")
  131. return redirect('home')
  132. if playlist.has_new_updates:
  133. recently_updated_videos = playlist.videos.filter(video_details_modified=True)
  134. for video in recently_updated_videos:
  135. if video.video_details_modified_at + datetime.timedelta(hours=12) < datetime.datetime.now(
  136. pytz.utc): # expired
  137. video.video_details_modified = False
  138. video.save()
  139. if not recently_updated_videos.exists():
  140. playlist.has_new_updates = False
  141. playlist.save()
  142. playlist_items = playlist.playlist_items.select_related('video').order_by("video_position")
  143. user_created_tags = Tag.objects.filter(created_by=request.user)
  144. unused_tags = user_created_tags.difference(playlist_tags)
  145. if request.user.profile.hide_unavailable_videos:
  146. playlist_items.exclude(Q(video__is_unavailable_on_yt=True) & Q(video__was_deleted_on_yt=False))
  147. return render(request, 'view_playlist.html', {"playlist": playlist,
  148. "playlist_tags": playlist_tags,
  149. "unused_tags": unused_tags,
  150. "playlist_items": playlist_items,
  151. "user_owned_playlists": user_owned_playlists,
  152. "watching_message": generateWatchingMessage(playlist),
  153. })
  154. @login_required
  155. def tagged_playlists(request, tag):
  156. tag = get_object_or_404(Tag, created_by=request.user, name=tag)
  157. playlists = request.user.playlists.all().filter(Q(is_in_db=True) & Q(tags__name=tag.name)).order_by("-updated_at")
  158. return render(request, 'all_playlists_with_tag.html', {"playlists": playlists, "tag": tag})
  159. @login_required
  160. def library(request, library_type):
  161. """
  162. Possible playlist types for marked_as attribute: (saved in database like this)
  163. "none", "watching", "plan-to-watch"
  164. """
  165. library_type = library_type.lower()
  166. watching = False
  167. if library_type.lower() == "home": # displays cards of all playlist types
  168. return render(request, 'library.html')
  169. elif library_type == "all":
  170. playlists = request.user.playlists.all().filter(is_in_db=True)
  171. library_type_display = "All Playlists"
  172. elif library_type == "user-owned": # YT playlists owned by user
  173. playlists = request.user.playlists.all().filter(Q(is_user_owned=True) & Q(is_in_db=True))
  174. library_type_display = "Your YouTube Playlists"
  175. elif library_type == "imported": # YT playlists (public) owned by others
  176. playlists = request.user.playlists.all().filter(Q(is_user_owned=False) & Q(is_in_db=True))
  177. library_type_display = "Imported playlists"
  178. elif library_type == "favorites": # YT playlists (public) owned by others
  179. playlists = request.user.playlists.all().filter(Q(is_favorite=True) & Q(is_in_db=True))
  180. library_type_display = "Favorites"
  181. elif library_type.lower() in ["watching", "plan-to-watch"]:
  182. playlists = request.user.playlists.filter(Q(marked_as=library_type.lower()) & Q(is_in_db=True))
  183. library_type_display = library_type.lower().replace("-", " ")
  184. if library_type.lower() == "watching":
  185. watching = True
  186. elif library_type.lower() == "yt-mix":
  187. playlists = request.user.playlists.all().filter(Q(is_yt_mix=True) & Q(is_in_db=True))
  188. library_type_display = "Your YouTube Mixes"
  189. elif library_type.lower() == "unavailable-videos":
  190. videos = request.user.videos.all().filter(Q(is_unavailable_on_yt=False) & Q(was_deleted_on_yt=True))
  191. return render(request, "unavailable_videos.html", {"videos": videos})
  192. elif library_type.lower() == "random": # randomize playlist
  193. if request.method == "POST":
  194. playlists_type = request.POST["playlistsType"]
  195. if playlists_type == "All":
  196. playlists = request.user.playlists.all().filter(is_in_db=True)
  197. elif playlists_type == "Favorites":
  198. playlists = request.user.playlists.all().filter(Q(is_favorite=True) & Q(is_in_db=True))
  199. elif playlists_type == "Watching":
  200. playlists = request.user.playlists.filter(Q(marked_as="watching") & Q(is_in_db=True))
  201. elif playlists_type == "Plan to Watch":
  202. playlists = request.user.playlists.filter(Q(marked_as="plan-to-watch") & Q(is_in_db=True))
  203. else:
  204. return redirect('/library/home')
  205. if not playlists.exists():
  206. messages.info(request, f"No playlists in {playlists_type}")
  207. return redirect('/library/home')
  208. random_playlist = random.choice(playlists)
  209. return redirect(f'/playlist/{random_playlist.playlist_id}')
  210. return render(request, 'library.html')
  211. else:
  212. return redirect('home')
  213. return render(request, 'all_playlists.html', {"playlists": playlists.order_by("-updated_at"),
  214. "library_type": library_type,
  215. "library_type_display": library_type_display,
  216. "watching": watching})
  217. @login_required
  218. def order_playlist_by(request, playlist_id, order_by):
  219. playlist = request.user.playlists.get(Q(playlist_id=playlist_id) & Q(is_in_db=True))
  220. display_text = "Nothing in this playlist! Add something!" # what to display when requested order/filter has no videws
  221. videos_details = ""
  222. if order_by == "all":
  223. playlist_items = playlist.playlist_items.select_related('video').order_by("video_position")
  224. elif order_by == "favorites":
  225. playlist_items = playlist.playlist_items.select_related('video').filter(video__is_favorite=True).order_by(
  226. "video_position")
  227. videos_details = "Sorted by Favorites"
  228. display_text = "No favorites yet!"
  229. elif order_by == "popularity":
  230. videos_details = "Sorted by Popularity"
  231. playlist_items = playlist.playlist_items.select_related('video').order_by("-video__like_count")
  232. elif order_by == "date-published":
  233. videos_details = "Sorted by Date Published"
  234. playlist_items = playlist.playlist_items.select_related('video').order_by("published_at")
  235. elif order_by == "views":
  236. videos_details = "Sorted by View Count"
  237. playlist_items = playlist.playlist_items.select_related('video').order_by("-video__view_count")
  238. elif order_by == "has-cc":
  239. videos_details = "Filtered by Has CC"
  240. playlist_items = playlist.playlist_items.select_related('video').filter(video__has_cc=True).order_by(
  241. "video_position")
  242. display_text = "No videos in this playlist have CC :("
  243. elif order_by == "duration":
  244. videos_details = "Sorted by Video Duration"
  245. playlist_items = playlist.playlist_items.select_related('video').order_by("-video__duration_in_seconds")
  246. elif order_by == 'new-updates':
  247. playlist_items = []
  248. videos_details = "Sorted by New Updates"
  249. display_text = "No new updates! Note that deleted videos will not show up here."
  250. if playlist.has_new_updates:
  251. recently_updated_videos = playlist.playlist_items.select_related('video').filter(
  252. video__video_details_modified=True)
  253. for playlist_item in recently_updated_videos:
  254. if playlist_item.video.video_details_modified_at + datetime.timedelta(hours=12) < datetime.datetime.now(
  255. pytz.utc): # expired
  256. playlist_item.video.video_details_modified = False
  257. playlist_item.video.save(update_fields=['video_details_modified'])
  258. if not recently_updated_videos.exists():
  259. playlist.has_new_updates = False
  260. playlist.save(update_fields=['has_new_updates'])
  261. else:
  262. playlist_items = recently_updated_videos.order_by("video_position")
  263. elif order_by == 'unavailable-videos':
  264. playlist_items = playlist.playlist_items.select_related('video').filter(
  265. Q(video__is_unavailable_on_yt=False) & Q(video__was_deleted_on_yt=True))
  266. videos_details = "Sorted by Unavailable Videos"
  267. display_text = "None of the videos in this playlist have gone unavailable... yet."
  268. elif order_by == 'channel':
  269. channel_name = request.GET["channel-name"]
  270. playlist_items = playlist.playlist_items.select_related('video').filter(
  271. video__channel_name=channel_name).order_by("video_position")
  272. videos_details = f"Sorted by Channel '{channel_name}'"
  273. else:
  274. return HttpResponse("Something went wrong :(")
  275. return HttpResponse(loader.get_template("intercooler/playlist_items.html").render({"playlist": playlist,
  276. "playlist_items": playlist_items,
  277. "videos_details": videos_details,
  278. "display_text": display_text,
  279. "order_by": order_by}))
  280. @login_required
  281. def order_playlists_by(request, library_type, order_by):
  282. watching = False
  283. if library_type == "" or library_type.lower() == "all":
  284. playlists = request.user.playlists.all()
  285. elif library_type.lower() == "favorites":
  286. playlists = request.user.playlists.filter(Q(is_favorite=True) & Q(is_in_db=True))
  287. elif library_type.lower() in ["watching", "plan-to-watch"]:
  288. playlists = request.user.playlists.filter(Q(marked_as=library_type.lower()) & Q(is_in_db=True))
  289. if library_type.lower() == "watching":
  290. watching = True
  291. elif library_type.lower() == "imported":
  292. playlists = request.user.playlists.filter(Q(is_user_owned=False) & Q(is_in_db=True))
  293. elif library_type.lower() == "user-owned":
  294. playlists = request.user.playlists.filter(Q(is_user_owned=True) & Q(is_in_db=True))
  295. else:
  296. return HttpResponse("Not found.")
  297. if order_by == 'recently-accessed':
  298. playlists = playlists.order_by("-updated_at")
  299. elif order_by == 'playlist-duration-in-seconds':
  300. playlists = playlists.order_by("-playlist_duration_in_seconds")
  301. elif order_by == 'video-count':
  302. playlists = playlists.order_by("-video_count")
  303. return HttpResponse(loader.get_template("intercooler/playlists.html")
  304. .render({"playlists": playlists, "watching": watching}))
  305. @login_required
  306. def mark_playlist_as(request, playlist_id, mark_as):
  307. playlist = request.user.playlists.get(playlist_id=playlist_id)
  308. marked_as_response = '<span></span><meta http-equiv="refresh" content="0" />'
  309. if mark_as in ["watching", "on-hold", "plan-to-watch"]:
  310. playlist.marked_as = mark_as
  311. playlist.save()
  312. icon = ""
  313. if mark_as == "watching":
  314. playlist.last_watched = datetime.datetime.now(pytz.utc)
  315. playlist.save(update_fields=['last_watched'])
  316. icon = '<i class="fas fa-fire-alt me-2"></i>'
  317. elif mark_as == "plan-to-watch":
  318. icon = '<i class="fas fa-flag me-2"></i>'
  319. marked_as_response = f'<span class="badge bg-success text-white" >{icon}{mark_as}</span> <meta http-equiv="refresh" content="0" />'
  320. elif mark_as == "none":
  321. playlist.marked_as = mark_as
  322. playlist.save()
  323. elif mark_as == "favorite":
  324. if playlist.is_favorite:
  325. playlist.is_favorite = False
  326. playlist.save()
  327. return HttpResponse('<i class="far fa-star"></i>')
  328. else:
  329. playlist.is_favorite = True
  330. playlist.save()
  331. return HttpResponse('<i class="fas fa-star" style="color: #fafa06"></i>')
  332. else:
  333. return redirect('home')
  334. return HttpResponse(marked_as_response)
  335. @login_required
  336. def playlists_home(request):
  337. return render(request, 'library.html')
  338. @login_required
  339. @require_POST
  340. def playlist_delete_videos(request, playlist_id, command):
  341. all = False
  342. num_vids = 0
  343. playlist_item_ids = []
  344. print(request.POST)
  345. if "all" in request.POST:
  346. if request.POST["all"] == "yes":
  347. all = True
  348. num_vids = request.user.playlists.get(playlist_id=playlist_id).playlist_items.all().count()
  349. if command == "start":
  350. playlist_item_ids = [playlist_item.playlist_item_id for playlist_item in
  351. request.user.playlists.get(playlist_id=playlist_id).playlist_items.all()]
  352. else:
  353. playlist_item_ids = request.POST.getlist("video-id", default=[])
  354. num_vids = len(playlist_item_ids)
  355. extra_text = " "
  356. if num_vids == 0:
  357. return HttpResponse("""
  358. <h5>Select some videos first!</h5><hr>
  359. """)
  360. if 'confirm before deleting' in request.POST:
  361. if request.POST['confirm before deleting'] == 'False':
  362. command = "confirmed"
  363. if command == "confirm":
  364. if all or num_vids == request.user.playlists.get(playlist_id=playlist_id).playlist_items.all().count():
  365. hx_vals = """hx-vals='{"all": "yes"}'"""
  366. delete_text = "ALL VIDEOS"
  367. extra_text = " This will not delete the playlist itself, will only make the playlist empty. "
  368. else:
  369. hx_vals = ""
  370. delete_text = f"{num_vids} videos"
  371. if playlist_id == "LL":
  372. extra_text += "Since you're deleting from your Liked Videos playlist, the selected videos will also be unliked from YouTube. "
  373. url = f"/playlist/{playlist_id}/delete-videos/confirmed"
  374. return HttpResponse(
  375. f"""
  376. <div hx-ext="class-tools">
  377. <div classes="add visually-hidden:30s">
  378. <h5>
  379. Are you sure you want to delete {delete_text} from your YouTube playlist?{extra_text}This cannot be undone.</h5>
  380. <button hx-post="{url}" hx-include="[id='video-checkboxes']" {hx_vals} hx-target="#delete-videos-confirm-box" type="button" class="btn btn-outline-danger btn-sm">Confirm</button>
  381. <hr>
  382. </div>
  383. </div>
  384. """)
  385. elif command == "confirmed":
  386. if all:
  387. hx_vals = """hx-vals='{"all": "yes"}'"""
  388. else:
  389. hx_vals = ""
  390. url = f"/playlist/{playlist_id}/delete-videos/start"
  391. return HttpResponse(
  392. f"""
  393. <div class="spinner-border text-light" role="status" hx-post="{url}" {hx_vals} hx-trigger="load" hx-include="[id='video-checkboxes']" hx-target="#delete-videos-confirm-box"></div><hr>
  394. """)
  395. elif command == "start":
  396. print("Deleting", len(playlist_item_ids), "videos")
  397. Playlist.objects.deletePlaylistItems(request.user, playlist_id, playlist_item_ids)
  398. if all:
  399. help_text = "Finished emptying this playlist."
  400. else:
  401. help_text = "Done deleting selected videos from your playlist on YouTube."
  402. messages.success(request, help_text)
  403. return HttpResponse(f"""
  404. <h5>
  405. Done! Refreshing...
  406. <script>
  407. window.location.reload();
  408. </script>
  409. </h5>
  410. <hr>
  411. """)
  412. @login_required
  413. @require_POST
  414. def delete_specific_videos(request, playlist_id, command):
  415. Playlist.objects.deleteSpecificPlaylistItems(request.user, playlist_id, command)
  416. help_text = "Error."
  417. if command == "unavailable":
  418. help_text = "Deleted all unavailable videos."
  419. elif command == "duplicate":
  420. help_text = "Deleted all duplicate videos."
  421. messages.success(request, help_text)
  422. return HttpResponse(f"""
  423. <h5>
  424. Done. Refreshing...
  425. <script>
  426. window.location.reload();
  427. </script>
  428. </h5>
  429. <hr>
  430. """)
  431. #### MANAGE VIDEOS #####
  432. @login_required
  433. def mark_video_favortie(request, video_id):
  434. video = request.user.videos.get(video_id=video_id)
  435. if video.is_favorite:
  436. video.is_favorite = False
  437. video.save(update_fields=['is_favorite'])
  438. return HttpResponse('<i class="far fa-heart"></i>')
  439. else:
  440. video.is_favorite = True
  441. video.save(update_fields=['is_favorite'])
  442. return HttpResponse('<i class="fas fa-heart" style="color: #fafa06"></i>')
  443. @login_required
  444. def mark_video_planned_to_watch(request, video_id):
  445. video = request.user.videos.get(video_id=video_id)
  446. if video.is_planned_to_watch:
  447. video.is_planned_to_watch = False
  448. video.save(update_fields=['is_planned_to_watch'])
  449. return HttpResponse('<i class="far fa-clock"></i>')
  450. else:
  451. video.is_planned_to_watch = True
  452. video.save(update_fields=['is_planned_to_watch'])
  453. return HttpResponse('<i class="fas fa-clock" style="color: #000000"></i>')
  454. @login_required
  455. def mark_video_watched(request, playlist_id, video_id):
  456. playlist = request.user.playlists.get(playlist_id=playlist_id)
  457. video = playlist.videos.get(video_id=video_id)
  458. if video.is_marked_as_watched:
  459. video.is_marked_as_watched = False
  460. video.save(update_fields=['is_marked_as_watched'])
  461. return HttpResponse(
  462. f'<i class="far fa-check-circle" hx-get="/playlist/{playlist_id}/get-watch-message" hx-trigger="load" hx-target="#playlist-watch-message"></i>')
  463. else:
  464. video.is_marked_as_watched = True
  465. video.save(update_fields=['is_marked_as_watched'])
  466. playlist.last_watched = datetime.datetime.now(pytz.utc)
  467. playlist.save(update_fields=['last_watched'])
  468. return HttpResponse(
  469. f'<i class="fas fa-check-circle" hx-get="/playlist/{playlist_id}/get-watch-message" hx-trigger="load" hx-target="#playlist-watch-message"></i>')
  470. ###########
  471. @login_required
  472. def load_more_videos(request, playlist_id, order_by, page):
  473. playlist = request.user.playlists.get(playlist_id=playlist_id)
  474. playlist_items = None
  475. if order_by == "all":
  476. playlist_items = playlist.playlist_items.select_related('video').order_by("video_position")
  477. print(f"loading page 1: {playlist_items.count()} videos")
  478. elif order_by == "favorites":
  479. playlist_items = playlist.playlist_items.select_related('video').filter(video__is_favorite=True).order_by(
  480. "video_position")
  481. elif order_by == "popularity":
  482. playlist_items = playlist.playlist_items.select_related('video').order_by("-video__like_count")
  483. elif order_by == "date-published":
  484. playlist_items = playlist.playlist_items.select_related('video').order_by("published_at")
  485. elif order_by == "views":
  486. playlist_items = playlist.playlist_items.select_related('video').order_by("-video__view_count")
  487. elif order_by == "has-cc":
  488. playlist_items = playlist.playlist_items.select_related('video').filter(video__has_cc=True).order_by(
  489. "video_position")
  490. elif order_by == "duration":
  491. playlist_items = playlist.playlist_items.select_related('video').order_by("-video__duration_in_seconds")
  492. elif order_by == 'new-updates':
  493. playlist_items = []
  494. if playlist.has_new_updates:
  495. recently_updated_videos = playlist.playlist_items.select_related('video').filter(
  496. video__video_details_modified=True)
  497. for playlist_item in recently_updated_videos:
  498. if playlist_item.video.video_details_modified_at + datetime.timedelta(hours=12) < datetime.datetime.now(
  499. pytz.utc): # expired
  500. playlist_item.video.video_details_modified = False
  501. playlist_item.video.save()
  502. if not recently_updated_videos.exists():
  503. playlist.has_new_updates = False
  504. playlist.save()
  505. else:
  506. playlist_items = recently_updated_videos.order_by("video_position")
  507. elif order_by == 'unavailable-videos':
  508. playlist_items = playlist.playlist_items.select_related('video').filter(
  509. Q(video__is_unavailable_on_yt=True) & Q(video__was_deleted_on_yt=True))
  510. elif order_by == 'channel':
  511. channel_name = request.GET["channel-name"]
  512. playlist_items = playlist.playlist_items.select_related('video').filter(
  513. video__channel_name=channel_name).order_by("video_position")
  514. if request.user.profile.hide_unavailable_videos:
  515. playlist_items.exclude(Q(video__is_unavailable_on_yt=True) & Q(video__was_deleted_on_yt=False))
  516. return HttpResponse(loader.get_template("intercooler/playlist_items.html")
  517. .render(
  518. {
  519. "playlist": playlist,
  520. "playlist_items": playlist_items[50 * page:], # only send 50 results per page
  521. "page": page + 1,
  522. "order_by": order_by}))
  523. @login_required
  524. @require_POST
  525. def update_playlist_settings(request, playlist_id):
  526. message_type = "success"
  527. message_content = "Saved!"
  528. print(request.POST)
  529. playlist = request.user.playlists.get(playlist_id=playlist_id)
  530. if 'user_label' in request.POST:
  531. playlist.user_label = bleach.clean(request.POST["user_label"])
  532. if 'pl-auto-update' in request.POST:
  533. playlist.auto_check_for_updates = True
  534. else:
  535. playlist.auto_check_for_updates = False
  536. playlist.save(update_fields=['auto_check_for_updates', 'user_label'])
  537. try:
  538. valid_title = bleach.clean(request.POST['playlistTitle'])
  539. valid_description = bleach.clean(request.POST['playlistDesc'])
  540. details = {
  541. "title": valid_title,
  542. "description": valid_description,
  543. "privacyStatus": True if request.POST['playlistPrivacy'] == "Private" else False
  544. }
  545. status = Playlist.objects.updatePlaylistDetails(request.user, playlist_id, details)
  546. if status == -1:
  547. message_type = "danger"
  548. message_content = "Could not save :("
  549. except:
  550. pass
  551. return HttpResponse(loader.get_template("intercooler/messages.html")
  552. .render(
  553. {"message_type": message_type,
  554. "message_content": message_content}))
  555. @login_required
  556. def update_playlist(request, playlist_id, command):
  557. playlist = request.user.playlists.get(playlist_id=playlist_id)
  558. if command == "checkforupdates":
  559. print("Checking if playlist changed...")
  560. result = Playlist.objects.checkIfPlaylistChangedOnYT(request.user, playlist_id)
  561. if result[0] == 1: # full scan was done (full scan is done for a playlist if a week has passed)
  562. deleted_videos, unavailable_videos, added_videos = result[1:]
  563. print("CHANGES", deleted_videos, unavailable_videos, added_videos)
  564. # playlist_changed_text = ["The following modifications happened to this playlist on YouTube:"]
  565. if deleted_videos != 0 or unavailable_videos != 0 or added_videos != 0:
  566. pass
  567. # if added_videos > 0:
  568. # playlist_changed_text.append(f"{added_videos} new video(s) were added")
  569. # if deleted_videos > 0:
  570. # playlist_changed_text.append(f"{deleted_videos} video(s) were deleted")
  571. # if unavailable_videos > 0:
  572. # playlist_changed_text.append(f"{unavailable_videos} video(s) went private/unavailable")
  573. # playlist.playlist_changed_text = "\n".join(playlist_changed_text)
  574. # playlist.has_playlist_changed = True
  575. # playlist.save()
  576. else: # no updates found
  577. return HttpResponse("""
  578. <div hx-ext="class-tools">
  579. <div id="checkforupdates" class="sticky-top" style="top: 0.5em;">
  580. <div class="alert alert-success alert-dismissible fade show" classes="add visually-hidden:1s" role="alert">
  581. Playlist upto date!
  582. </div>
  583. </div>
  584. </div>
  585. """)
  586. elif result[0] == -1: # playlist changed
  587. print("Playlist was deleted from YouTube")
  588. playlist.videos.all().delete()
  589. playlist.delete()
  590. return HttpResponse("""
  591. <div id="checkforupdates" class="sticky-top" style="top: 0.5em;">
  592. <div class="alert alert-danger alert-dismissible fade show sticky-top visually-hidden" role="alert" style="top: 0.5em;">
  593. The playlist owner deleted this playlist on YouTube. It will be deleted for you as well :(
  594. <meta http-equiv="refresh" content="1" />
  595. </div>
  596. </div>
  597. """)
  598. else: # no updates found
  599. return HttpResponse("""
  600. <div id="checkforupdates" class="sticky-top" style="top: 0.5em;">
  601. <div hx-ext="class-tools">
  602. <div classes="add visually-hidden:2s" class="alert alert-success alert-dismissible fade show sticky-top visually-hidden" role="alert" style="top: 0.5em;">
  603. No new updates!
  604. </div>
  605. </div>
  606. </div>
  607. """)
  608. return HttpResponse(f"""
  609. <div hx-get="/playlist/{playlist_id}/update/auto" hx-trigger="load" hx-target="this" class="sticky-top" style="top: 0.5em;">
  610. <div class="alert alert-success alert-dismissible fade show" role="alert">
  611. <div class="d-flex justify-content-center" id="loading-sign">
  612. <img src="/static/svg-loaders/circles.svg" width="40" height="40">
  613. <h5 class="mt-2 ms-2">Changes detected on YouTube, updating playlist '{playlist.name}'...</h5>
  614. </div>
  615. </div>
  616. </div>
  617. """)
  618. if command == "manual":
  619. print("MANUAL")
  620. return HttpResponse(
  621. f"""<div hx-get="/playlist/{playlist_id}/update/auto" hx-trigger="load" hx-swap="outerHTML">
  622. <div class="d-flex justify-content-center mt-4 mb-3" id="loading-sign">
  623. <img src="/static/svg-loaders/circles.svg" width="40" height="40" style="filter: invert(0%) sepia(18%) saturate(7468%) hue-rotate(241deg) brightness(84%) contrast(101%);">
  624. <h5 class="mt-2 ms-2">Refreshing playlist '{playlist.name}', please wait!</h5>
  625. </div>
  626. </div>""")
  627. print("Attempting to update playlist")
  628. status, deleted_playlist_item_ids, unavailable_videos, added_videos = Playlist.objects.updatePlaylist(request.user,
  629. playlist_id)
  630. playlist = request.user.playlists.get(playlist_id=playlist_id)
  631. if status == -1:
  632. playlist_name = playlist.name
  633. playlist.delete()
  634. return HttpResponse(
  635. f"""
  636. <div class="d-flex justify-content-center mt-4 mb-3" id="loading-sign">
  637. <h5 class="mt-2 ms-2">Looks like the playlist '{playlist_name}' was deleted on YouTube. It has been removed from UnTube as well.</h5>
  638. </div>
  639. """)
  640. print("Updated playlist")
  641. playlist_changed_text = []
  642. if len(added_videos) != 0:
  643. playlist_changed_text.append(f"{len(added_videos)} added")
  644. for video in added_videos:
  645. playlist_changed_text.append(f"--> {video.name}")
  646. # if len(added_videos) > 3:
  647. # playlist_changed_text.append(f"+ {len(added_videos) - 3} more")
  648. if len(unavailable_videos) != 0:
  649. if len(playlist_changed_text) == 0:
  650. playlist_changed_text.append(f"{len(unavailable_videos)} went unavailable")
  651. else:
  652. playlist_changed_text.append(f"\n{len(unavailable_videos)} went unavailable")
  653. for video in unavailable_videos:
  654. playlist_changed_text.append(f"--> {video.name}")
  655. if len(deleted_playlist_item_ids) != 0:
  656. if len(playlist_changed_text) == 0:
  657. playlist_changed_text.append(f"{len(deleted_playlist_item_ids)} deleted")
  658. else:
  659. playlist_changed_text.append(f"\n{len(deleted_playlist_item_ids)} deleted")
  660. for playlist_item_id in deleted_playlist_item_ids:
  661. playlist_item = playlist.playlist_items.select_related('video').get(playlist_item_id=playlist_item_id)
  662. video = playlist_item.video
  663. playlist_changed_text.append(f"--> {playlist_item.video.name}")
  664. playlist_item.delete()
  665. if playlist_id == "LL":
  666. video.liked = False
  667. video.save(update_fields=['liked'])
  668. if not playlist.playlist_items.filter(video__video_id=video.video_id).exists():
  669. playlist.videos.remove(video)
  670. if len(playlist_changed_text) == 0:
  671. playlist_changed_text = [
  672. "Updated playlist and video details to their latest. No new changes found in terms of modifications made to this playlist!"]
  673. # return HttpResponse
  674. return HttpResponse(loader.get_template("intercooler/playlist_updates.html")
  675. .render(
  676. {"playlist_changed_text": "\n".join(playlist_changed_text),
  677. "playlist_id": playlist_id}))
  678. @login_required
  679. def view_playlist_settings(request, playlist_id):
  680. try:
  681. playlist = request.user.playlists.get(playlist_id=playlist_id)
  682. except apps.main.models.Playlist.DoesNotExist:
  683. messages.error(request, "No such playlist found!")
  684. return redirect('home')
  685. return render(request, 'view_playlist_settings.html', {"playlist": playlist})
  686. @login_required
  687. def get_playlist_tags(request, playlist_id):
  688. playlist = request.user.playlists.get(playlist_id=playlist_id)
  689. playlist_tags = playlist.tags.all()
  690. return HttpResponse(loader.get_template("intercooler/playlist_tags.html")
  691. .render(
  692. {"playlist_id": playlist_id,
  693. "playlist_tags": playlist_tags}))
  694. @login_required
  695. def get_unused_playlist_tags(request, playlist_id):
  696. playlist = request.user.playlists.get(playlist_id=playlist_id)
  697. user_created_tags = Tag.objects.filter(created_by=request.user)
  698. playlist_tags = playlist.tags.all()
  699. unused_tags = user_created_tags.difference(playlist_tags)
  700. return HttpResponse(loader.get_template("intercooler/playlist_tags_unused.html")
  701. .render(
  702. {"unused_tags": unused_tags}))
  703. @login_required
  704. def get_watch_message(request, playlist_id):
  705. playlist = request.user.playlists.get(playlist_id=playlist_id)
  706. return HttpResponse(loader.get_template("intercooler/playlist_watch_message.html")
  707. .render(
  708. {"playlist": playlist}))
  709. @login_required
  710. @require_POST
  711. def create_playlist_tag(request, playlist_id):
  712. tag_name = request.POST["createTagField"]
  713. if tag_name.lower() == 'Pick from existing unused tags'.lower():
  714. return HttpResponse("Can't use that! Try again >_<")
  715. playlist = request.user.playlists.get(playlist_id=playlist_id)
  716. user_created_tags = Tag.objects.filter(created_by=request.user)
  717. if not user_created_tags.filter(name__iexact=tag_name).exists(): # no tag found, so create it
  718. tag = Tag(name=tag_name, created_by=request.user)
  719. tag.save()
  720. # add it to playlist
  721. playlist.tags.add(tag)
  722. else:
  723. return HttpResponse("""
  724. Already created. Try Again >w<
  725. """)
  726. # playlist_tags = playlist.tags.all()
  727. # unused_tags = user_created_tags.difference(playlist_tags)
  728. return HttpResponse(f"""
  729. Created and Added!
  730. <span class="visually-hidden" hx-get="/playlist/{playlist_id}/get-tags" hx-trigger="load" hx-target="#playlist-tags"></span>
  731. """)
  732. @login_required
  733. @require_POST
  734. def add_playlist_tag(request, playlist_id):
  735. tag_name = request.POST["playlistTag"]
  736. if tag_name == 'Pick from existing unused tags':
  737. return HttpResponse("Pick something! >w<")
  738. try:
  739. tag = request.user.playlist_tags.get(name__iexact=tag_name)
  740. except:
  741. return HttpResponse("Uh-oh, looks like this tag was deleted :(")
  742. playlist = request.user.playlists.get(playlist_id=playlist_id)
  743. playlist_tags = playlist.tags.all()
  744. if not playlist_tags.filter(name__iexact=tag_name).exists(): # tag not on this playlist, so add it
  745. # add it to playlist
  746. playlist.tags.add(tag)
  747. else:
  748. return HttpResponse("Already Added >w<")
  749. return HttpResponse(f"""
  750. Added!
  751. <span class="visually-hidden" hx-get="/playlist/{playlist_id}/get-tags" hx-trigger="load" hx-target="#playlist-tags"></span>
  752. """)
  753. @login_required
  754. @require_POST
  755. def remove_playlist_tag(request, playlist_id, tag_name):
  756. playlist = request.user.playlists.get(playlist_id=playlist_id)
  757. playlist_tags = playlist.tags.all()
  758. if playlist_tags.filter(name__iexact=tag_name).exists(): # tag on this playlist, remove it it
  759. tag = Tag.objects.filter(Q(created_by=request.user) & Q(name__iexact=tag_name)).first()
  760. print("Removed tag", tag_name)
  761. # remove it from the playlist
  762. playlist.tags.remove(tag)
  763. else:
  764. return HttpResponse("Whoops >w<")
  765. return HttpResponse("")
  766. @login_required
  767. def delete_playlist(request, playlist_id):
  768. playlist = request.user.playlists.get(playlist_id=playlist_id)
  769. if request.GET["confirmed"] == "no":
  770. return HttpResponse(f"""
  771. <a href="/playlist/{playlist_id}/delete-playlist?confirmed=yes" hx-indicator="#delete-pl-loader" class="btn btn-danger">Confirm Delete</a>
  772. <a href="/playlist/{playlist_id}" class="btn btn-secondary ms-1">Cancel</a>
  773. """)
  774. if not playlist.is_user_owned: # if playlist trying to delete isn't user owned
  775. video_ids = [video.video_id for video in playlist.videos.all()]
  776. playlist.delete()
  777. for video_id in video_ids:
  778. video = request.user.videos.get(video_id=video_id)
  779. if video.playlists.all().count() == 0:
  780. video.delete()
  781. messages.success(request, "Successfully deleted playlist from UnTube.")
  782. else:
  783. # deletes it from YouTube first then from UnTube
  784. status = Playlist.objects.deletePlaylistFromYouTube(request.user, playlist_id)
  785. if status[0] == -1: # failed to delete playlist from youtube
  786. # if status[2] == 404:
  787. # playlist.delete()
  788. # messages.success(request, 'Looks like the playlist was already deleted on YouTube. Removed it from UnTube as well.')
  789. # return redirect('home')
  790. messages.error(request, f"[{status[1]}] Failed to delete playlist from YouTube :(")
  791. return redirect('view_playlist_settings', playlist_id=playlist_id)
  792. messages.success(request, "Successfully deleted playlist from YouTube and removed it from UnTube as well.")
  793. return redirect('home')
  794. @login_required
  795. def reset_watched(request, playlist_id):
  796. playlist = request.user.playlists.get(playlist_id=playlist_id)
  797. for video in playlist.videos.filter(Q(is_unavailable_on_yt=False) & Q(was_deleted_on_yt=False)):
  798. video.is_marked_as_watched = False
  799. video.save(update_fields=['is_marked_as_watched'])
  800. # messages.success(request, "Successfully marked all videos unwatched.")
  801. return redirect(f'/playlist/{playlist.playlist_id}')
  802. @login_required
  803. @require_POST
  804. def playlist_move_copy_videos(request, playlist_id, action):
  805. playlist_ids = request.POST.getlist("playlist-ids", default=[])
  806. playlist_item_ids = request.POST.getlist("video-id", default=[])
  807. # basic processing
  808. if not playlist_ids and not playlist_item_ids:
  809. return HttpResponse(f"""
  810. <span class="text-warning">Mistakes happen. Try again >w<</span>""")
  811. elif not playlist_ids:
  812. return HttpResponse(f"""
  813. <span class="text-danger">First select some playlists to {action} to!</span>""")
  814. elif not playlist_item_ids:
  815. return HttpResponse(f"""
  816. <span class="text-danger">First select some videos to {action}!</span>""")
  817. success_message = f"""
  818. <div hx-ext="class-tools">
  819. <span classes="add visually-hidden:5s" class="text-success">Successfully {'moved' if action == 'move' else 'copied'} {len(playlist_item_ids)} video(s) to {len(playlist_ids)} other playlist(s)!
  820. Go visit those playlist(s)!</span>
  821. </div>
  822. """
  823. if action == "move":
  824. result = Playlist.objects.moveCopyVideosFromPlaylist(request.user,
  825. from_playlist_id=playlist_id,
  826. to_playlist_ids=playlist_ids,
  827. playlist_item_ids=playlist_item_ids,
  828. action="move")
  829. if result['status'] == -1:
  830. if result['status'] == 404:
  831. return HttpResponse(
  832. "<span class='text-danger'>You cannot copy/move unavailable videos! De-select them and try again.</span>")
  833. return HttpResponse("Error moving!")
  834. else: # copy
  835. status = Playlist.objects.moveCopyVideosFromPlaylist(request.user,
  836. from_playlist_id=playlist_id,
  837. to_playlist_ids=playlist_ids,
  838. playlist_item_ids=playlist_item_ids)
  839. if status[0] == -1:
  840. if status[1] == 404:
  841. return HttpResponse(
  842. "<span class='text-danger'>You cannot copy/move unavailable videos! De-select them and try again.</span>")
  843. return HttpResponse("Error copying!")
  844. return HttpResponse(success_message)
  845. @login_required
  846. def playlist_open_random_video(request, playlist_id):
  847. playlist = request.user.playlists.get(playlist_id=playlist_id)
  848. videos = playlist.videos.all()
  849. random_video = random.choice(videos)
  850. return redirect(f'/video/{random_video.video_id}')
  851. @login_required
  852. def playlist_completion_times(request, playlist_id):
  853. playlist_duration = request.user.playlists.get(playlist_id=playlist_id).playlist_duration_in_seconds
  854. return HttpResponse(f"""
  855. <h5 class="text-warning">Playlist completion times:</h5>
  856. <h6>At 1.25x speed: {getHumanizedTimeString(playlist_duration / 1.25)}</h6>
  857. <h6>At 1.5x speed: {getHumanizedTimeString(playlist_duration / 1.5)}</h6>
  858. <h6>At 1.75x speed: {getHumanizedTimeString(playlist_duration / 1.75)}</h6>
  859. <h6>At 2x speed: {getHumanizedTimeString(playlist_duration / 2)}</h6>
  860. """)
  861. @login_required
  862. def video_completion_times(request, video_id):
  863. video_duration = request.user.videos.get(video_id=video_id).duration_in_seconds
  864. return HttpResponse(f"""
  865. <h5 class="text-warning">Video completion times:</h5>
  866. <h6>At 1.25x speed: {getHumanizedTimeString(video_duration / 1.25)}</h6>
  867. <h6>At 1.5x speed: {getHumanizedTimeString(video_duration / 1.5)}</h6>
  868. <h6>At 1.75x speed: {getHumanizedTimeString(video_duration / 1.75)}</h6>
  869. <h6>At 2x speed: {getHumanizedTimeString(video_duration / 2)}</h6>
  870. """)
  871. @login_required
  872. @require_POST
  873. def add_video_user_label(request, video_id):
  874. video = request.user.videos.get(video_id=video_id)
  875. if "user_label" in request.POST:
  876. video.user_label = bleach.clean(request.POST["user_label"])
  877. video.save(update_fields=['user_label'])
  878. return redirect('video', video_id=video_id)
  879. @login_required
  880. @require_POST
  881. def edit_tag(request, tag):
  882. tag = request.user.playlist_tags.get(name=tag)
  883. if 'tag_name' in request.POST:
  884. tag.name = bleach.clean(request.POST["tag_name"])
  885. tag.save(update_fields=['name'])
  886. messages.success(request, "Successfully updated the tag's name!")
  887. return redirect('tagged_playlists', tag=tag.name)
  888. @login_required
  889. @require_POST
  890. def delete_tag(request, tag):
  891. tag = request.user.playlist_tags.get(name__iexact=tag)
  892. tag.delete()
  893. messages.success(request, f"Successfully deleted the tag '{tag.name}'")
  894. return redirect('/library/home')
  895. @login_required
  896. @require_POST
  897. def add_playlist_user_label(request, playlist_id):
  898. playlist = request.user.playlists.get(playlist_id=playlist_id)
  899. if "user_label" in request.POST:
  900. playlist.user_label = bleach.clean(request.POST["user_label"].strip())
  901. playlist.save(update_fields=['user_label'])
  902. return redirect('playlist', playlist_id=playlist_id)
  903. @login_required
  904. @require_POST
  905. def playlist_add_new_videos(request, playlist_id):
  906. textarea_input = bleach.clean(request.POST["add-videos-textarea"])
  907. video_links = textarea_input.strip().split("\n")[:25]
  908. video_ids = []
  909. for video_link in video_links:
  910. if video_link.strip() == "":
  911. continue
  912. video_id = getVideoId(video_link)
  913. if video_id is None or video_id in video_ids:
  914. continue
  915. video_ids.append(video_id)
  916. result = Playlist.objects.addVideosToPlaylist(request.user, playlist_id, video_ids)
  917. added = result["num_added"]
  918. max_limit_reached = result["playlistContainsMaximumNumberOfVideos"]
  919. if max_limit_reached and added == 0:
  920. message = "Could not add any new videos to this playlist as the max limit has been reached :("
  921. messages.error(request, message)
  922. elif max_limit_reached and added != 0:
  923. message = f"Only added the first {added} video link(s) to this playlist as the max playlist limit has been reached :("
  924. messages.warning(request, message)
  925. # else:
  926. # message = f"Successfully added {added} videos to this playlist."
  927. # messages.success(request, message)
  928. return HttpResponse("""
  929. Done! Refreshing...
  930. <script>
  931. window.location.reload();
  932. </script>
  933. """)
  934. @login_required
  935. @require_POST
  936. def playlist_create_new_playlist(request, playlist_id):
  937. playlist_name = bleach.clean(request.POST["playlist-name"].strip())
  938. playlist_description = bleach.clean(request.POST["playlist-description"])
  939. if playlist_name == "":
  940. return HttpResponse("Enter a playlist name first!")
  941. unclean_playlist_item_ids = request.POST.getlist("video-id", default=[])
  942. clean_playlist_item_ids = [bleach.clean(playlist_item_id) for playlist_item_id in unclean_playlist_item_ids]
  943. playlist_items = request.user.playlists.get(playlist_id=playlist_id).playlist_items.filter(
  944. playlist_item_id__in=clean_playlist_item_ids)
  945. if not playlist_items.exists():
  946. return HttpResponse("Select some videos first!")
  947. else:
  948. result = Playlist.objects.createNewPlaylist(request.user, playlist_name, playlist_description)
  949. if result["status"] == 0: # playlist created on youtube
  950. new_playlist_id = result["playlist_id"]
  951. elif result["status"] == -1:
  952. return HttpResponse("Error creating playlist!")
  953. elif result["status"] == 400:
  954. return HttpResponse("Max playlists limit reached!")
  955. video_ids = []
  956. for playlist_item in playlist_items:
  957. video_ids.append(playlist_item.video.video_id)
  958. result = Playlist.objects.addVideosToPlaylist(request.user, new_playlist_id, video_ids)
  959. added = result["num_added"]
  960. max_limit_reached = result["playlistContainsMaximumNumberOfVideos"]
  961. if max_limit_reached:
  962. message = f"Only added the first {added} video link(s) to the new playlist as the max playlist limit has been reached :("
  963. else:
  964. message = f"""Successfully created '{playlist_name}' and added {added} videos to it. Visit the <a href="/home/" target="_blank" style="text-decoration: none; color: white" class="ms-1 me-1">dashboard</a> to import it into UnTube."""
  965. return HttpResponse(message)