MythTV master
tmdb3tv.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# -*- coding: UTF-8 -*-
3# ----------------------
4# Name: tmdb3tv.py
5# Python Script
6# Author: Peter Bennett
7# Purpose:
8# Frontend for the tmdb3 lookup.py script that now supports both Movies and
9# TV shows. This frontend supports TV shows.
10# Command example:
11# See help (-h) options
12#
13# License:Creative Commons GNU GPL v2
14# (http://creativecommons.org/licenses/GPL/2.0/)
15# -------------------------------------
16#
17__title__ = "TheMovieDB.org for TV V3"
18__author__ = "Peter Bennett"
19
20from optparse import OptionParser
21import sys
22import signal
23
25 err = 0
26 try:
27 import MythTV
28 except:
29 err = 1
30 print ("Failed to import MythTV bindings. Check your `configure` output "
31 "to make sure installation was not disabled due to external "
32 "dependencies")
33 try:
34 import MythTV.tmdb3
35 except:
36 err = 1
37 print ("Failed to import PyTMDB3 library. This should have been included "
38 "with the python MythTV bindings.")
39 try:
40 import lxml
41 except:
42 err = 1
43 print ("Failed to import python lxml library.")
44
45 if not err:
46 print ("Everything appears in order.")
47 return err
48
49def main(showType, command):
50
51 parser = OptionParser()
52
53 parser.add_option('-v', "--version", action="store_true", default=False,
54 dest="version", help="Display version and author")
55 parser.add_option('-t', "--test", action="store_true", default=False,
56 dest="test", help="Perform self-test for dependencies.")
57 parser.add_option('-M', "--movielist", "--list", action="store_true", default=False,
58 dest="movielist",
59 help="Get Television Series List. Needs search key.")
60 parser.add_option('-D', "--moviedata", "--data", action="store_true", default=False,
61 dest="moviedata", help="Get TV Episode data. " \
62 "Needs inetref, season and episode. ")
63 parser.add_option('-C', "--collection", action="store_true", default=False,
64 dest="collectiondata",
65 help='Get a television Series (collection) "series" level information')
66 parser.add_option("-N", "--numbers", action="store_true", default=False, dest="numbers",
67 help="Get television Season and Episode numbers. " \
68 "Needs title and subtitle or inetref and subtitle.")
69 parser.add_option('-l', "--language", metavar="LANGUAGE", default=u'en',
70 dest="language", help="Specify language for filtering.")
71 parser.add_option('-a', "--area", metavar="COUNTRY", default=None,
72 dest="country", help="Specify country for custom data.")
73 parser.add_option('--debug', action="store_true", default=False,
74 dest="debug", help=("Disable caching and enable raw "
75 "data output."))
76
77 opts, args = parser.parse_args()
78
79 from MythTV.tmdb3.lookup import timeouthandler
80 signal.signal(signal.SIGALRM, timeouthandler)
81 signal.alarm(180)
82
83 if opts.test:
84 return performSelfTest()
85
86 from MythTV.tmdb3 import set_key, set_cache, set_locale
87 set_key('c27cb71cff5bd76e1a7a009380562c62')
88
89 if opts.debug:
90 import MythTV.tmdb3
91 MythTV.tmdb3.request.DEBUG = True
92 set_cache(engine='null')
93 else:
94 import os
95 confdir = os.environ.get('MYTHCONFDIR', '')
96 if (not confdir) or (confdir == '/'):
97 confdir = os.environ.get('HOME', '')
98 if (not confdir) or (confdir == '/'):
99 print ("Unable to find MythTV directory for metadata cache.")
100 return 1
101 confdir = os.path.join(confdir, '.mythtv')
102 cachedir = os.path.join(confdir, 'cache')
103 if not os.path.exists(cachedir):
104 os.makedirs(cachedir)
105 cachepath = os.path.join(cachedir, 'pytmdb3.cache')
106 set_cache(engine='file', filename=cachepath)
107
108 if opts.language:
109 set_locale(language=opts.language, fallthrough=True)
110 if opts.country:
111 set_locale(country=opts.country, fallthrough=True)
112
113 if (not opts.version):
114 if (len(args) < 1) or (args[0] == ''):
115 sys.stdout.write('ERROR: tmdb3.py requires at least one non-empty argument.\n')
116 return 1
117
118 if opts.moviedata and len(args) < 3:
119 sys.stdout.write('ERROR: tmdb3.py -D requires three non-empty arguments.\n')
120 return 1
121
122 from MythTV.tmdb3.lookup import buildVersion, buildTVList, \
123 buildEpisode, buildTVSeries, print_etree
124 try:
125 xml = None
126 if opts.version:
127 xml = buildVersion(showType, command)
128
129 elif opts.movielist:
130 xml = buildTVList(args[0], opts)
131
132 elif opts.numbers:
133 xml = buildEpisode(args[0:2], opts)
134
135 elif opts.moviedata:
136 xml = buildEpisode(args[0:3], opts)
137
138 elif opts.collectiondata:
139 xml = buildTVSeries(args[0], opts)
140
141 # if a number is returned, it is an error code return
142 if isinstance(xml,int):
143 return xml
144
145 if xml:
146 print_etree(xml)
147 else:
148 return 1
149
150 except RuntimeError as exc:
151 sys.stdout.write('ERROR: ' + str(exc) + ' exception')
152 import traceback
153 traceback.print_exc()
154 return 1
155
156 return 0
157
158if __name__ == '__main__':
159 sys.exit(main("television",'tmdb3tv.py'))
def buildVersion()
Definition: culrcwrap.py:101
def main(showType, command)
Definition: tmdb3tv.py:49
def performSelfTest()
Definition: tmdb3tv.py:24
def print_etree(etostr)
Definition: ttvdb4.py:25