tubearchivist/tubearchivist/home/views.py

641 lines
21 KiB
Python
Raw Normal View History

2021-09-05 17:10:14 +00:00
"""
Functionality:
- all views for home app
2021-09-18 10:28:16 +00:00
- process post data received from frontend via ajax
2021-09-05 17:10:14 +00:00
"""
import json
2021-09-18 13:02:54 +00:00
import urllib.parse
2021-09-05 17:10:14 +00:00
from datetime import datetime
from time import sleep
import requests
from django.http import JsonResponse
2021-09-18 13:02:54 +00:00
from django.shortcuts import redirect, render
2021-09-05 17:10:14 +00:00
from django.utils.http import urlencode
2021-09-18 13:02:54 +00:00
from django.views import View
2021-09-05 17:10:14 +00:00
from home.src.config import AppConfig
2021-09-18 13:02:54 +00:00
from home.src.download import ChannelSubscription, PendingList
2021-09-21 09:25:22 +00:00
from home.src.helper import (
get_dl_message,
get_message,
process_url_list,
set_message,
)
2021-09-18 13:02:54 +00:00
from home.src.searching import Pagination, SearchHandler
2021-09-21 09:25:22 +00:00
from home.tasks import (
download_pending,
download_single,
extrac_dl,
run_backup,
run_manual_import,
run_restore_backup,
update_subscribed,
)
2021-09-05 17:10:14 +00:00
class HomeView(View):
2021-09-21 09:25:22 +00:00
"""resolves to /
2021-09-09 09:45:46 +00:00
handle home page and video search post functionality
"""
2021-09-05 17:10:14 +00:00
CONFIG = AppConfig().config
2021-09-21 09:25:22 +00:00
ES_URL = CONFIG["application"]["es_url"]
2021-09-05 17:10:14 +00:00
def get(self, request):
2021-09-21 09:25:22 +00:00
"""return home search results"""
2021-09-05 17:10:14 +00:00
colors, sort_order, hide_watched = self.read_config()
# handle search
2021-09-21 09:25:22 +00:00
search_get = request.GET.get("search", False)
2021-09-05 17:10:14 +00:00
if search_get:
search_encoded = urllib.parse.quote(search_get)
else:
search_encoded = False
# define page size
2021-09-21 09:25:22 +00:00
page_get = int(request.GET.get("page", 0))
2021-09-05 17:10:14 +00:00
pagination_handler = Pagination(page_get, search_encoded)
2021-09-21 09:25:22 +00:00
url = self.ES_URL + "/ta_video/_search"
2021-09-05 17:10:14 +00:00
data = self.build_data(
pagination_handler, sort_order, search_get, hide_watched
)
search = SearchHandler(url, data)
videos_hits = search.get_data()
max_hits = search.max_hits
pagination_handler.validate(max_hits)
context = {
2021-09-21 09:25:22 +00:00
"videos": videos_hits,
"pagination": pagination_handler.pagination,
"sortorder": sort_order,
"hide_watched": hide_watched,
"colors": colors,
2021-09-05 17:10:14 +00:00
}
2021-09-21 09:25:22 +00:00
return render(request, "home/home.html", context)
2021-09-05 17:10:14 +00:00
@staticmethod
def build_data(pagination_handler, sort_order, search_get, hide_watched):
2021-09-21 09:25:22 +00:00
"""build the data dict for the search query"""
page_size = pagination_handler.pagination["page_size"]
page_from = pagination_handler.pagination["page_from"]
2021-09-05 17:10:14 +00:00
data = {
2021-09-21 09:25:22 +00:00
"size": page_size,
"from": page_from,
"query": {"match_all": {}},
2021-09-05 17:10:14 +00:00
"sort": [
{"published": {"order": "desc"}},
2021-09-21 09:25:22 +00:00
{"date_downloaded": {"order": "desc"}},
],
2021-09-05 17:10:14 +00:00
}
# define sort
2021-09-21 09:25:22 +00:00
if sort_order == "downloaded":
del data["sort"][0]
2021-09-05 17:10:14 +00:00
if search_get:
2021-09-21 09:25:22 +00:00
del data["sort"]
2021-09-05 17:10:14 +00:00
if hide_watched:
2021-09-21 09:25:22 +00:00
data["query"] = {"term": {"player.watched": {"value": False}}}
2021-09-05 17:10:14 +00:00
if search_get:
query = {
"multi_match": {
"query": search_get,
"fields": ["title", "channel.channel_name", "tags"],
"type": "cross_fields",
2021-09-21 09:25:22 +00:00
"operator": "and",
2021-09-05 17:10:14 +00:00
}
}
2021-09-21 09:25:22 +00:00
data["query"] = query
2021-09-05 17:10:14 +00:00
return data
@staticmethod
def read_config():
2021-09-21 09:25:22 +00:00
"""read needed values from redis"""
2021-09-05 17:10:14 +00:00
config_handler = AppConfig().config
2021-09-21 09:25:22 +00:00
colors = config_handler["application"]["colors"]
sort_order = get_message("sort_order")
hide_watched = get_message("hide_watched")
2021-09-05 17:10:14 +00:00
return colors, sort_order, hide_watched
@staticmethod
def post(request):
2021-09-21 09:25:22 +00:00
"""handle post from search form"""
2021-09-05 17:10:14 +00:00
post_data = dict(request.POST)
2021-09-21 09:25:22 +00:00
search_query = post_data["videoSearch"][0]
search_url = "/?" + urlencode({"search": search_query})
2021-09-05 17:10:14 +00:00
return redirect(search_url, permanent=True)
class AboutView(View):
2021-09-21 09:25:22 +00:00
"""resolves to /about/
2021-09-05 17:10:14 +00:00
show helpful how to information
"""
@staticmethod
def get(request):
2021-09-21 09:25:22 +00:00
"""handle http get"""
2021-09-05 17:10:14 +00:00
config = AppConfig().config
2021-09-21 09:25:22 +00:00
colors = config["application"]["colors"]
context = {"title": "About", "colors": colors}
return render(request, "home/about.html", context)
2021-09-05 17:10:14 +00:00
class DownloadView(View):
2021-09-21 09:25:22 +00:00
"""resolves to /download/
2021-09-05 17:10:14 +00:00
takes POST for downloading youtube links
"""
def get(self, request):
2021-09-21 09:25:22 +00:00
"""handle get requests"""
2021-09-05 17:10:14 +00:00
config = AppConfig().config
2021-09-21 09:25:22 +00:00
colors = config["application"]["colors"]
2021-09-21 09:25:22 +00:00
page_get = int(request.GET.get("page", 0))
pagination_handler = Pagination(page_get)
2021-09-21 09:25:22 +00:00
url = config["application"]["es_url"] + "/ta_download/_search"
data = self.build_data(pagination_handler)
search = SearchHandler(url, data, cache=False)
videos_hits = search.get_data()
max_hits = search.max_hits
if videos_hits:
2021-09-21 09:25:22 +00:00
all_pending = [i["source"] for i in videos_hits]
pagination_handler.validate(max_hits)
pagination = pagination_handler.pagination
else:
all_pending = False
pagination = False
2021-09-05 17:10:14 +00:00
context = {
2021-09-21 09:25:22 +00:00
"pending": all_pending,
"max_hits": max_hits,
"pagination": pagination,
"title": "Downloads",
"colors": colors,
2021-09-05 17:10:14 +00:00
}
2021-09-21 09:25:22 +00:00
return render(request, "home/downloads.html", context)
2021-09-05 17:10:14 +00:00
@staticmethod
def build_data(pagination_handler):
2021-09-21 09:25:22 +00:00
"""build data dict for search"""
page_size = pagination_handler.pagination["page_size"]
page_from = pagination_handler.pagination["page_from"]
data = {
2021-09-21 09:25:22 +00:00
"size": page_size,
"from": page_from,
"query": {"term": {"status": {"value": "pending"}}},
2021-09-21 09:25:22 +00:00
"sort": [{"timestamp": {"order": "desc"}}],
}
return data
2021-09-05 17:10:14 +00:00
@staticmethod
def post(request):
2021-09-21 09:25:22 +00:00
"""handle post requests"""
2021-09-05 17:10:14 +00:00
download_post = dict(request.POST)
2021-09-21 09:25:22 +00:00
if "vid-url" in download_post.keys():
url_str = download_post["vid-url"]
print("adding to queue")
2021-09-05 17:10:14 +00:00
youtube_ids = process_url_list(url_str)
if not youtube_ids:
# failed to process
print(url_str)
mess_dict = {
"status": "downloading",
"level": "error",
2021-09-21 09:25:22 +00:00
"title": "Failed to extract links.",
"message": "",
}
2021-09-21 09:25:22 +00:00
set_message("progress:download", mess_dict)
return redirect("downloads")
2021-09-05 17:10:14 +00:00
print(youtube_ids)
extrac_dl.delay(youtube_ids)
sleep(2)
2021-09-21 09:25:22 +00:00
return redirect("downloads", permanent=True)
2021-09-05 17:10:14 +00:00
class ChannelIdView(View):
2021-09-21 09:25:22 +00:00
"""resolves to /channel/<channel-id>/
2021-09-09 09:45:46 +00:00
display single channel page from channel_id
"""
2021-09-05 17:10:14 +00:00
def get(self, request, channel_id_detail):
2021-09-21 09:25:22 +00:00
"""get method"""
2021-09-05 17:10:14 +00:00
es_url, colors = self.read_config()
context = self.get_channel_videos(request, channel_id_detail, es_url)
2021-09-21 09:25:22 +00:00
context.update({"colors": colors})
return render(request, "home/channel_id.html", context)
2021-09-05 17:10:14 +00:00
@staticmethod
def read_config():
2021-09-21 09:25:22 +00:00
"""read config file"""
2021-09-05 17:10:14 +00:00
config = AppConfig().config
2021-09-21 09:25:22 +00:00
es_url = config["application"]["es_url"]
colors = config["application"]["colors"]
2021-09-05 17:10:14 +00:00
return es_url, colors
def get_channel_videos(self, request, channel_id_detail, es_url):
2021-09-21 09:25:22 +00:00
"""get channel from video index"""
page_get = int(request.GET.get("page", 0))
2021-09-05 17:10:14 +00:00
pagination_handler = Pagination(page_get)
# get data
2021-09-21 09:25:22 +00:00
url = es_url + "/ta_video/_search"
2021-09-05 17:10:14 +00:00
data = self.build_data(pagination_handler, channel_id_detail)
search = SearchHandler(url, data)
videos_hits = search.get_data()
max_hits = search.max_hits
if max_hits:
2021-09-21 09:25:22 +00:00
channel_info = videos_hits[0]["source"]["channel"]
channel_name = channel_info["channel_name"]
2021-09-05 17:10:14 +00:00
pagination_handler.validate(max_hits)
pagination = pagination_handler.pagination
else:
# get details from channel index when when no hits
channel_info, channel_name = self.get_channel_info(
channel_id_detail, es_url
)
videos_hits = False
pagination = False
context = {
2021-09-21 09:25:22 +00:00
"channel_info": channel_info,
"videos": videos_hits,
"max_hits": max_hits,
"pagination": pagination,
"title": "Channel: " + channel_name,
2021-09-05 17:10:14 +00:00
}
return context
@staticmethod
def build_data(pagination_handler, channel_id_detail):
2021-09-21 09:25:22 +00:00
"""build data dict for search"""
page_size = pagination_handler.pagination["page_size"]
page_from = pagination_handler.pagination["page_from"]
2021-09-05 17:10:14 +00:00
data = {
2021-09-21 09:25:22 +00:00
"size": page_size,
"from": page_from,
2021-09-05 17:10:14 +00:00
"query": {
"term": {"channel.channel_id": {"value": channel_id_detail}}
},
"sort": [
{"published": {"order": "desc"}},
2021-09-21 09:25:22 +00:00
{"date_downloaded": {"order": "desc"}},
],
2021-09-05 17:10:14 +00:00
}
return data
@staticmethod
def get_channel_info(channel_id_detail, es_url):
2021-09-21 09:25:22 +00:00
"""get channel info from channel index if no videos"""
url = f"{es_url}/ta_channel/_doc/{channel_id_detail}"
2021-09-05 17:10:14 +00:00
data = False
search = SearchHandler(url, data)
channel_data = search.get_data()
2021-09-21 09:25:22 +00:00
channel_info = channel_data[0]["source"]
channel_name = channel_info["channel_name"]
2021-09-05 17:10:14 +00:00
return channel_info, channel_name
class ChannelView(View):
2021-09-21 09:25:22 +00:00
"""resolves to /channel/
2021-09-09 09:45:46 +00:00
handle functionality for channel overview page, subscribe to channel,
search as you type for channel name
"""
2021-09-05 17:10:14 +00:00
def get(self, request):
2021-09-21 09:25:22 +00:00
"""handle http get requests"""
2021-09-05 17:10:14 +00:00
es_url, colors = self.read_config()
2021-09-21 09:25:22 +00:00
page_get = int(request.GET.get("page", 0))
2021-09-05 17:10:14 +00:00
pagination_handler = Pagination(page_get)
2021-09-21 09:25:22 +00:00
page_size = pagination_handler.pagination["page_size"]
page_from = pagination_handler.pagination["page_from"]
2021-09-05 17:10:14 +00:00
# get
2021-09-21 09:25:22 +00:00
url = es_url + "/ta_channel/_search"
2021-09-05 17:10:14 +00:00
data = {
2021-09-21 09:25:22 +00:00
"size": page_size,
"from": page_from,
"query": {"match_all": {}},
"sort": [{"channel_name.keyword": {"order": "asc"}}],
2021-09-05 17:10:14 +00:00
}
2021-09-21 09:25:22 +00:00
show_subed_only = get_message("show_subed_only")
2021-09-05 17:10:14 +00:00
if show_subed_only:
2021-09-21 09:25:22 +00:00
data["query"] = {"term": {"channel_subscribed": {"value": True}}}
2021-09-05 17:10:14 +00:00
search = SearchHandler(url, data)
channel_hits = search.get_data()
max_hits = search.max_hits
pagination_handler.validate(search.max_hits)
context = {
2021-09-21 09:25:22 +00:00
"channels": channel_hits,
"max_hits": max_hits,
"pagination": pagination_handler.pagination,
"show_subed_only": show_subed_only,
"title": "Channels",
"colors": colors,
2021-09-05 17:10:14 +00:00
}
2021-09-21 09:25:22 +00:00
return render(request, "home/channel.html", context)
2021-09-05 17:10:14 +00:00
@staticmethod
def read_config():
2021-09-21 09:25:22 +00:00
"""read config file"""
2021-09-05 17:10:14 +00:00
config = AppConfig().config
2021-09-21 09:25:22 +00:00
es_url = config["application"]["es_url"]
colors = config["application"]["colors"]
2021-09-05 17:10:14 +00:00
return es_url, colors
def post(self, request):
2021-09-21 09:25:22 +00:00
"""handle http post requests"""
2021-09-05 17:10:14 +00:00
subscriptions_post = dict(request.POST)
print(subscriptions_post)
subscriptions_post = dict(request.POST)
2021-09-21 09:25:22 +00:00
if "subscribe" in subscriptions_post.keys():
sub_str = subscriptions_post["subscribe"]
try:
youtube_ids = process_url_list(sub_str)
self.subscribe_to(youtube_ids)
except ValueError:
2021-09-21 09:25:22 +00:00
print("parsing subscribe ids failed!")
print(sub_str)
sleep(1)
2021-09-21 09:25:22 +00:00
return redirect("channel", permanent=True)
@staticmethod
def subscribe_to(youtube_ids):
2021-09-21 09:25:22 +00:00
"""process the subscribe ids"""
for youtube_id in youtube_ids:
2021-09-21 09:25:22 +00:00
if youtube_id["type"] == "video":
to_sub = youtube_id["url"]
vid_details = PendingList().get_youtube_details(to_sub)
2021-09-21 09:25:22 +00:00
channel_id_sub = vid_details["channel_id"]
elif youtube_id["type"] == "channel":
channel_id_sub = youtube_id["url"]
2021-09-05 17:10:14 +00:00
else:
2021-09-21 09:25:22 +00:00
raise ValueError("failed to subscribe to: " + youtube_id)
2021-09-05 17:10:14 +00:00
ChannelSubscription().change_subscribe(
channel_id_sub, channel_subscribed=True
)
2021-09-21 09:25:22 +00:00
print("subscribed to: " + channel_id_sub)
2021-09-05 17:10:14 +00:00
class VideoView(View):
2021-09-21 09:25:22 +00:00
"""resolves to /video/<video-id>/
2021-09-09 09:45:46 +00:00
display details about a single video
"""
2021-09-05 17:10:14 +00:00
def get(self, request, video_id):
2021-09-21 09:25:22 +00:00
"""get single video"""
2021-09-05 17:10:14 +00:00
es_url, colors = self.read_config()
2021-09-21 09:25:22 +00:00
url = f"{es_url}/ta_video/_doc/{video_id}"
2021-09-05 17:10:14 +00:00
data = None
look_up = SearchHandler(url, data)
video_hit = look_up.get_data()
2021-09-21 09:25:22 +00:00
video_data = video_hit[0]["source"]
video_title = video_data["title"]
context = {"video": video_data, "title": video_title, "colors": colors}
return render(request, "home/video.html", context)
2021-09-05 17:10:14 +00:00
@staticmethod
def read_config():
2021-09-21 09:25:22 +00:00
"""read config file"""
2021-09-05 17:10:14 +00:00
config = AppConfig().config
2021-09-21 09:25:22 +00:00
es_url = config["application"]["es_url"]
colors = config["application"]["colors"]
2021-09-05 17:10:14 +00:00
return es_url, colors
class SettingsView(View):
2021-09-21 09:25:22 +00:00
"""resolves to /settings/
2021-09-09 09:45:46 +00:00
handle the settings page, display current settings,
take post request from the form to update settings
"""
2021-09-05 17:10:14 +00:00
@staticmethod
def get(request):
2021-09-21 09:25:22 +00:00
"""read and display current settings"""
2021-09-05 17:10:14 +00:00
config = AppConfig().config
2021-09-21 09:25:22 +00:00
colors = config["application"]["colors"]
2021-09-05 17:10:14 +00:00
2021-09-21 09:25:22 +00:00
context = {"title": "Settings", "config": config, "colors": colors}
2021-09-05 17:10:14 +00:00
2021-09-21 09:25:22 +00:00
return render(request, "home/settings.html", context)
2021-09-05 17:10:14 +00:00
@staticmethod
def post(request):
2021-09-21 09:25:22 +00:00
"""handle form post to update settings"""
2021-09-05 17:10:14 +00:00
form_post = dict(request.POST)
2021-09-21 09:25:22 +00:00
del form_post["csrfmiddlewaretoken"]
2021-09-05 17:10:14 +00:00
print(form_post)
config_handler = AppConfig()
config_handler.update_config(form_post)
2021-09-21 09:25:22 +00:00
return redirect("settings", permanent=True)
2021-09-05 17:10:14 +00:00
def progress(request):
# pylint: disable=unused-argument
2021-09-21 09:25:22 +00:00
"""endpoint for download progress ajax calls"""
2021-09-05 17:10:14 +00:00
config = AppConfig().config
2021-09-21 09:25:22 +00:00
cache_dir = config["application"]["cache_dir"]
2021-09-05 17:10:14 +00:00
json_data = get_dl_message(cache_dir)
return JsonResponse(json_data)
def process(request):
2021-09-21 09:25:22 +00:00
"""handle all the buttons calls via POST ajax"""
if request.method == "POST":
2021-09-05 17:10:14 +00:00
post_dict = json.loads(request.body.decode())
post_handler = PostData(post_dict)
if post_handler.to_do:
task_result = post_handler.run_task()
return JsonResponse(task_result)
2021-09-21 09:25:22 +00:00
return JsonResponse({"success": False})
2021-09-05 17:10:14 +00:00
class PostData:
2021-09-21 09:25:22 +00:00
"""generic post handler from process route"""
2021-09-05 17:10:14 +00:00
CONFIG = AppConfig().config
2021-09-21 09:25:22 +00:00
ES_URL = CONFIG["application"]["es_url"]
2021-09-05 17:10:14 +00:00
VALID_KEYS = [
2021-09-21 09:25:22 +00:00
"watched",
"rescan_pending",
"ignore",
"dl_pending",
"unsubscribe",
"sort_order",
"hide_watched",
"show_subed_only",
"channel-search",
"video-search",
"dlnow",
"manual-import",
"db-backup",
"db-restore",
2021-09-05 17:10:14 +00:00
]
def __init__(self, post_dict):
self.post_dict = post_dict
self.to_do = self.validate()
def validate(self):
2021-09-21 09:25:22 +00:00
"""validate the post_dict"""
2021-09-05 17:10:14 +00:00
to_do = []
for key, value in self.post_dict.items():
if key in self.VALID_KEYS:
2021-09-21 09:25:22 +00:00
task_item = {"task": key, "status": value}
2021-09-05 17:10:14 +00:00
print(task_item)
to_do.append(task_item)
else:
2021-09-21 09:25:22 +00:00
print(key + " not a valid key")
2021-09-05 17:10:14 +00:00
return to_do
def run_task(self):
2021-09-21 09:25:22 +00:00
"""run through the tasks to do"""
2021-09-05 17:10:14 +00:00
for item in self.to_do:
2021-09-21 09:25:22 +00:00
task = item["task"]
if task == "watched":
youtube_id = item["status"]
2021-09-05 17:10:14 +00:00
self.parse_watched(youtube_id)
2021-09-21 09:25:22 +00:00
elif task == "rescan_pending":
print("rescan subscribed channels")
2021-09-05 17:10:14 +00:00
update_subscribed.delay()
2021-09-21 09:25:22 +00:00
elif task == "ignore":
print("ignore video")
2021-09-05 17:10:14 +00:00
handler = PendingList()
2021-09-21 09:25:22 +00:00
ignore_list = item["status"]
2021-09-05 17:10:14 +00:00
handler.ignore_from_pending([ignore_list])
2021-09-21 09:25:22 +00:00
elif task == "dl_pending":
print("download pending")
2021-09-05 17:10:14 +00:00
download_pending.delay()
2021-09-21 09:25:22 +00:00
elif task == "unsubscribe":
channel_id_unsub = item["status"]
print("unsubscribe from " + channel_id_unsub)
2021-09-05 17:10:14 +00:00
ChannelSubscription().change_subscribe(
channel_id_unsub, channel_subscribed=False
)
2021-09-21 09:25:22 +00:00
elif task == "sort_order":
sort_order = item["status"]
set_message("sort_order", sort_order, expire=False)
elif task == "hide_watched":
hide_watched = bool(int(item["status"]))
print(item["status"])
set_message("hide_watched", hide_watched, expire=False)
elif task == "show_subed_only":
show_subed_only = bool(int(item["status"]))
2021-09-05 17:10:14 +00:00
print(show_subed_only)
2021-09-21 09:25:22 +00:00
set_message("show_subed_only", show_subed_only, expire=False)
elif task == "channel-search":
search_query = item["status"]
print("searching for: " + search_query)
2021-09-05 17:10:14 +00:00
search_results = self.search_channels(search_query)
return search_results
2021-09-21 09:25:22 +00:00
elif task == "video-search":
search_query = item["status"]
print("searching for: " + search_query)
2021-09-05 17:10:14 +00:00
search_results = self.search_videos(search_query)
return search_results
2021-09-21 09:25:22 +00:00
elif task == "dlnow":
youtube_id = item["status"]
print("downloading: " + youtube_id)
download_single.delay(youtube_id=youtube_id)
2021-09-21 09:25:22 +00:00
elif task == "manual-import":
print("starting manual import")
run_manual_import.delay()
2021-09-21 09:25:22 +00:00
elif task == "db-backup":
print("backing up database")
run_backup.delay()
2021-09-21 09:25:22 +00:00
elif task == "db-restore":
print("restoring index from backup zip")
run_restore_backup.delay()
2021-09-21 09:25:22 +00:00
return {"success": True}
2021-09-05 17:10:14 +00:00
def search_channels(self, search_query):
2021-09-21 09:25:22 +00:00
"""fancy searching channels as you type"""
url = self.ES_URL + "/ta_channel/_search"
2021-09-05 17:10:14 +00:00
data = {
"size": 10,
"query": {
"multi_match": {
"query": search_query,
"type": "bool_prefix",
"fields": [
"channel_name.search_as_you_type",
"channel_name._2gram",
2021-09-21 09:25:22 +00:00
"channel_name._3gram",
],
2021-09-05 17:10:14 +00:00
}
2021-09-21 09:25:22 +00:00
},
2021-09-05 17:10:14 +00:00
}
look_up = SearchHandler(url, data, cache=False)
search_results = look_up.get_data()
2021-09-21 09:25:22 +00:00
return {"results": search_results}
2021-09-05 17:10:14 +00:00
def search_videos(self, search_query):
2021-09-21 09:25:22 +00:00
"""fancy searching videos as you type"""
url = self.ES_URL + "/ta_video/_search"
2021-09-05 17:10:14 +00:00
data = {
"size": 10,
"query": {
"multi_match": {
"query": search_query,
"type": "bool_prefix",
"fields": [
"title.search_as_you_type",
"title._2gram",
2021-09-21 09:25:22 +00:00
"title._3gram",
],
2021-09-05 17:10:14 +00:00
}
2021-09-21 09:25:22 +00:00
},
2021-09-05 17:10:14 +00:00
}
look_up = SearchHandler(url, data, cache=False)
search_results = look_up.get_data()
2021-09-21 09:25:22 +00:00
return {"results": search_results}
2021-09-05 17:10:14 +00:00
def parse_watched(self, youtube_id):
2021-09-21 09:25:22 +00:00
"""marked as watched based on id type"""
2021-09-05 17:10:14 +00:00
es_url = self.ES_URL
2021-09-21 09:25:22 +00:00
id_type = process_url_list([youtube_id])[0]["type"]
2021-09-05 17:10:14 +00:00
stamp = int(datetime.now().strftime("%s"))
2021-09-21 09:25:22 +00:00
if id_type == "video":
2021-09-05 17:10:14 +00:00
stamp = int(datetime.now().strftime("%s"))
2021-09-21 09:25:22 +00:00
url = self.ES_URL + "/ta_video/_update/" + youtube_id
2021-09-05 17:10:14 +00:00
source = {
"doc": {"player": {"watched": True, "watched_date": stamp}}
}
request = requests.post(url, json=source)
if not request.ok:
print(request.text)
2021-09-21 09:25:22 +00:00
elif id_type == "channel":
headers = {"Content-type": "application/json"}
2021-09-05 17:10:14 +00:00
data = {
"description": youtube_id,
"processors": [
{"set": {"field": "player.watched", "value": True}},
2021-09-21 09:25:22 +00:00
{"set": {"field": "player.watched_date", "value": stamp}},
],
2021-09-05 17:10:14 +00:00
}
payload = json.dumps(data)
2021-09-21 09:25:22 +00:00
url = es_url + "/_ingest/pipeline/" + youtube_id
2021-09-05 17:10:14 +00:00
request = requests.put(url, data=payload, headers=headers)
if not request.ok:
print(request.text)
# apply pipeline
must_list = [
{"term": {"channel.channel_id": {"value": youtube_id}}},
2021-09-21 09:25:22 +00:00
{"term": {"player.watched": {"value": False}}},
2021-09-05 17:10:14 +00:00
]
data = {"query": {"bool": {"must": must_list}}}
payload = json.dumps(data)
2021-09-21 09:25:22 +00:00
url = f"{es_url}/ta_video/_update_by_query?pipeline={youtube_id}"
2021-09-05 17:10:14 +00:00
request = requests.post(url, data=payload, headers=headers)
if not request.ok:
print(request.text)