summaryrefslogtreecommitdiffstats
path: root/modules/twitter.module/module.cpp
blob: eb704cc7323570a56b19f2b96180398416e7e43e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*  Copyright 2008,2009,2010 Edwin Eefting ([email protected]) 

    This file is part of Synapse.

    Synapse is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Synapse is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Synapse.  If not, see <http://www.gnu.org/licenses/>. */


/** \file
The twitter module.

This is a simple module that follows twitterfeeds


*/


#include "synapse.h"
#include "cconfig.h"
#include <time.h>


namespace twitter
{
using namespace std;


synapse::Cconfig config;
string userIds;
enum eState {
		GET_USERS,
		STREAM
};
eState state;
string queue;
int moduleId;

//send resquest based on current state
SYNAPSE_REGISTER(twitter_Request)
{
	queue="";

	Cmsg out;

	//abort old stuff
	out["id"]="twitter";
	out.event="curl_Abort";
	out.send();

	//add oauth parameters:
	out["oauth"]["consumer_key"]=config["oauth_consumer_key"];
	out["oauth"]["consumer_key_secret"]=config["oauth_consumer_key_secret"];
	out["oauth"]["token"]=config["oauth_token"];
	out["oauth"]["token_secret"]=config["oauth_token_secret"];
	out["failonerror"]=0;

	if (state==GET_USERS)
	{
		out["url"]="http://api.twitter.com/1/statuses/home_timeline.json?count=200";
	}

	else if (state==STREAM)
	{
		out["url"]="https://userstream.twitter.com/2/user.json";

		//reset error status
		Cmsg err;
		err.event="twitter_Ok";
		err.send();
	}

	out.event="curl_Get";
	out.send();
}

void request()
{
	Cmsg out;
	out.dst=moduleId;
	out.event="twitter_Request";
	out.send();
}

void delayedRequest()
{
	Cmsg out;
	out.event="timer_Set";
	out["seconds"]=60;
	out["event"]="twitter_Request";
	out["dst"]=moduleId;
	out.send();
}


SYNAPSE_REGISTER(module_Init)
{
	Cmsg out;

	moduleId=msg.dst;

	config.load("etc/synapse/twitter.conf");

	out.event="core_LoadModule";
	out["name"]="curl";
	out.send();
}

SYNAPSE_REGISTER(curl_Ready)
{
	Cmsg out;
	out.event="core_LoadModule";
	out["name"]="http_json";
	out.send();

}

SYNAPSE_REGISTER(http_json_Ready)
{
	Cmsg out;
	out.event="core_LoadModule";
	out["name"]="timer";
	out.send();
}

SYNAPSE_REGISTER(timer_Ready)
{

	Cmsg out;
	//tell the rest of the world we are ready for duty
	out.event="core_Ready";
	out.send();

	Cmsg err;
	err.event="twitter_Error";
	err["error"]="Connecting...";
	out.send();

	out.clear();

	state=GET_USERS;
	request();
}


SYNAPSE_REGISTER(twitter_GetConfig)
{
	Cmsg out;
	out.dst=msg.src;
	out.event="twitter_Config";
	out.map()=config.map();
	out.send();
}

SYNAPSE_REGISTER(twitter_SetConfig)
{
	config.map()=msg.map();
	config.changed();
	config.save();


}


SYNAPSE_REGISTER(curl_Ok)
{
	try
	{

		//determine how to interpret the data:
		if (state==GET_USERS)
		{
			Cvar data;
			data.fromJson(queue);

			if (data.which()==CVAR_MAP && data.isSet("error"))
			{
				Cmsg out;
				out.event="twitter_Error";
				out["error"]=data["error"].str();
				out.send();

				delayedRequest();
			}
			else
			{
				//traverse all the users and send their last statusses
				userIds="";
				ERROR("count " << data.list().size());

				for (CvarList::reverse_iterator I=data.list().rbegin(); I!=data.list().rend(); I++)
				{
					if (userIds=="")
						userIds+=(*I)["id_str"].str();
					else
						userIds+=","+(*I)["id_str"].str();

					Cmsg out;
					out.event="twitter_Data";
					out.map()=(*I).map();
					out.send();
				}
				state=STREAM;

				request();
			}
		}
		else if (state==STREAM)
		{
			//if we get disconnected, we need to reget history in case we missed something
			state=GET_USERS;

			delayedRequest();
		}
	}
	catch(...)
	{
		Cmsg out;
		out.event="twitter_Error";
		out["error"]="Error while parsing twitter data";
		out.send();

		delayedRequest();
		throw;
	}


}

SYNAPSE_REGISTER(curl_Error)
{
	Cmsg out;
	out.event="twitter_Error";
	out["error"]=msg["error"];
	out.send();

	if (state==STREAM)
	{
		//if we get disconnected, we need to reget history in case we missed something
		state=GET_USERS;
	}

	delayedRequest();
}

SYNAPSE_REGISTER(curl_Data)
{
	queue+=msg["data"].str();
	if(state==STREAM)
	{
		while (queue.find("\n")!=string::npos)
		{
			//get the jsonStr, delimited by \n:
			string jsonStr=queue.substr(0,queue.find("\n")+1);
			queue.erase(0,jsonStr.length());

			if (jsonStr.length()>2)
			{
				//convert to data
				Cvar data;
				try
				{
					data.fromJson(jsonStr);
				}
				catch(...)
				{
					Cmsg out;
					out.event="twitter_Error";
					out["error"]=jsonStr;
					out.send();
					return;
				}

				//send out message
				Cmsg out;
				out.event="twitter_Data";
				out.map()=data;
				out.send();
			}
		}
	}
}







}
;