-
Notifications
You must be signed in to change notification settings - Fork 46
/
star_wars_gif.py
148 lines (121 loc) · 4.88 KB
/
star_wars_gif.py
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
143
144
145
146
147
148
#!env python
import ast
import configparser
import pysrt
import urwid
from make_gifs import make_gif, check_config
config = configparser.ConfigParser()
config.read("config.cfg")
config.sections()
(ffmpeg_path, vlc_path, movies, slugs) = check_config("config.cfg")
selected_movie = None
index = 0
subtitle = ""
def menu(title, movies):
body = [urwid.Text(title), urwid.Divider()]
button = urwid.Button('All movies (might take a long time)')
urwid.connect_signal(button, 'click', item_chosen, 'all')
body.append(urwid.AttrMap(button, None, focus_map='reversed'))
for m in movies:
button = urwid.Button(m['title'])
urwid.connect_signal(button, 'click', item_chosen, m)
body.append(urwid.AttrMap(button, None, focus_map='reversed'))
return urwid.ListBox(urwid.SimpleFocusListWalker(body))
def item_chosen(button, movie):
global selected_movie
if movie == 'all':
response = urwid.Text([u'You chose ', 'all movies', u'\n'])
else:
response = urwid.Text([u'You chose ', movie['title'], u'\n'])
selected_movie = movie
done = urwid.Button(u'Ok')
urwid.connect_signal(done, 'click', search)
main.original_widget = urwid.Filler(urwid.Pile(
[response, urwid.AttrMap(done, None, focus_map='reversed')]))
# TODO: Add enter as "send" -> QuestionBox in Tutorial
def search(button):
edit = urwid.Edit("Search quotes: ")
done = urwid.Button(u'Search')
urwid.connect_signal(done, 'click', find_quotes, edit)
exit_button = urwid.Button(u'Exit')
urwid.connect_signal(exit_button, 'click', exit)
main.original_widget = urwid.Filler(urwid.Pile([
edit,
urwid.AttrMap(done, None, focus_map='reversed'),
urwid.AttrMap(exit_button, None, focus_map='reversed')]))
def find_quotes(button, edit):
if selected_movie == 'all':
subs = []
for movie in movies:
subs.extend([
(movie, quote) for quote in pysrt.open(movie['subtitle_path'])
])
else:
subs = [
(selected_movie,
quote) for quote in pysrt.open(selected_movie['subtitle_path'])]
search_text = edit.edit_text.lower()
def seek(item, quote):
for i in item.split(' '):
if i in quote:
return True
return False
matching = [(m, s) for (m, s) in subs if seek(search_text, s.text.lower())]
if len(matching) > 0:
body_text = "Select quote ({} matches found)".format(len(matching))
else:
body_text = "No quotes found"
body = [urwid.Text(body_text), urwid.Divider()]
for movie_tuple in matching:
(m, s) = movie_tuple
if selected_movie == 'all':
button = urwid.Button('{}: {}'.format(m['title'], s.text))
else:
button = urwid.Button(s.text)
urwid.connect_signal(
button, 'click', add_custom_subtitle, movie_tuple)
body.append(urwid.AttrMap(button, None, focus_map='reversed'))
back_button = urwid.Button('Go back')
urwid.connect_signal(back_button, 'click', search)
body.append(urwid.AttrMap(back_button, None, focus_map='reversed'))
main.original_widget = urwid.ListBox(urwid.SimpleFocusListWalker(body))
def add_custom_subtitle(button, movie_tuple):
global index, selected_movie
(m, s) = movie_tuple
index = s.index
# if we searched in all movies, we have to set proper movie now
if selected_movie == 'all':
selected_movie = m
body = [urwid.Text("Add a custom quote?"), urwid.Divider()]
yes_button = urwid.Button("Yes")
urwid.connect_signal(yes_button, 'click', enter_custom_subtitle)
body.append(urwid.AttrMap(yes_button, None, focus_map='reversed'))
no_button = urwid.Button("No")
urwid.connect_signal(no_button, 'click', generate_gif)
body.append(urwid.AttrMap(no_button, None, focus_map='reversed'))
main.original_widget = urwid.ListBox(urwid.SimpleFocusListWalker(body))
def enter_custom_subtitle(button):
subtitle = urwid.Edit("Enter custom subtitle: ")
done = urwid.Button(u'Submit')
urwid.connect_signal(done, 'click', generate_gif_with_subtitle, subtitle)
main.original_widget = urwid.Filler(urwid.Pile([
subtitle, urwid.AttrMap(done, None, focus_map='reversed')]))
def generate_gif(button):
raise urwid.ExitMainLoop()
def generate_gif_with_subtitle(button, edit):
global subtitle
subtitle = edit.edit_text
raise urwid.ExitMainLoop()
def exit(button):
global index
index = None
raise urwid.ExitMainLoop()
main = urwid.Padding(menu(u'Movie choice', movies), left=2, right=2)
top = urwid.Overlay(main, urwid.SolidFill(
u'\N{MEDIUM SHADE}'),
align='center', width=('relative', 60),
valign='middle', height=('relative', 60),
min_width=20, min_height=9)
urwid.MainLoop(top, palette=[('reversed', 'standout', '')]).run()
if index:
make_gif(selected_movie['slug'], [index], custom_subtitle=[subtitle])