-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__init__.py
executable file
·123 lines (107 loc) · 4.66 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import random
from os.path import join, dirname
import requests
from json_database import JsonStorageXDG
from ovos_utils.ocp import MediaType, PlaybackType
from ovos_workshop.decorators.ocp import ocp_search, ocp_featured_media
from ovos_workshop.skills.common_play import OVOSCommonPlaybackSkill
class HorrorBabbleSkill(OVOSCommonPlaybackSkill):
def __init__(self, *args, **kwargs):
self.supported_media = [MediaType.AUDIOBOOK]
self.skill_icon = join(dirname(__file__), "ui", "horrorbabble_icon.png")
self.default_bg = join(dirname(__file__), "ui", "bg.png")
self.archive = JsonStorageXDG("HorrorBabble", subfolder="OCP")
super().__init__(*args, **kwargs)
def initialize(self):
self._sync_db()
self.load_ocp_keywords()
def _sync_db(self):
bootstrap = "https://github.com/JarbasSkills/skill-horrorbabble/raw/dev/bootstrap.json"
data = requests.get(bootstrap).json()
self.archive.merge(data)
self.schedule_event(self._sync_db, random.randint(3600, 24 * 3600))
def load_ocp_keywords(self):
book_names = []
book_authors = []
for url, data in self.archive.items():
t = data["title"].split("/")[0].strip()
if " by " in t:
title, author = t.split(" by ")
title = title.replace('"', "").strip()
author = author.split("(")[0].strip()
book_names.append(title)
book_authors.append(author)
if " " in author:
book_authors += author.split(" ")
elif t.startswith('"') and t.endswith('"'):
book_names.append(t[1:-1])
else:
book_names.append(t)
self.register_ocp_keyword(MediaType.AUDIOBOOK,
"book_author",
list(set(book_authors)))
self.register_ocp_keyword(MediaType.AUDIOBOOK,
"book_name",
list(set(book_names)))
self.register_ocp_keyword(MediaType.AUDIOBOOK,
"audiobook_streaming_provider",
["HorrorBabble", "Horror Babble"])
def get_playlist(self, score=50):
return {
"match_confidence": score,
"media_type": MediaType.AUDIOBOOK,
"playlist": self.featured_media(),
"playback": PlaybackType.AUDIO,
"skill_icon": self.skill_icon,
"image": self.skill_icon,
"bg_image": self.default_bg,
"title": "Horror Babble (Playlist)",
"author": "Horror Babble"
}
@ocp_search()
def search_db(self, phrase, media_type):
base_score = 15 if media_type == MediaType.AUDIOBOOK else 0
entities = self.ocp_voc_match(phrase)
author = entities.get("book_author")
title = entities.get("book_name")
source = entities.get('audiobook_streaming_provider', '')
if source == 'HorrorBabble':
base_score += 30
candidates = list(self.archive.values())
if title or author:
if author:
base_score += 30
candidates = [video for video in candidates
if author.lower() in video["title"].lower()]
if title:
base_score += 30
candidates = [video for video in candidates
if title.lower() in video["title"].lower()]
for video in candidates:
yield {
"title": video["title"],
"author": author or "Horror Babble",
"match_confidence": min(100, base_score),
"media_type": MediaType.AUDIOBOOK,
"uri": "youtube//" + video["url"],
"playback": PlaybackType.AUDIO,
"skill_icon": self.skill_icon,
"skill_id": self.skill_id,
"image": video["thumbnail"],
"bg_image": self.default_bg
}
elif source == 'HorrorBabble':
yield self.get_playlist(min(100, base_score))
@ocp_featured_media()
def featured_media(self):
return [{
"title": video["title"],
"image": video["thumbnail"],
"match_confidence": 70,
"media_type": MediaType.AUDIOBOOK,
"uri": "youtube//" + url,
"playback": PlaybackType.AUDIO,
"skill_icon": self.skill_icon,
"bg_image": video["thumbnail"],
"skill_id": self.skill_id
} for url, video in self.archive.items()]