Repository navigation
Expand file tree
/
Copy pathfeed.py
More file actions
223 lines (162 loc) · 5.8 KB
/
Copy pathfeed.py
File metadata and controls
223 lines (162 loc) · 5.8 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import feedparser
import pygame
import urllib
import threading
from PIL import Image
from io import BytesIO
#Simple structure for storing data used
class article:
def __init__(self, title, summary, thumb_link, link, time):
self.title = title
self.summary = summary
self.thumb = ""
self.link = link
self.time = time
self.thumb_link = thumb_link
def set_thumb(self, thumb):
self.thumb = thumb
#Returns the image at url as a pygame surface
def get_image_from_url(url):
#Open image from url
img_url = urllib.request.urlopen(url)
img = BytesIO(img_url.read())
img = Image.open(img)
mode = img.mode
size = img.size
data = img.tostring()
assert mode in "RGB", "RGBA"
#Convert image url to pygame surface
surface = pygame.image.fromstring(data, size, mode)
return surface
#Returns thumbnail from entry as pygame surface
def get_thumb(entry, big=1):
entry = entry['media_thumbnail']
if len(entry) == 1:
big = 0
url = entry[big]['url']
return get_image_from_url(url)
#Retrieve data from feed, returning a list of articles
def fetch_text_data(url):
data = feedparser.parse(url)
articles = []
for entry in data.entries:
title = entry['title']
summary = entry['summary']
link = entry['link']
time = entry['published']
thumb_link = entry
articles.append( article(title, summary, thumb_link, link, time) )
return articles
#Splits a string into maximum length substrings for wrapping
def split_text(text, rect, font):
rect = pygame.Rect(rect)
i = 1
lines = []
#Iterate the string. If it's over max width, split at most recent space. Repeat
while text:
while font.size(text[:i])[0] < rect.width and i < len(text):
i += 1
if i < len(text):
i = text.rfind(" ", 0, i) + 1
lines.append(text[:i])
text = text[i:]
return lines
#Calls split_text on a string then render and blits
def draw_text(text, rect, font):
lines = split_text(text, rect, font)
y = 0
spacing = 3
font_height = font.size("Tg")[1]
#blit to screen one line at a time
for line in lines:
line = font.render(line, 1, colour)
screen.blit(line, (rect[0], rect[1]+y))
y += font_height + spacing
#Commits retrieved data to screen
#Causes flickering of pictures except top 2
#6 is placeholder atm
def update_screen(articles, offset, first=0, num=6):
count = 0
articles = articles[first:]
for article in articles:
#Render and blit to screen
if not article.thumb == "":
screen.blit(article.thumb, (MARGIN, (thumb_height + SPACING)*count + MARGIN + offset))
draw_text(article.title, (text_left,(thumb_height + SPACING)*count + offset, WIDTH - MARGIN - text_left, HEIGHT), title_font)
draw_text(article.summary, (text_left,(thumb_height + SPACING)*count + title_font_height + offset, WIDTH - MARGIN - text_left,50), sum_font)
#Only draw as many as you can have on screen
count +=1
if count >= num:
break
pygame.display.flip()
#Threading to fetch images. Speeds start-up time
#occasional flickering when scrolling as loads
class fetch_thumbs(threading.Thread):
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
global articles, offset
count = 0
print("Starting "+self.name)
for article in articles:
article.set_thumb(get_thumb(article.thumb_link))
screen.blit(article.thumb, (MARGIN, (thumb_height + SPACING)*count + MARGIN + offset))
pygame.display.flip()
count += 1
################################
#
# Main body of program
#
################################
pygame.init()
pygame.display.set_caption('Feed Me')
WIDTH=640
HEIGHT=480
MARGIN = 5 #Pixels between content and window border
SPACING = 3
title_font = pygame.font.SysFont("arial", 30)
sum_font = pygame.font.SysFont("arial", 16)
title_font_height = title_font.size("Tg")[1]
size = (WIDTH, HEIGHT)
screen = pygame.display.set_mode(size)
colour = (10,10,10)
bg_colour = (255,255,255)
screen.fill(bg_colour)
pygame.display.flip()
url = "http://feeds.bbci.co.uk/news/rss.xml"
data = feedparser.parse(url) #need to add exception handling
#useful ones:
#media_thumbnail, title, link, published, summary
big = 1
if len(data.entries[0]['media_thumbnail']) == 1:
big = 0
thumb_width = int(data.entries[0]['media_thumbnail'][big]['width'])
thumb_height = int(data.entries[0]['media_thumbnail'][big]['height'])
text_left = MARGIN + thumb_width + SPACING
count = 0
offset = 0
articles = fetch_text_data(url)
thread1 = fetch_thumbs(1, "Thread-1", 1)
thread1.start() #need to join at some point
update_screen(articles, offset)
################################
#
# Main event loop
#
################################
scrolling = False
while True:
event = pygame.event.wait()
if event.type == pygame.MOUSEMOTION and scrolling:
screen.fill(bg_colour)
if event.rel[1] != 0: #add some tolerance?
offset += event.rel[1] #add some fluidity to the effect
#velocity = rel[1]?
update_screen(articles, offset, num=len(data.entries))
elif event.type == pygame.MOUSEBUTTONDOWN:
scrolling = True
elif event.type == pygame.MOUSEBUTTONUP:
scrolling = False