Last active
August 29, 2015 14:04
-
-
Save eoghain/01f08423ab648cf0a5f1 to your computer and use it in GitHub Desktop.
TVDB Favorites Cleanup - Remove favorites that are of canceled shows.
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
#!/usr/bin/env python | |
import requests # Requires install | |
import json | |
import random | |
import io | |
import itertools as IT | |
from xml.etree import ElementTree | |
from xml.etree.ElementTree import Element | |
from xml.etree.ElementTree import SubElement | |
APIKEY="F1517403DEB5DB13" | |
ACCOUNT="F0BF1634377623D0" | |
def getMirror(): | |
"""Return the URL of a random Mirror""" | |
mirrorsURL = "http://thetvdb.com/api/%s/mirrors.xml" % APIKEY | |
mirrorsXML = requests.get(mirrorsURL).text | |
root = ElementTree.fromstring(mirrorsXML) | |
mirrors = root.find("Mirror") | |
randomIndex = random.randint(1,len(mirrors)) | |
index = 0 | |
for mirror in mirrors.findall("mirrorpath"): | |
index += 1 | |
if (index == randomIndex): | |
return mirror.text | |
return "http://thetvdb.com/"; | |
def getFavorites(mirrorURL): | |
"""Return list of Accounts Favorites""" | |
favoritesURL = "%s/api/User_Favorites.php?accountid=%s" % (mirrorURL, ACCOUNT) | |
favoritesXML = requests.get(favoritesURL).text | |
favorites = ElementTree.fromstring(favoritesXML) | |
favoritesList = [] | |
# favorites = root.find("Favorites") | |
for series in favorites.findall("Series"): | |
favoritesList.append(series.text) | |
return favoritesList | |
def removeFavorite(mirrorURL, seriesId): | |
"""Remove series from list of Accounts Favorites""" | |
favoritesURL = "%s/api/User_Favorites.php?accountid=%s&type=remove&seriesid=%s" % (mirrorURL, ACCOUNT, seriesId) | |
response = requests.get(favoritesURL) | |
if (response.status_code == 200): | |
print '\033[92mRemoved!\033[0m' | |
else: | |
print '\033[91mError Removing\033[0m: [%s] %s' % (response.status_code, response.text) | |
def isSeriesEnded(mirrorURL, seriesId): | |
"""Return Series Object""" | |
seriesURL = "%s/api/%s/series/%s" % (mirrorURL, APIKEY, seriesId) | |
try: | |
seriesXML = requests.get(seriesURL).text | |
except ConnectionError, e: | |
print '\033[91mConnection Error\033[0m: skipping %s' % seriesId | |
seriesXMLText = seriesXML.encode('ascii', 'ignore') | |
try: | |
data = ElementTree.fromstring(seriesXMLText) | |
except ElementTree.ParseError as err: | |
lineno, column = err.position | |
line = next(IT.islice(io.BytesIO(seriesXMLText), lineno)) | |
caret = '{:=>{}}'.format('^', column) | |
errorMsg = '{}\n{}\n{}'.format(err, line, caret) | |
print '\033[91mError Parsing\033[0m:\n\tseriesURL: http://thetvdb.com/index.php?tab=series&id=%s\n\terror: %s' % (seriesId, errorMsg) | |
return | |
series = data.find("Series") | |
name = series.find("SeriesName") | |
status = series.find("Status") | |
color = '\033[92m' | |
if (status.text == "Ended"): | |
color = '\033[93m' | |
print "%s is %s%s\033[0m" % (name.text, color, status.text) | |
if (status.text == "Ended"): | |
while True: | |
remove = raw_input('- remove it [y/n]?') | |
if len(remove) == 1: | |
remove = remove.lower() | |
if (remove == "y"): | |
removeFavorite(mirrorURL, seriesId) | |
break | |
if (remove == "n"): | |
break | |
else: | |
print "Please enter 'y' or 'n'" | |
def main(): | |
"""Main""" | |
mirrorURL = getMirror() | |
favorites = getFavorites(mirrorURL) | |
for favorite in favorites: | |
isSeriesEnded(mirrorURL, favorite) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment