Mercurial > pumpbridge
comparison src/syncALONE.coffee @ 19:6b3f38af3fa4
use child_process for backend
author | Mathias Gebbe <mgebbe@intevation.de> |
---|---|
date | Fri, 13 Jun 2014 18:13:36 +0200 |
parents | |
children | 9436298e6d78 |
comparison
equal
deleted
inserted
replaced
18:c552faae9c1c | 19:6b3f38af3fa4 |
---|---|
1 # Copyright (C) 2014 by Intevation GmbH | |
2 # Author: Mathias Gebbe <mgebbe@intevation.de> | |
3 # | |
4 # This file is Free Software under the Apache License, Version 2.0; | |
5 # and comes with NO WARRANTY! | |
6 # See the documentation coming with pumpbridge for details. | |
7 | |
8 _ = require("underscore") | |
9 async = require("async") | |
10 Facebook = require("./facebook") | |
11 Usermap = require("./usermap") | |
12 Google = require("./google") | |
13 Config = require ("./config") | |
14 config = Config.config | |
15 Twitter = require("./twitter")(config) | |
16 | |
17 databank = require("databank") | |
18 Databank = databank.Databank | |
19 DatabankObject = databank.DatabankObject | |
20 | |
21 db = Databank.get(config.driver, config.params) | |
22 DatabankObject.bank = db | |
23 | |
24 syncFromESN = () -> | |
25 console.log 'syncFromESN' | |
26 | |
27 async.waterfall [ | |
28 (callback) -> | |
29 db.connect(config.params, callback) | |
30 (callback) -> | |
31 #### | |
32 # Facebook | |
33 | |
34 try | |
35 Usermap.scan ((user) -> | |
36 if user.id.indexOf('@facebook') isnt -1 | |
37 console.log "start sync for facebook user" | |
38 Facebook.sync(user) | |
39 ), (err) -> | |
40 catch err | |
41 console.log 'Error!' + err | |
42 | |
43 # Twitter | |
44 try | |
45 Usermap.scan ((user) -> | |
46 if user.id.indexOf('@twitter') isnt -1 | |
47 console.log "start sync for twitter user" | |
48 Twitter.sync(user) | |
49 ), (err) -> | |
50 catch err | |
51 console.log 'Error!' + err | |
52 | |
53 | |
54 # Google | |
55 try | |
56 Usermap.scan ((user) -> | |
57 if user.id.indexOf('@google') isnt -1 | |
58 console.log "start sync for google user" | |
59 Google.sync(user) | |
60 ), (err) -> | |
61 catch err | |
62 console.log 'Error!' + err | |
63 #### | |
64 ], (err, result) -> | |
65 callback null, 'done' | |
66 | |
67 return | |
68 | |
69 | |
70 postParser = (post, user, network, callback) -> | |
71 parsed = "" | |
72 #console.log "\n" + post.id + "\n" + user.id + user.displayName | |
73 #PROFILE_LINK PROFILE_NAME PROFILE_PIC_LINK_80x80=$3 POST_LINK POST_TIME CONTENT | |
74 #text='<img src='$PROFILE_PIC_LINK_80x80'></img> <a href='$PROFILE_LINK'>'$PROFILE_NAME'</a> <a href='$POST_LINK'>wrotes</a> at '$POST_TIME':<br><br>'$CONTENT'' | |
75 | |
76 if network is 'facebook' | |
77 if user? and user.profilePicLink? | |
78 profilePicLink = user.profilePicLink.replace("50x50", "80x80") | |
79 else | |
80 profilePicLink = 'http://upload.wikimedia.org/wikipedia/commons/thumb/c/c2/F_icon.svg/80px-F_icon.svg.png' | |
81 | |
82 if post.type is 'status' | |
83 parsed = "<img src='" + profilePicLink + "'></img> <a href='" + user.profileLink + "'>" + user.displayName + "</a> <a href='https://www.facebook.com/" + post.id + "'> wrotes</a> via " + network + " at " + post.updated_time + ":<br><br>" + post.message | |
84 | |
85 if post.type is 'photo' | |
86 parsed = "<img src='" + profilePicLink + "'></img> <a href='" + user.profileLink + "'>" + user.displayName + "</a> <a href='"+ post.link + "'> wrotes</a> via " + network + " at " + post.updated_time + ":<br>" | |
87 parsed += "<br>" + post.message if post.message? and post.message isnt "" | |
88 parsed += "<br><img src='" + post.picture + "'></img>" | |
89 | |
90 if post.type is 'link' or post.type is 'video' | |
91 parsed = "<img src='" + profilePicLink + "'></img> <a href='" + user.profileLink + "'>" + user.displayName + "</a> <a href='https://www.facebook.com/" + post.id + "'> wrotes</a> via " + network + " at " + post.updated_time + ":<br>" | |
92 parsed += "<br>" + post.message if post.message? and post.message isnt "" | |
93 parsed += "<br>" + post.description if post.description? | |
94 parsed += "<br><a href='" + post.link + "'>"+ post.link + "</a>" if post.link? | |
95 parsed += "<br><img src='" + post.picture + "'></img>" if post.picture? | |
96 | |
97 callback null, parsed | |
98 | |
99 if network is 'google' | |
100 parsed = "<img src='" + post.actor.image.url + "'></img> <a href='" + post.actor.url + "'>" + post.actor.displayName + "</a> <a href='"+ post.object.url + "'> wrotes</a> via " + network + " at " + post.updated + ":<br><br>" + post.object.content | |
101 if not post.object.attachments? | |
102 _.each post.object.attachments, (attachment) -> | |
103 | |
104 if attachment.objectType == "photo" | |
105 parsed += "<br><img src='" + attachment.image.url + "'></img>" | |
106 | |
107 if attachment.objectType == "article" | |
108 parsed += "<br><a href='" + attachment.url + "'>"+ attachment.displayName + "</a>" | |
109 if not attachment.image? | |
110 parsed += "<br><img src='" + attachment.image.url + "'></img>" | |
111 | |
112 if attachment.objectType == "video" | |
113 parsed += "<br><a href='" + attachment.url + "'>"+ attachment.displayName + "</a>" | |
114 if not attachment.image? | |
115 parsed += "<br><img src='" + attachment.image.url + "'></img>" | |
116 | |
117 callback null, parsed | |
118 | |
119 if network is 'twitter' | |
120 | |
121 parsed = "<img src='" + post.user.profile_image_url + "'></img> <a href='https://twitter.com/account/redirect_by_id/" + post.user.id + "'>" + post.user.name + "</a> <a href='https://twitter.com/" + post.user.screen_name + "/status/" + post.id_str + "'> wrotes </a> via " + network + " at " + post.created_at + ":<br><br>" + post.text | |
122 _.each post.entities.media, (attachment) -> | |
123 parsed += "<br><img src='" + attachment.media_url + "'></img>" | |
124 callback null, parsed | |
125 | |
126 return | |
127 | |
128 | |
129 sync = () -> | |
130 | |
131 # Do this every xx minutes | |
132 console.log '\n\n\n' + "starting sync deamon" | |
133 interval = config.interval | |
134 if not (interval?) | |
135 interval = 15 * 60 * 1000 # 900 000 ms (15min) | |
136 setInterval syncFromESN, interval | |
137 | |
138 syncFromESN() | |
139 | |
140 return | |
141 | |
142 sync() | |
143 | |
144 exports.postParser = postParser | |
145 exports.sync = sync |