16 from pprint
import pprint
17 from configparser
import ConfigParser
20 __title__ =
"TheTVDatabaseV4"
21 __author__ =
"Roland Ernst"
26 """lxml.etree.tostring is a bytes object in python3, and a str in python2.
28 sys.stdout.write(etostr.decode(
"utf-8"))
32 """ Parse the config read by ConfigParser."""
34 for section
in config.sections():
36 for k, v
in config[section].items():
42 from lxml
import etree
43 version = etree.XML(
u'<grabber></grabber>')
44 etree.SubElement(version,
"name").text = __title__
45 etree.SubElement(version,
"author").text = __author__
46 etree.SubElement(version,
"thumbnail").text =
'ttvdb.png'
47 etree.SubElement(version,
"command").text =
'ttvdb4.py'
48 etree.SubElement(version,
"type").text =
'television'
49 etree.SubElement(version,
"description").text = \
50 'Search and downloads metadata from TheTVDB.com (API v4)'
51 etree.SubElement(version,
"version").text = __version__
52 print_etree(etree.tostring(version, encoding=
'UTF-8', pretty_print=
True,
53 xml_declaration=
True))
63 print(
"Failed to import python lxml library.")
69 print(
"Failed to import python-requests or python-request-cache library.")
74 print(
"Failed to import MythTV bindings. Check your `configure` output "
75 "to make sure installation was not disabled due to external dependencies.")
77 from MythTV.ttvdbv4.myth4ttvdbv4
import Myth4TTVDBv4
78 from MythTV.ttvdbv4
import ttvdbv4_api
as ttvdb
80 print(
"TheTVDBv4 Script Version: ", __version__)
81 print(
"TheTVDBv4-API version: ", ttvdb.MYTHTV_TTVDBV4_API_VERSION)
82 print(
"TheTVDBv4-API file location: ", ttvdb.__file__)
85 print(
"Failed to import Py TTVDB4 library. This should have been included "
86 "with the python MythTV bindings.")
88 inipath = os.path.abspath(os.path.dirname(sys.argv[0]))
89 inifile = os.path.join(inipath,
"ttvdb4.ini")
90 config = ConfigParser()
92 config.optionxform = str
93 config.read(inifile,
'UTF-8')
95 config_version = config_dict[
'ConfigVersion'][
'TTVDBv4ConfigVersion']
97 print(
"Config version of 'ttvdb4.ini': ", config_version)
100 print(
"Failed to read the ini file 'ttvdb4.ini'. Check your installation "
101 "if such a file exists alongside this grabber script.")
103 print(
"Everything appears in order.")
109 Main executor for MythTV's ttvdb v4 grabber.
111 description =
'''A python script to retrieve metadata for TV-Shows.'''
113 parser = argparse.ArgumentParser(description=description)
115 parser.add_argument(
'-v',
'--version', action=
"store_true",
116 dest=
"version", help=
"Display version and author")
118 parser.add_argument(
'-t',
'--test', action=
"store_true", default=
False,
119 dest=
"test", help=
"Perform self-test for dependencies.")
121 parser.add_argument(
'-l',
"--language", metavar=
"LANGUAGE", default=
u'en',
122 dest=
"language", help=
"Specify language for filtering.")
124 parser.add_argument(
'-a',
"--area", metavar=
"COUNTRY", default=
None,
125 dest=
"country", help=
"Specify country for custom data.")
127 group = parser.add_mutually_exclusive_group()
129 group.add_argument(
'-M',
"--list", nargs=1,
130 dest=
"tvtitle", help=
"Get TV Shows matching 'tvtitle'.")
132 group.add_argument(
'-D',
"--data", metavar=(
"INETREF",
"SEASON",
"EPISODE"),
133 nargs=3, type=int, dest=
"tvdata",
134 help=
"Get TV-Show data for 'inetref', 'season' and 'episode.")
136 group.add_argument(
'-C',
"--collection", nargs=1, type=int, dest=
"collectionref",
137 help=
"Get Collection data for 'collectionref'.")
139 group.add_argument(
'-N',
"--numbers", metavar=(
"ARG0",
"ARG1"), nargs=2, type=str,
141 help=
"Get Season and Episode numbers: "
142 "'ARG0' can be ['series-title', 'inetref'] and "
143 "'ARG1': ['episode-title', 'iso-date-time'].")
145 parser.add_argument(
'--configure', nargs=
'?', type=str, default=
'ttvdb4.ini',
146 dest=
"inifile", help=
"Use local configuration file, defaults to "
147 "'~/.mythtv/'ttvdb4.ini'.")
149 parser.add_argument(
'--debug', action=
"store_true", default=
False, dest=
"debug",
150 help=
"Disable caching and enable raw data output.")
152 parser.add_argument(
'--jsondebug', action=
"store_true", default=
False, dest=
"jsondebug",
153 help=
"Enable raw json data output.")
155 parser.add_argument(
'--doctest', action=
"store_true", default=
False, dest=
"doctest",
156 help=
"Run doctests. You need to change to the folder where ttvdb4.py "
159 args = parser.parse_args()
168 cmd_args = vars(args)
170 print(
"0000: Init: cmd_args: ", cmd_args)
174 confdir = os.environ.get(
'MYTHCONFDIR',
'')
175 if (
not confdir)
or (confdir ==
'/'):
176 confdir = os.environ.get(
'HOME',
'')
177 if (
not confdir)
or (confdir ==
'/'):
178 print(
"Unable to find MythTV directory for grabber initialization.")
180 confdir = os.path.join(confdir,
'.mythtv')
182 cachedir = os.path.join(confdir,
'cache')
183 if not os.path.exists(cachedir):
184 os.makedirs(cachedir)
186 if not args.debug
and not args.doctest:
187 cache_name = os.path.join(cachedir,
'py3ttvdb4')
188 import requests_cache
189 requests_cache.install_cache(cache_name, backend=
'sqlite', expire_after=3600)
194 inipath = os.path.abspath(os.path.dirname(sys.argv[0]))
195 inifile = os.path.join(inipath,
"ttvdb4.ini")
197 global_config = ConfigParser()
199 global_config.optionxform = str
200 global_config.read(inifile,
'UTF-8')
203 print(
"0000: Init: Global Config File parsed successfully.")
206 print(
"0000: Init: Parsing Global Config File failed.")
209 local_config_file = os.path.join(confdir, args.inifile)
210 if os.path.isfile(local_config_file):
212 local_config = ConfigParser()
214 local_config.optionxform = str
215 local_config.read(local_config_file,
'UTF-8')
216 for section
in local_config.sections():
217 for k,v
in local_config[section].items():
218 config_dict[section][k] = v
220 print(
"0000: Init: Local Config File '%s' parsed successfully."
224 print(
"0000: Init: Parsing Local Config File failed.")
227 shutil.copy(inifile, local_config_file)
229 print(
"0000: Init: Local config file '%s' created." % local_config_file)
232 config_dict[
'auth_file'] = os.path.join(cachedir,
"py3ttvdb4_bearer.pickle")
234 cmd_args[
"config"] = config_dict
236 print(
"0000: Init: Using this configuration:")
237 pprint(cmd_args[
"config"])
242 with open(
"ttvdb4_doctests")
as f:
243 dtests =
"".join(f.readlines())
244 main.__doc__ += dtests
248 return doctest.testmod(verbose=args.debug, optionflags=doctest.ELLIPSIS)
252 from MythTV.ttvdbv4.myth4ttvdbv4
import Myth4TTVDBv4
253 from lxml
import etree
254 mttvdb = Myth4TTVDBv4(**cmd_args)
258 elif args.collectionref:
260 mttvdb.buildCollection()
267 mttvdb.buildNumbers()
269 sys.stdout.write(
'ERROR: This script must be called with one of '
270 '[-t, -v, -C, -D, -M, -N] switches.')
273 print_etree(etree.tostring(mttvdb.tree, encoding=
'UTF-8', pretty_print=
True,
274 xml_declaration=
True))
278 sys.stdout.write(
'ERROR: ' + str(sys.exc_info()[0]) +
' : '
279 + str(sys.exc_info()[1]) +
'\n')
283 if __name__ ==
"__main__":