comparison newsfeed.py @ 49:a9a3c55ef91c

Made CGI feed script more readable.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 20 Mar 2014 14:01:48 +0100
parents 8cc0036041cf
children f00a8a8d9225
comparison
equal deleted inserted replaced
48:0e0937880bb6 49:a9a3c55ef91c
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 import urllib2 3 import urllib2
4 import simplejson 4 import simplejson
5 import time 5 import time
6 import sys
7 6
8 # SETTINGS 7 # SETTINGS
9 8
10 url = "http://io.intevation.de/api/user/dive4elements/feed/public" 9 user = "dive4elements-test"
10 url = "http://io.intevation.de/api/user/%s/feed/public" % user
11 verb = "share" # post or share 11 verb = "share" # post or share
12 max_posts = 5 12 max_posts = 5
13 max_length = 350 13 max_length = 350
14 opacity75 = 20 14 opacity75 = 20
15 ocacity50 = 10 15 ocacity50 = 10
16 16
17 # END SETTINGS 17 # END SETTINGS
18 18
19 output = [] 19 output = []
20 20
21 postcount = 1 21 oa = output.append
22 22
23 output.append( 23 postcount = 1
24 '<ul style="list-style-type: square; margin-left:-25px;line-height: 23px;">' 24
25 ) 25 oa('<ul style="list-style-type: square; margin-left:-25px;line-height: 23px;">')
26 26
27 response = urllib2.urlopen(url) 27 response = urllib2.urlopen(url)
28 data = simplejson.load(response,'utf8') 28 data = simplejson.load(response,'utf8')
29 29
30 for item in data["items"]: 30 for item in data["items"]:
31 31
32 # only shares or posts are interesting 32 # only shares or posts are interesting
33 if item["verb"] != verb: continue 33 if item["verb"] != verb: continue
34 34
35 # max_posts 35 # max_posts
36 if postcount > max_posts: break 36 if postcount > max_posts: break
37 37
38 try: 38 try:
39 content = item["object"]["content"] 39 content = item["object"]["content"]
40 except KeyError: 40 except KeyError:
41 continue 41 continue
42 postcount+=1
43 42
44 content = content.encode('utf8') 43 postcount += 1
45 output.append("<li>") 44 content = content.encode('utf8')
45 oa("<li>")
46 46
47 if len(content) >= max_length: 47 if len(content) >= max_length:
48 output.append(content[:(max_length-opacity75)]) 48 oa(content[:(max_length-opacity75)])
49 output.append("<a href="+item["actor"]["url"]+">") 49 oa("<a href="+item["actor"]["url"]+">")
50 #output.append("<a href="+item["object"]["url"]+">") 50 #oa("<a href="+item["object"]["url"]+">")
51 output.append('<span style="opacity: 0.75;filter:Alpha(opacity=75)">' + content[(max_length-opacity75):(max_length-ocacity50)] + '</span>' + '<span style="opacity: 0.50;filter:Alpha(opacity=50)">' + content[(max_length-ocacity50):(max_length)] + '...</span></a>') 51 oa('<span style="opacity: 0.75;filter:Alpha(opacity=75)">')
52 else: 52 oa(content[(max_length-opacity75):(max_length-ocacity50)])
53 output.append(content) 53 oa('</span>')
54 oa('<span style="opacity: 0.50;filter:Alpha(opacity=50)">')
55 oa(content[(max_length-ocacity50):(max_length)])
56 oa('...</span></a>')
57 else:
58 oa(content)
54 59
55 output.append('<br>') 60 oa('<br>')
56 #output.append("<a href="+item["object"]["links"]["self"]["href"]+">more...</a>") 61 actor = item["actor"]
57 output.append('<span style="font-size:smaller">') 62 #oa("<a href="+item["object"]["links"]["self"]["href"]+">more...</a>")
58 output.append('<a href="'+item["actor"]["url"]+'">'+item["actor"]["displayName"]+'</a>') 63 oa('<span style="font-size:smaller">')
59 #output.append(item["content"]) 64 oa('<a href="'+actor["url"]+'">'+actor["displayName"]+'</a>')
60 if verb == "share": output.append(' via <a href="'+item["object"]["url"]+'">'+item["object"]["author"]["displayName"]+'</a>') 65 #oa(item["content"])
61 #if verb == "share": output.append(' via <a href="'+item["object"]["author"]["url"]+'">'+item["object"]["author"]["displayName"]+'</a>') 66 if verb == "share":
62 # 2014-01-03T10:30:02Z 67 oa(' via <a href="'+item["object"]["url"]+'">'+item["object"]["author"]["displayName"]+'</a>')
63 date=time.strptime(item["updated"],"%Y-%m-%dT%H:%M:%SZ") 68 #if verb == "share":
64 output.append(" "+(time.strftime("%d.%m.%Y um %H:%M:%S", date))) 69 # oa(' via <a href="'+item["object"]["author"]["url"]+'">'+item["object"]["author"]["displayName"]+'</a>')
65 output.append('</span>')
66 output.append("</li>")
67 70
68 output.append("</ul>") 71 # 2014-01-03T10:30:02Z
69 #output.append('<a href="'+data["author"]["url"]+'">mehr von '+data["author"]["displayName"]+'</a>' ) 72 date = time.strptime(item["updated"],"%Y-%m-%dT%H:%M:%SZ")
70 s = ''.join(output) 73 oa(time.strftime(" %d.%m.%Y um %H:%M:%S", date))
74 oa('</span>')
75 oa("</li>")
76
77 oa("</ul>")
78 #oa('<a href="'+data["author"]["url"]+'">mehr von '+data["author"]["displayName"]+'</a>' )
79 body = ''.join(output)
71 80
72 print "Content-Type: text/html;charset=utf-8" 81 print "Content-Type: text/html;charset=utf-8"
73 print 82 print
74 print s 83 print body
75 84
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)