forked from Shimmy/Kvitter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.py
94 lines (81 loc) · 3.06 KB
/
functions.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
import os, os.path, base64, re, hashlib
import Image, gtk, pango
import StringIO, urllib
import timesince, time, cgi
import webbrowser
def open_url(menuitem, url):
webbrowser.open(url)
def updateTimeLineListStore(statuses, liststore):
liststore.clear()
for status in statuses:
im, ext = fetchImageFromUrl(status.user.profile_image_url)
pixbuf = Image_to_GdkPixbuf(im)
time_ago = timesince.timesince(time.strptime(status.created_at.replace('+0000 ','') ,"%a %b %d %H:%M:%S %Y"))
row = liststore.append()
liststore.set_value (row, 0, pixbuf)
liststore.set_value (row, 1, '<span size="medium"><b>%s</b> (%s)\n%s\n</span><span size="x-small">%s ago</span>' % (status.user.screen_name, cgi.escape(status.user.name), cgi.escape(status.text), time_ago))
liststore.set_value (row, 2, status.user.screen_name)
liststore.set_value (row, 3, status.created_at)
liststore.set_value (row, 4, status.source)
def resize_wrap(scroll, allocation, treeview, column, cell):
cell.set_property('wrap-width', allocation.width-100)
cell.set_property('wrap-mode', pango.WRAP_WORD)
def get_ext(url):
urls = os.path.splitext(url)
ext = urls[-1]
extnodot = re.findall('\.(\w{3,4})', ext)
if extnodot[0]:
return extnodot[0]
else:
return 'jpg'
def fetchImageFromUrl(url):
tmp_path = "/tmp/kvitter/"
try:
_mkdir(tmp_path)
url = "http://api.iglaset.se/resizely/crop/50x50/?url=%s" % url
ext = get_ext(url)
source = base64.urlsafe_b64encode(hashlib.sha224(url).hexdigest())
dest = "%s%s" % ('/tmp/kvitter/', source)
if not os.path.exists(dest):
datasource = urllib.urlopen(url)
im = datasource.read()
datasource.close()
f = open(dest, 'w')
f.write(im)
f.close()
im = Image.open(dest)
except Exception as e:
print "error %s" % url
im = Image.open('img/avatar.gif')
ext = 'gif'
return (im, ext)
def Image_to_GdkPixbuf (image):
file = StringIO.StringIO ()
if image.mode != "RGB":
image = image.convert("RGBA")
image.save (file, 'ppm')
contents = file.getvalue()
file.close ()
loader = gtk.gdk.PixbufLoader ()
loader.write (contents, len (contents))
pixbuf = loader.get_pixbuf ()
loader.close ()
return pixbuf
def _mkdir(newdir):
"""works the way a good mkdir should :)
- already exists, silently complete
- regular file in the way, raise an exception
- parent directory(ies) does not exist, make them as well
"""
if os.path.isdir(newdir):
pass
elif os.path.isfile(newdir):
raise OSError("a file with the same name as the desired " \
"dir, '%s', already exists." % newdir)
else:
head, tail = os.path.split(newdir)
if head and not os.path.isdir(head):
_mkdir(head)
#print "_mkdir %s" % repr(newdir)
if tail:
os.mkdir(newdir)