[PATCH] Made CGI feed script more readable
Wald Commits
scm-commit at wald.intevation.org
Thu Mar 20 14:01:53 CET 2014
# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1395320508 -3600
# Node ID a9a3c55ef91c2d9afe39ad06c1e05d8de1be7ab2
# Parent 0e0937880bb69064ff63e682e1798ddac250542b
Made CGI feed script more readable.
diff -r 0e0937880bb6 -r a9a3c55ef91c newsfeed.py
--- a/newsfeed.py Thu Mar 20 13:43:48 2014 +0100
+++ b/newsfeed.py Thu Mar 20 14:01:48 2014 +0100
@@ -3,11 +3,11 @@
import urllib2
import simplejson
import time
-import sys
# SETTINGS
-url = "http://io.intevation.de/api/user/dive4elements/feed/public"
+user = "dive4elements-test"
+url = "http://io.intevation.de/api/user/%s/feed/public" % user
verb = "share" # post or share
max_posts = 5
max_length = 350
@@ -18,58 +18,67 @@
output = []
-postcount = 1
+oa = output.append
-output.append(
- '<ul style="list-style-type: square; margin-left:-25px;line-height: 23px;">'
-)
+postcount = 1
+
+oa('<ul style="list-style-type: square; margin-left:-25px;line-height: 23px;">')
response = urllib2.urlopen(url)
data = simplejson.load(response,'utf8')
for item in data["items"]:
- # only shares or posts are interesting
- if item["verb"] != verb: continue
+ # only shares or posts are interesting
+ if item["verb"] != verb: continue
- # max_posts
- if postcount > max_posts: break
+ # max_posts
+ if postcount > max_posts: break
- try:
- content = item["object"]["content"]
- except KeyError:
- continue
- postcount+=1
+ try:
+ content = item["object"]["content"]
+ except KeyError:
+ continue
- content = content.encode('utf8')
- output.append("<li>")
+ postcount += 1
+ content = content.encode('utf8')
+ oa("<li>")
- if len(content) >= max_length:
- output.append(content[:(max_length-opacity75)])
- output.append("<a href="+item["actor"]["url"]+">")
- #output.append("<a href="+item["object"]["url"]+">")
- 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>')
- else:
- output.append(content)
+ if len(content) >= max_length:
+ oa(content[:(max_length-opacity75)])
+ oa("<a href="+item["actor"]["url"]+">")
+ #oa("<a href="+item["object"]["url"]+">")
+ oa('<span style="opacity: 0.75;filter:Alpha(opacity=75)">')
+ oa(content[(max_length-opacity75):(max_length-ocacity50)])
+ oa('</span>')
+ oa('<span style="opacity: 0.50;filter:Alpha(opacity=50)">')
+ oa(content[(max_length-ocacity50):(max_length)])
+ oa('...</span></a>')
+ else:
+ oa(content)
- output.append('<br>')
- #output.append("<a href="+item["object"]["links"]["self"]["href"]+">more...</a>")
- output.append('<span style="font-size:smaller">')
- output.append('<a href="'+item["actor"]["url"]+'">'+item["actor"]["displayName"]+'</a>')
- #output.append(item["content"])
- if verb == "share": output.append(' via <a href="'+item["object"]["url"]+'">'+item["object"]["author"]["displayName"]+'</a>')
- #if verb == "share": output.append(' via <a href="'+item["object"]["author"]["url"]+'">'+item["object"]["author"]["displayName"]+'</a>')
- # 2014-01-03T10:30:02Z
- date=time.strptime(item["updated"],"%Y-%m-%dT%H:%M:%SZ")
- output.append(" "+(time.strftime("%d.%m.%Y um %H:%M:%S", date)))
- output.append('</span>')
- output.append("</li>")
+ oa('<br>')
+ actor = item["actor"]
+ #oa("<a href="+item["object"]["links"]["self"]["href"]+">more...</a>")
+ oa('<span style="font-size:smaller">')
+ oa('<a href="'+actor["url"]+'">'+actor["displayName"]+'</a>')
+ #oa(item["content"])
+ if verb == "share":
+ oa(' via <a href="'+item["object"]["url"]+'">'+item["object"]["author"]["displayName"]+'</a>')
+ #if verb == "share":
+ # oa(' via <a href="'+item["object"]["author"]["url"]+'">'+item["object"]["author"]["displayName"]+'</a>')
-output.append("</ul>")
-#output.append('<a href="'+data["author"]["url"]+'">mehr von '+data["author"]["displayName"]+'</a>' )
-s = ''.join(output)
+ # 2014-01-03T10:30:02Z
+ date = time.strptime(item["updated"],"%Y-%m-%dT%H:%M:%SZ")
+ oa(time.strftime(" %d.%m.%Y um %H:%M:%S", date))
+ oa('</span>')
+ oa("</li>")
+
+oa("</ul>")
+#oa('<a href="'+data["author"]["url"]+'">mehr von '+data["author"]["displayName"]+'</a>' )
+body = ''.join(output)
print "Content-Type: text/html;charset=utf-8"
print
-print s
+print body
More information about the Dive4Elements-commits
mailing list