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