1 |
import gdata |
2 |
import atom |
3 |
from gdata import service |
4 |
import json |
5 |
from pymongo import * |
6 |
from bson.code import * |
7 |
from urllib import unquote_plus |
8 |
import random |
9 |
|
10 |
def application(environ, start_response): |
11 |
#set the headers |
12 |
status = '200 OK' |
13 |
response_headers = [('Content-type', 'text/plain'),( 'Access-Control-Allow-Origin', '*')] |
14 |
start_response(status, response_headers) |
15 |
|
16 |
try: |
17 |
recieved = environ['wsgi.input'].read(int(environ['CONTENT_LENGTH'])) |
18 |
|
19 |
except KeyError: |
20 |
recieved= 'empty' |
21 |
print >> environ['wsgi.errors'], recieved |
22 |
return 'empty' |
23 |
else: |
24 |
print >> environ['wsgi.errors'], recieved |
25 |
#connect to DB |
26 |
MONGODB_SERVER = 'localhost' |
27 |
MONGODB_PORT = 27017 |
28 |
MONGODB_DB = 'alipi' |
29 |
MONGODB_COLLECTION = 'post' |
30 |
#MONGODB_UNIQ_KEY = 'url' |
31 |
|
32 |
connection = Connection(MONGODB_SERVER, MONGODB_PORT) |
33 |
db = connection[MONGODB_DB] |
34 |
collection = db[MONGODB_COLLECTION] |
35 |
collection.create_index("url") |
36 |
ren_id = random.random() #all elements from the same ren have the same id |
37 |
|
38 |
#parse recieved data and save in a dict() |
39 |
string = '' |
40 |
lang = '' |
41 |
target = '' |
42 |
about = '' |
43 |
author = '' |
44 |
commands = recieved.split('###') #for every elementary re-narration (e.g a paragraph) |
45 |
dicts = [] |
46 |
i = 0 |
47 |
for command in commands: |
48 |
d = {} |
49 |
parameter_pairs = command.split('&'); |
50 |
for parameter_pair in parameter_pairs: |
51 |
parameter_pair = parameter_pair.split('=',1) |
52 |
d[unquote_plus(parameter_pair[0])] = unquote_plus(parameter_pair[1]) |
53 |
|
54 |
d['ren_id']= ren_id |
55 |
alipius = "lang:{0},location:{1},elementtype:{2},style:{3},author:{4}".format(d['lang'],d['location'],d['elementtype'],d['style'],d['author']) |
56 |
if d['elementtype'] == 'text': |
57 |
string +='<p about="{0}" xpath="{1}" alipius="{2}">{3}</p>'.format(d['about'],d['xpath'],alipius,d['data']) |
58 |
elif d['elementtype'] == 'audio/ogg': |
59 |
string+='<audio about="{0}" xpath="{1}" controls="controls" alipius="{2}" src="{3}"></audio>'.format(d['about'],d['xpath'],alipius,d['data']) |
60 |
else: |
61 |
src = d['data'].split(',')[1] |
62 |
width = d['data'].split(',')[0].split('x')[0] |
63 |
height = d['data'].split(',')[0].split('x')[1] |
64 |
string+='<img about="{0}" xpath="{1}" alipius="{2}" src="{3}" width={4} height={5}></img>'.format(d['about'],d['xpath'],alipius,src,width,height) |
65 |
|
66 |
lang = d['lang'] |
67 |
target = d['location'] |
68 |
about = d['about'] |
69 |
author = d['author'] |
70 |
|
71 |
dicts.append(d) |
72 |
i+=1 |
73 |
blogEntry= '' |
74 |
blogger_service = service.GDataService("allipi123@gmail.com", "allipi3354") |
75 |
blogger_service.source = 'Servelots-alipi-1.0' |
76 |
blogger_service.service = 'blogger' |
77 |
blogger_service.account_type = 'GOOGLE' |
78 |
blogger_service.server = 'www.blogger.com' |
79 |
blogger_service.ProgrammaticLogin() |
80 |
query = service.Query() |
81 |
query.feed = '/feeds/default/blogs' |
82 |
feed = blogger_service.Get(query.ToUri()) |
83 |
blog_id = " " |
84 |
for entry in feed.entry: |
85 |
if "http://alipi123.blogspot.com/" == entry.GetHtmlLink().href: |
86 |
blog_id = entry.GetSelfLink().href.split("/")[-1] |
87 |
blogEntry = CreatePublicPost(blogger_service, blog_id, title="Re-narration", content=string + "<blockquote><p>Re-narration by "+author+' in '+lang+' targeting '+target+' for this web <a href="'+about+'">page</a></p></blockquote>') |
88 |
|
89 |
j=0 |
90 |
while j< len(dicts): |
91 |
dicts[j]["blog"] = str(blogEntry.GetHtmlLink().href) |
92 |
collection.insert(dicts[j]) |
93 |
j+=1 |
94 |
|
95 |
#commands.getoutput(cmd) |
96 |
|
97 |
return 'ok' |
98 |
#return ["Blog successfuly posted!!"] |
99 |
|
100 |
def CreatePublicPost(blogger_service, blog_id, title, content): |
101 |
entry = gdata.GDataEntry() |
102 |
entry.title = atom.Title('xhtml', title) |
103 |
entry.content = atom.Content(content_type='html', text=content) |
104 |
return blogger_service.Post(entry, '/feeds/%s/posts/default' % blog_id) |