mirror of
https://github.com/tubearchivist/tubearchivist-frontend.git
synced 2024-11-25 21:20:15 +00:00
cleanup, use ElasticWrap
This commit is contained in:
parent
f802c4d596
commit
5b1c9c64de
@ -3,22 +3,15 @@ functionality:
|
|||||||
- handle watched state for videos, channels and playlists
|
- handle watched state for videos, channels and playlists
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import json
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
import requests
|
from home.src.es.connect import ElasticWrap
|
||||||
from home.src.ta.config import AppConfig
|
|
||||||
from home.src.ta.helper import UrlListParser
|
from home.src.ta.helper import UrlListParser
|
||||||
|
|
||||||
|
|
||||||
class WatchState:
|
class WatchState:
|
||||||
"""handle watched checkbox for videos and channels"""
|
"""handle watched checkbox for videos and channels"""
|
||||||
|
|
||||||
CONFIG = AppConfig().config
|
|
||||||
ES_URL = CONFIG["application"]["es_url"]
|
|
||||||
ES_AUTH = CONFIG["application"]["es_auth"]
|
|
||||||
HEADERS = {"Content-type": "application/json"}
|
|
||||||
|
|
||||||
def __init__(self, youtube_id):
|
def __init__(self, youtube_id):
|
||||||
self.youtube_id = youtube_id
|
self.youtube_id = youtube_id
|
||||||
self.stamp = int(datetime.now().strftime("%s"))
|
self.stamp = int(datetime.now().strftime("%s"))
|
||||||
@ -33,7 +26,7 @@ class WatchState:
|
|||||||
elif url_type == "playlist":
|
elif url_type == "playlist":
|
||||||
self.mark_playlist_watched()
|
self.mark_playlist_watched()
|
||||||
|
|
||||||
print(f"marked {self.youtube_id} as watched")
|
print(f"{self.youtube_id}: marked as watched")
|
||||||
|
|
||||||
def mark_as_unwatched(self):
|
def mark_as_unwatched(self):
|
||||||
"""revert watched state to false"""
|
"""revert watched state to false"""
|
||||||
@ -41,7 +34,7 @@ class WatchState:
|
|||||||
if url_type == "video":
|
if url_type == "video":
|
||||||
self.mark_vid_watched(revert=True)
|
self.mark_vid_watched(revert=True)
|
||||||
|
|
||||||
print(f"revert {self.youtube_id} as unwatched")
|
print(f"{self.youtube_id}: revert as unwatched")
|
||||||
|
|
||||||
def dedect_type(self):
|
def dedect_type(self):
|
||||||
"""find youtube id type"""
|
"""find youtube id type"""
|
||||||
@ -52,77 +45,54 @@ class WatchState:
|
|||||||
|
|
||||||
def mark_vid_watched(self, revert=False):
|
def mark_vid_watched(self, revert=False):
|
||||||
"""change watched status of single video"""
|
"""change watched status of single video"""
|
||||||
url = self.ES_URL + "/ta_video/_update/" + self.youtube_id
|
path = f"ta_video/_update/{self.youtube_id}"
|
||||||
data = {
|
data = {
|
||||||
"doc": {"player": {"watched": True, "watched_date": self.stamp}}
|
"doc": {"player": {"watched": True, "watched_date": self.stamp}}
|
||||||
}
|
}
|
||||||
if revert:
|
if revert:
|
||||||
data["doc"]["player"]["watched"] = False
|
data["doc"]["player"]["watched"] = False
|
||||||
|
|
||||||
payload = json.dumps(data)
|
response, status_code = ElasticWrap(path).post(data=data)
|
||||||
request = requests.post(
|
if status_code != 200:
|
||||||
url, data=payload, headers=self.HEADERS, auth=self.ES_AUTH
|
print(response)
|
||||||
)
|
|
||||||
if not request.ok:
|
|
||||||
print(request.text)
|
|
||||||
raise ValueError("failed to mark video as watched")
|
raise ValueError("failed to mark video as watched")
|
||||||
|
|
||||||
def mark_channel_watched(self):
|
def mark_channel_watched(self):
|
||||||
"""change watched status of every video in channel"""
|
"""change watched status of every video in channel"""
|
||||||
data = {
|
path = "ta_video/_update_by_query"
|
||||||
"query": {
|
must_list = [
|
||||||
"bool": {
|
{"term": {"channel.channel_id": {"value": self.youtube_id}}},
|
||||||
"must": [
|
|
||||||
{
|
|
||||||
"term": {
|
|
||||||
"channel.channel_id": {
|
|
||||||
"value": self.youtube_id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{"term": {"player.watched": {"value": False}}},
|
{"term": {"player.watched": {"value": False}}},
|
||||||
]
|
]
|
||||||
}
|
data = {
|
||||||
},
|
"query": {"bool": {"must": must_list}},
|
||||||
"script": {
|
"script": {
|
||||||
"source": "ctx._source.player['watched'] = true",
|
"source": "ctx._source.player['watched'] = true",
|
||||||
"lang": "painless",
|
"lang": "painless",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
payload = json.dumps(data)
|
|
||||||
url = f"{self.ES_URL}/ta_video/_update_by_query"
|
response, status_code = ElasticWrap(path).post(data=data)
|
||||||
request = requests.post(
|
if status_code != 200:
|
||||||
url, data=payload, headers=self.HEADERS, auth=self.ES_AUTH
|
print(response)
|
||||||
)
|
|
||||||
if not request.ok:
|
|
||||||
print(request.text)
|
|
||||||
raise ValueError("failed mark channel as watched")
|
raise ValueError("failed mark channel as watched")
|
||||||
|
|
||||||
def mark_playlist_watched(self):
|
def mark_playlist_watched(self):
|
||||||
"""change watched state of all videos in playlist"""
|
"""change watched state of all videos in playlist"""
|
||||||
data = {
|
path = "ta_video/_update_by_query"
|
||||||
"query": {
|
must_list = [
|
||||||
"bool": {
|
{"term": {"playlist.keyword": {"value": self.youtube_id}}},
|
||||||
"must": [
|
|
||||||
{
|
|
||||||
"term": {
|
|
||||||
"playlist.keyword": {"value": self.youtube_id}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{"term": {"player.watched": {"value": False}}},
|
{"term": {"player.watched": {"value": False}}},
|
||||||
]
|
]
|
||||||
}
|
data = {
|
||||||
},
|
"query": {"bool": {"must": must_list}},
|
||||||
"script": {
|
"script": {
|
||||||
"source": "ctx._source.player['watched'] = true",
|
"source": "ctx._source.player['watched'] = true",
|
||||||
"lang": "painless",
|
"lang": "painless",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
payload = json.dumps(data)
|
|
||||||
url = f"{self.ES_URL}/ta_video/_update_by_query"
|
response, status_code = ElasticWrap(path).post(data=data)
|
||||||
request = requests.post(
|
if status_code != 200:
|
||||||
url, data=payload, headers=self.HEADERS, auth=self.ES_AUTH
|
print(response)
|
||||||
)
|
|
||||||
if not request.ok:
|
|
||||||
print(request.text)
|
|
||||||
raise ValueError("failed mark playlist as watched")
|
raise ValueError("failed mark playlist as watched")
|
||||||
|
Loading…
Reference in New Issue
Block a user