Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple fixes #3

Merged
merged 2 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion custom_components/medusa/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"documentation": "https://github.com/IDmedia/hass-medusa",
"requirements": [],
"dependencies": [],
"version": "1.0.0",
"version": "1.0.1",
"codeowners": [
"@IDmedia"
]
Expand Down
43 changes: 24 additions & 19 deletions custom_components/medusa/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def state(self):
return self._state

@property
def device_state_attributes(self):
def extra_state_attributes(self):
"""Return the state attributes."""
return self.data

Expand All @@ -81,20 +81,22 @@ def update(self):
card_json.append(init)

tv_shows = self.get_infos(self.protocol, self.host, self.port, self.token, self.web_root, 'future')

directory = "{0}/www/custom-lovelace/{1}/images/".format(self.base_dir, self._name)
if not os.path.exists(directory):
os.makedirs(directory)
regex_img = re.compile(r'\d+-(fanart|poster)\.jpg')
regex_img = re.compile(r'\d+-(fanart|poster|banner)\.jpg')
lst_images = list(filter(regex_img.search,
os.listdir(directory)))
del_images = list(filter(regex_img.search,
os.listdir(directory)))

for category in tv_shows["data"]:
for show in tv_shows["data"].get(category):

airdate_str = show["airdate"] + ' ' + show["airs"]
airdate_dt = datetime.strptime(airdate_str, "%Y-%m-%d %A %I:%M %p")
airdate = airdate_dt.strftime("%Y-%m-%d %H:%M:%SZ")
airdate = airdate_dt.strftime("%Y-%m-%dT%H:%M:%SZ")

number = "S" + str(show["season"]).zfill(2) + "E" + str(show["episode"]).zfill(2)

Expand All @@ -110,9 +112,9 @@ def update(self):
card_items["title"] = show["show_name"]
card_items["episode"] = show["ep_name"]
card_items["release"] = '$day, $date $time'
card_items["poster"] = self.add_poster(lst_images, directory, poster, show["indexerid"], card_items)
card_items["fanart"] = self.add_fanart(lst_images, directory, fanart, show["indexerid"], card_items)
card_items["banner"] = self.add_banner(lst_images, directory, banner, show["indexerid"], card_items)
card_items["poster"] = self.add_poster(lst_images, directory, poster, show["indexerid"], card_items, del_images)
card_items["fanart"] = self.add_fanart(lst_images, directory, fanart, show["indexerid"], card_items, del_images)
card_items["banner"] = self.add_banner(lst_images, directory, banner, show["indexerid"], card_items, del_images)

card_shows.append(card_items)

Expand All @@ -122,20 +124,21 @@ def update(self):
attributes["data"] = card_json
self._state = tv_shows["result"]
self.data = attributes
self.delete_old_tvshows(lst_images, directory)
self.delete_old_tvshows(del_images, directory)

def get_infos(self, proto, host, port, token, web_root, cmd):
url = "{0}://{1}:{2}{3}/api/{4}/?cmd={5}".format(
url = "{0}://{1}:{2}{3}/api/{4}/?cmd={5}&type=today|soon".format(
proto, host, port, web_root, token, cmd)
ifs_movies = requests.get(url).json()
return ifs_movies

def add_poster(self, lst_images, directory, poster, id, card_items):
def add_poster(self, lst_images, directory, poster, id, card_items, del_images):
if poster in lst_images:
lst_images.remove(poster)
if poster in del_images:
del_images.remove(poster)

else:
img_data = requests.get("{0}://{1}:{2}{3}/api/v1/{4}/?cmd=show.getposter&indexerid={5}".format(self.protocol, self.host, self.port, self.web_root, self.token, id))

if not img_data.status_code.__eq__(200):
_LOGGER.error(card_items)
return ""
Expand All @@ -146,9 +149,10 @@ def add_poster(self, lst_images, directory, poster, id, card_items):
_LOGGER.error("Unable to create file.")
return "/local/custom-lovelace/{0}/images/{1}".format(self._name, poster)

def add_fanart(self, lst_images, directory, fanart, id, card_items):
def add_fanart(self, lst_images, directory, fanart, id, card_items, del_images):
if fanart in lst_images:
lst_images.remove(fanart)
if fanart in del_images:
del_images.remove(fanart)
else:
img_data = requests.get("{0}://{1}:{2}{3}/api/v1/{4}/?cmd=show.getfanart&indexerid={5}".format(self.protocol, self.host, self.port, self.web_root, self.token, id))

Expand All @@ -161,11 +165,12 @@ def add_fanart(self, lst_images, directory, fanart, id, card_items):
_LOGGER.error("Unable to create file.")
return "/local/custom-lovelace/{0}/images/{1}".format(self._name, fanart)

def add_banner(self, lst_images, directory, banner, id, card_items):
def add_banner(self, lst_images, directory, banner, id, card_items, del_images):
if banner in lst_images:
lst_images.remove(banner)
if banner in del_images:
del_images.remove(banner)
else:
img_data = requests.get("{0}://{1}:{2}/api/v1/{3}/?cmd=show.getbanner&indexerid={4}".format(self.protocol, self.host, self.port, self.token, id))
img_data = requests.get("{0}://{1}:{2}/api/v1/{4}/?cmd=show.getbanner&indexerid={4}".format(self.protocol, self.host, self.port, self.web_root, self.token, id))

if not img_data.status_code.__eq__(200):
return ""
Expand All @@ -176,8 +181,8 @@ def add_banner(self, lst_images, directory, banner, id, card_items):
_LOGGER.error("Unable to create file.")
return "/local/custom-lovelace/{0}/images/{1}".format(self._name, banner)

def delete_old_tvshows(self, lst_images, directory):
for img in lst_images:
def delete_old_tvshows(self, del_images, directory):
for img in del_images:
try:
os.remove(directory + img)
_LOGGER.info("Delete finished tv show images")
Expand Down