-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
122 lines (98 loc) · 3.34 KB
/
main.py
File metadata and controls
122 lines (98 loc) · 3.34 KB
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
import argparse
from Queue import Queue
from Tkinter import Tk, Label, Button, StringVar
from collections import deque
from threading import Thread
import requests
from PIL import Image, ImageTk
from Strip import Strip
from parse import DilbertParser
strips = deque()
strips_lookup = set()
idx = 0
def append_strip(url, left=False):
global idx
if url not in strips_lookup:
r = requests.get("http://dilbert.com{}".format(url))
parser = DilbertParser()
parser.feed(r.text)
if left:
strips.appendleft(parser.strip)
idx += 1
else:
strips.append(parser.strip)
strips_lookup.add(url)
print "GET: {}".format(url)
class ComicGrabber(Thread):
def __init__(self):
Thread.__init__(self, name="ComicGrabber")
self.task_queue = Queue()
def push(self, url, left):
self.task_queue.put((url, left))
def run(self):
while True:
u, d = self.task_queue.get()
append_strip(u, d)
self.task_queue.task_done()
parser = argparse.ArgumentParser(description='Dilbert browser')
parser.add_argument('date', nargs="?", default="2017-02-05", help='Start date')
args = parser.parse_args()
start_date = "/strip/{}".format(args.date)
append_strip(start_date)
class DilbertGUI:
def __init__(self, master):
self.master = master
master.title("Dilbert browser")
current = strips[0] # type: Strip
self.url_text = StringVar()
self.url_text.set(current.url)
self.url = Label(master, textvariable=self.url_text)
self.url.pack()
self.title_text = StringVar()
self.title_text.set(current.title)
self.title = Label(master, textvariable=self.title_text)
self.title.pack()
image = Image.open(current.comic)
photo = ImageTk.PhotoImage(image)
self.strip_img = Label(image=photo)
self.strip_img.image = photo # keep a reference!
self.strip_img.pack()
self.close_button = Button(master, text="Close", command=master.quit)
self.close_button.pack()
self.cg = ComicGrabber()
self.cg.setDaemon(True)
self.cg.start()
self.cg.push(current.next_strip, False)
self.cg.push(current.prev_strip, True)
self.master.bind("<Left>", lambda e, d=-1: self.change_strip(d))
self.master.bind("<Right>", lambda e, d=1: self.change_strip(d))
def change_strip(self, x):
global idx
"""
:type x: int
"""
# idx += x
# idx %= len(strips)
nidx = max(min(idx + x, len(strips) - 1), 0)
if nidx == idx:
return
idx = nidx
self.render_strip(strips[idx])
# preload
current = strips[idx]
self.cg.push(current.next_strip, False)
self.cg.push(current.prev_strip, True)
def render_strip(self, s):
"""
:type s: Strip
"""
self.url_text.set(s.url)
self.title_text.set(s.title)
img = Image.open(s.comic)
p = ImageTk.PhotoImage(img)
self.strip_img.configure(image=p)
self.strip_img.image = p
self.strip_img.pack()
root = Tk()
my_gui = DilbertGUI(root)
root.mainloop()