Last active
February 15, 2023 12:08
-
-
Save halspg/556d9343c933a9e8da216efa87aa6806 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! python3 | |
# coding: utf-8 | |
import re | |
import requests | |
class iTunesLink(): | |
def __init__(self, search_word, country="JP", limit=20): | |
self.country = country | |
self.limit = limit | |
pattern = re.compile(r"\s| ") | |
splitted_word = pattern.split(search_word, maxsplit=1) | |
self.media, self.entity = self._set_abbr(splitted_word[0].lower()) | |
self.term = splitted_word[1] | |
self.params = { | |
"term": self.term, | |
"country": self.country, | |
"media": self.media, | |
"entity": self.entity, | |
"limit": self.limit | |
} | |
def _set_abbr(self, abbr): | |
match abbr: | |
case "iphone" | "ipad" | "mac": | |
self.title_key = ["trackCensoredName"] | |
self.url_key = "trackViewUrl" | |
if abbr == "iphone": | |
return ["software", "software"] | |
elif abbr == "ipad": | |
return ["software", "iPadSoftware"] | |
else: | |
return ["software", "macSoftware"] | |
case "movie" | "映画": | |
self.title_key = ["trackCensoredName"] | |
self.url_key = "trackViewUrl" | |
return ["movie", "movie"] | |
case "podcast" | "ポッドキャスト": | |
self.title_key = ["collectionCensoredName"] | |
self.url_key = "collectionViewUrl" | |
return ["podcast", "podcast"] | |
case "song" | "曲" | "musicvideo" | "mv": | |
self.title_key = ["artistName", "trackCensoredName", "collectionCensoredName"] | |
self.url_key = "trackViewUrl" | |
if abbr in ["song", "曲"]: | |
return ["music", "song"] | |
else: | |
return ["musicVideo", "musicVideo"] | |
case "album" | "アルバム" | "audiobook" | "ab": | |
self.title_key = ["artistName", "collectionCensoredName"] | |
self.url_key = "collectionViewUrl" | |
if abbr in ["album", "アルバム"]: | |
return ["music", "album"] | |
else: | |
return ["audiobook", "audiobook"] | |
case "shortfilm" | "sf": | |
self.title_key = ["artistName", "trackCensoredName"] | |
self.url_key = "trackViewUrl" | |
return ["shortFilm", "shortFilm"] | |
case "tvshow" | "tv": | |
self.title_key = ["collectionCensoredName"] | |
self.url_key = "collectionViewUrl" | |
return ["tvShow", "tvSeason"] | |
case "ebook" | "book" | "本" | "書籍" | "漫画" | "マンガ" | "小説": | |
self.title_key = ["trackCensoredName", "artistName"] | |
self.url_key = "trackViewUrl" | |
return ["ebook", "ebook"] | |
case _: | |
self.title_key = ["trackCensoredName"] | |
self.url_key = "trackViewUrl" | |
return ["software", "software"] | |
def get_json(self): | |
j = requests.get( | |
"http://itunes.apple.com/search", | |
params=self.params | |
) | |
if j.status_code == 200 and j.json()["resultCount"] != 0: | |
return j.json()["results"] | |
else: | |
return None | |
def get_link(self, separator=" - "): | |
source = self.get_json() | |
if source: | |
link_list = [] | |
for item in source: | |
title_source = [] | |
for k in self.title_key: | |
title_source.append(item[k]) | |
link_list.append({ | |
"title": separator.join(title_source), | |
"url": item[self.url_key] | |
}) | |
return link_list | |
else: | |
return None | |
if __name__ == "__main__": | |
import dialogs | |
import console | |
from string import Template | |
import keyboard | |
kbd = keyboard.is_keyboard() | |
if kbd: | |
search_word = keyboard.get_selected_text() | |
else: | |
search_word = dialogs.input_alert("iTunesLink") | |
j = iTunesLink(search_word).get_link() | |
def markdown_escape(string): | |
rpl = str.maketrans({ | |
"[": "\[", | |
"]": "\]", | |
"(": "\(", | |
")": "\)", | |
}) | |
return string.translate(rpl) | |
markdown = "[${title}](${url})" | |
html = '<a href="${url}">${title}</a>' | |
plaintext = "${title}\n${url}" | |
out = Template(markdown) | |
if j: | |
d = dialogs.list_dialog("Result", j, False) | |
if d: | |
d["title"] = markdown_escape(d["title"]) | |
if kbd: | |
keyboard.insert_text(out.substitute(d)) | |
else: | |
print(out.substitute(d)) | |
else: | |
console.hud_alert("Exit", "error", 0.5) | |
else: | |
console.hud_alert(f"{search_word}: Not found", "error", 1.0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment