views.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import bleach
  2. from django.contrib.auth.decorators import login_required
  3. from django.db.models import Q
  4. from django.http import HttpResponse
  5. from django.shortcuts import redirect, render
  6. from django.template import loader
  7. from django.views.decorators.http import require_POST
  8. import logging
  9. logger = logging.getLogger(__name__)
  10. @login_required
  11. def search(request):
  12. if request.method == 'GET':
  13. logger.debug(request.GET)
  14. if 'mode' in request.GET:
  15. mode = bleach.clean(request.GET['mode'])
  16. else:
  17. mode = 'playlists'
  18. if 'type' in request.GET:
  19. item_type = bleach.clean(request.GET['type'])
  20. else:
  21. item_type = 'all'
  22. if 'query' in request.GET:
  23. query = bleach.clean(request.GET['query'])
  24. else:
  25. query = ''
  26. if 'tag' in request.GET:
  27. pl_tag = bleach.clean(request.GET['tag'])
  28. else:
  29. pl_tag = ''
  30. if 'channel' in request.GET:
  31. vid_channel_name = bleach.clean(request.GET['channel'])
  32. else:
  33. vid_channel_name = ''
  34. return render(
  35. request, 'search_untube_page.html', {
  36. 'playlists': request.user.playlists.all(),
  37. 'mode': mode,
  38. 'item_type': item_type,
  39. 'query': query,
  40. 'pl_tag': pl_tag,
  41. 'vid_channel_name': vid_channel_name
  42. }
  43. )
  44. else:
  45. return redirect('home')
  46. @login_required
  47. @require_POST
  48. def search_UnTube(request):
  49. search_query = bleach.clean(request.POST['search'])
  50. if request.POST['search-settings'] == 'playlists':
  51. playlist_type = bleach.clean(request.POST['playlistsType'])
  52. all_playlists = request.user.playlists.filter(is_in_db=True)
  53. if playlist_type == 'Favorite':
  54. all_playlists = all_playlists.filter(is_favorite=True)
  55. elif playlist_type == 'Watching':
  56. all_playlists = all_playlists.filter(marked_as='watching')
  57. elif playlist_type == 'Plan to Watch':
  58. all_playlists = all_playlists.filter(marked_as='plan-to-watch')
  59. elif playlist_type == 'Owned':
  60. all_playlists = all_playlists.filter(is_user_owned=True)
  61. elif playlist_type == 'Imported':
  62. all_playlists = all_playlists.filter(is_user_owned=False)
  63. elif playlist_type == 'Mix':
  64. all_playlists = all_playlists.filter(is_yt_mix=True)
  65. if 'playlist-tags' in request.POST:
  66. tags = [bleach.clean(t) for t in request.POST.getlist('playlist-tags')]
  67. for tag in tags:
  68. all_playlists = all_playlists.filter(tags__name=tag)
  69. playlists = all_playlists.filter(Q(name__istartswith=search_query) | Q(user_label__istartswith=search_query))
  70. if not playlists.exists():
  71. playlists = all_playlists.filter(Q(name__icontains=search_query) | Q(user_label__icontains=search_query))
  72. if search_query.strip() == '':
  73. playlists = all_playlists
  74. order_by = bleach.clean(request.POST['sortPlaylistsBy'])
  75. if order_by == 'recently-accessed':
  76. playlists = playlists.order_by('-updated_at')
  77. elif order_by == 'playlist-duration-in-seconds':
  78. playlists = playlists.order_by('-playlist_duration_in_seconds')
  79. elif order_by == 'video-count':
  80. playlists = playlists.order_by('-video_count')
  81. return HttpResponse(
  82. loader.get_template('intercooler/search_untube_results.html').render({
  83. 'playlists': playlists,
  84. 'view_mode': 'playlists',
  85. 'search_query': search_query,
  86. 'playlist_type': playlist_type
  87. })
  88. )
  89. else:
  90. videos_type = bleach.clean(request.POST['videosType'])
  91. all_videos = request.user.videos.filter(is_unavailable_on_yt=False)
  92. if videos_type == 'Liked':
  93. all_videos = all_videos.filter(liked=True)
  94. elif videos_type == 'Favorite':
  95. all_videos = all_videos.filter(is_favorite=True)
  96. elif videos_type == 'Watched':
  97. all_videos = all_videos.filter(is_marked_as_watched=True)
  98. elif videos_type == 'Planned to Watch':
  99. all_videos = all_videos.filter(is_planned_to_watch=True)
  100. elif videos_type == 'Unavailable':
  101. all_videos = all_videos.filter(Q(is_unavailable_on_yt=False) & Q(was_deleted_on_yt=True))
  102. if 'channel-names' in request.POST:
  103. channels = [bleach.clean(name) for name in request.POST.getlist('channel-names')]
  104. all_videos = all_videos.filter(channel_name__in=channels)
  105. videos = all_videos.filter(Q(name__istartswith=search_query) | Q(user_label__istartswith=search_query))
  106. if not videos.exists():
  107. videos = all_videos.filter(Q(name__icontains=search_query) | Q(user_label__icontains=search_query))
  108. if search_query.strip() == '':
  109. videos = all_videos
  110. order_by = bleach.clean(request.POST['sortVideosBy'])
  111. if order_by == 'recently-accessed':
  112. videos = videos.order_by('-updated_at')
  113. elif order_by == 'video-duration-in-seconds':
  114. videos = videos.order_by('-duration_in_seconds')
  115. elif order_by == 'most-liked':
  116. videos = videos.order_by('-like_count')
  117. elif order_by == 'most-views':
  118. videos = videos.order_by('-view_count')
  119. elif order_by == 'date-uploaded':
  120. videos = videos.order_by('-published_at')
  121. if 'has-cc' in request.POST:
  122. videos = videos.filter(has_cc=True)
  123. if 'playlist-ids' in request.POST:
  124. playlist_ids = [bleach.clean(pl_id) for pl_id in request.POST.getlist('playlist-ids')]
  125. videos = videos.filter(playlists__playlist_id__in=playlist_ids)
  126. return HttpResponse(
  127. loader.get_template('intercooler/search_untube_results.html').render({
  128. 'videos': videos,
  129. 'view_mode': 'videos',
  130. 'videos_type': videos_type,
  131. 'search_query': search_query
  132. })
  133. )
  134. @login_required
  135. @require_POST
  136. def search_library(request, library_type):
  137. # print_(request.POST) # prints <QueryDict: {'search': ['aa']}>
  138. search_query = bleach.clean(request.POST['search'])
  139. watching = False
  140. playlists = None
  141. if library_type == 'all':
  142. try:
  143. playlists = request.user.playlists.all().filter(
  144. Q(is_in_db=True)
  145. ).filter(Q(name__startswith=search_query) | Q(user_label__startswith=search_query))
  146. except Exception:
  147. playlists = request.user.playlists.all().filter(is_in_db=True)
  148. elif library_type == 'user-owned': # YT playlists owned by user
  149. try:
  150. playlists = request.user.playlists.filter(Q(is_user_owned=True) & Q(is_in_db=True)).filter(
  151. Q(name__startswith=search_query) | Q(user_label__startswith=search_query)
  152. )
  153. except Exception:
  154. playlists = request.user.playlists.filter(Q(is_user_owned=True) & Q(is_in_db=True))
  155. elif library_type == 'imported': # YT playlists (public) owned by others
  156. try:
  157. playlists = request.user.playlists.filter(Q(is_user_owned=False) & Q(is_in_db=True)).filter(
  158. Q(name__startswith=search_query) | Q(user_label__startswith=search_query)
  159. )
  160. except Exception:
  161. playlists = request.user.playlists.filter(Q(is_user_owned=True) & Q(is_in_db=True))
  162. elif library_type == 'favorites': # YT playlists (public) owned by others
  163. try:
  164. playlists = request.user.playlists.filter(Q(is_favorite=True) & Q(is_in_db=True)).filter(
  165. Q(name__startswith=search_query) | Q(user_label__startswith=search_query)
  166. )
  167. except Exception:
  168. playlists = request.user.playlists.filter(Q(is_favorite=True) & Q(is_in_db=True))
  169. elif library_type in ['watching', 'plan-to-watch']:
  170. try:
  171. playlists = request.user.playlists.filter(Q(marked_as=library_type) & Q(is_in_db=True)).filter(
  172. Q(name__startswith=search_query) | Q(user_label__startswith=search_query)
  173. )
  174. except Exception:
  175. playlists = request.user.playlists.all().filter(Q(marked_as=library_type) & Q(is_in_db=True))
  176. if library_type == 'watching':
  177. watching = True
  178. elif library_type == 'yt-mix': # YT playlists owned by user
  179. try:
  180. playlists = request.user.playlists.filter(Q(is_yt_mix=True) & Q(is_in_db=True)).filter(
  181. Q(name__startswith=search_query) | Q(user_label__startswith=search_query)
  182. )
  183. except Exception:
  184. playlists = request.user.playlists.filter(Q(is_yt_mix=True) & Q(is_in_db=True))
  185. elif library_type == 'unavailable-videos':
  186. try:
  187. videos = request.user.videos.filter(Q(is_unavailable_on_yt=False) & Q(was_deleted_on_yt=True)).filter(
  188. Q(name__startswith=search_query) | Q(user_label__startswith=search_query)
  189. )
  190. except Exception:
  191. videos = request.user.videos.filter(Q(is_unavailable_on_yt=False) & Q(was_deleted_on_yt=True))
  192. return HttpResponse(loader.get_template('intercooler/video_cards.html').render({'videos': videos}))
  193. return HttpResponse(
  194. loader.get_template('intercooler/playlists.html').render({
  195. 'playlists': playlists.order_by('-updated_at'),
  196. 'show_controls': True,
  197. 'watching': watching
  198. })
  199. )
  200. @login_required
  201. @require_POST
  202. def search_tagged_playlists(request, tag):
  203. search_query = bleach.clean(request.POST['search'])
  204. try:
  205. playlists = request.user.playlists.all(
  206. ).filter(Q(is_in_db=True) &
  207. Q(tags__name=tag)).filter(Q(name__startswith=search_query) | Q(user_label__startswith=search_query))
  208. except Exception:
  209. playlists = request.user.playlists.all().filter(Q(is_in_db=True) & Q(tags__name=tag)).order_by('-updated_at')
  210. return HttpResponse(loader.get_template('intercooler/playlists.html').render({'playlists': playlists}))