1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
#!/usr/bin/env python
import os
import sys
import urwid
import tmdbsimple as tmdb
from pprint import pprint
def run_async(func):
from threading import Thread
from functools import wraps
@wraps(func)
def async_func(*args, **kwargs):
func_hl = Thread(target = func, args = args, kwargs = kwargs)
func_hl.start()
return func_hl
return async_func
def read_key():
if "TMDB_KEY" in os.environ.keys():
return os.environ["TMDB_KEY"]
if "XDG_CONFIG_HOME" in os.environ.keys():
cfg_home = os.environ["XDG_CONFIG_HOME"]
else:
cfg_home = os.path.join(os.path.expanduser("~"), ".config")
if os.path.exists(os.path.join(cfg_home, "tmdbkey")):
return open(os.path.join(cfg_home, "tmdbkey"), "r").read().strip()
if os.path.exists(os.path.join(os.path.expanduser("~"), ".tmdbkey")):
return open(os.path.exists(os.path.join(os.path.expanduser("~"), ".tmdbkey"))).read().strip()
raise Exception("No TheMovieDB Key defined. Set Env. var. TMDB_KEY or write config key")
class FocusableFrame(urwid.Frame):
def keypress(self,size,key):
x=('header', 'footer', 'body')
if key == "tab":
i = x.index(self.focus_position)
self.set_focus(x[(i+1)%3])
else:
self.__super.keypress(size,key)
class SearchField(urwid.Edit):
signals = ["activate"]
def keypress(self,size,key):
if key == "enter":
self._emit("activate")
else:
self.__super.keypress(size,key)
class UI(urwid.WidgetWrap):
def __init__(self, term, search_service, loop=None):
self.edit_search = SearchField(("bold", u"Search for: "), term)
header = urwid.Pile([self.edit_search, urwid.Divider(), urwid.Text(("bold", u"Results:"))])
self.model = urwid.SimpleFocusListWalker([])
self.listbox = urwid.ListBox(self.model)
btn_quit = urwid.Button(("button", u"Exit"))
self.btn_apply = urwid.Button(("button", u"Apply"))
self.description = urwid.Text(u"", align='center')
self.btns = urwid.Columns([btn_quit, self.btn_apply])
footer = urwid.Pile([self.description, self.btns])
self.view = FocusableFrame(self.listbox, header=header, footer=footer, focus_part='header')
super(UI, self).__init__(self.view)
self.search_service = search_service
self.loop = loop
urwid.connect_signal(btn_quit, 'click', lambda *a: sys.exit(0))
urwid.connect_signal(self.edit_search, 'activate', self.search)
def search(self, *_):
@run_async
def query(*_):
results = self.search_service.search(self.edit_search.edit_text)
self.fill_results(self.loop, results)
self.view.set_focus("body")
while len(self.model) > 0: self.model.pop()
self.description.set_text(u"Searching for {}".format(self.edit_search.edit_text))
query()
def fill_results(self, loop, data):
for result in data:
btn = urwid.Button(u"{release_date} - {title}".format(**result))
urwid.connect_signal(btn, 'click', self.select_movie, result["id"])
self.model.append(btn)
loop.draw_screen()
def select_movie(self, src, movie_id):
@run_async
def query(pos):
try:
info = self.search_service.info(movie_id)
info["year"] = info["release_date"].split("-")[0]
info["filename"] = u"{title} ({year}) #{imdb_id}".format(**info)
self.model.remove( self.model[pos] )
btn = urwid.Button(u"{title} ({year}) #{imdb_id}".format(**info))
urwid.connect_signal(btn, 'click', self.rename, info)
self.model.insert(pos, btn)
self.description.set_text(('description',info["overview"]))
except:
self.description.set_text(('error', "Request failed"))
finally:
self.loop.draw_screen()
pos = self.listbox.focus_position
query(pos)
def rename(self, src, info):
def rename(*_):
print info["filename"]
sys.exit(1)
self.description.set_text([
("bold", u"Rename "),
("underline", u"foo"),
" to ",
("underline", info["filename"]),
" ?"])
self.view.set_focus("footer")
self.btns.set_focus(self.btn_apply)
urwid.connect_signal(self.btn_apply, 'click', rename)
class Search(object):
def __init__(self):
self.tmdb_search = tmdb.Search()
def search(self, term):
return self.tmdb_search.movie(query=term)["results"]
def info(self, id):
return tmdb.Movies(id).info()
if __name__ == "__main__":
tmdb.API_KEY = read_key()
palette = [
('bold', 'default,bold', 'default', 'bold'),
('button', 'default,bold', 'default', 'bold', '', 'dark blue'),
('underline', 'default,underline', 'default', 'underline'),
('description', '', '', '', 'light gray', '')]
args = sys.argv[1:]
ui = UI(len(args) > 0 and " ".join(args) or "", Search())
loop = urwid.MainLoop(ui, palette)
loop.screen.set_terminal_properties(colors=256)
ui.loop = loop
loop.run()
|