COSMOS-Get Twitter data based on location

The flow may be used in order to register to Twitter and start receiving tweets based on filters of geolocation. The tweets are processed in order to keep only a part of the information and annotate them with sentiment analysis from the built-in nodered node. Needs configuration on one node (Twitlib), needs Twitter OAth credentials. You also need to import tne node.js lib https://github.com/ttezel/twit and follow a process for including it in Node-RED mentioned in nodered.org (http://nodered.org/docs/writing-functions)

[{"id":"5169acbc.78d0fc","type":"inject","name":"Press to start","topic":"","payload":"start","payloadType":"string","repeat":"","crontab":"","once":false,"x":143,"y":193,"z":"61165d51.e79b34","wires":[["3fca1e83.88b6f2"]]},{"id":"3fca1e83.88b6f2","type":"function","name":"TwitLib","func":"var Twit = context.global.twit;\n\nvar d = new Date();\n//d.setTimezone('Europe/Amsterdam',true);\nconsole.log(d);\n\nvar T = new Twit({\n    consumer_key:         'YOUR_CONSUMER_KEY'\n  , consumer_secret:      'YOUR_CONSUMER_SECRET_KEY'\n  , access_token:         'YOUR_ACCESS_TOKEN'\n  , access_token_secret:  'YOUR_SECRET_ACCESS_TOKEN'\n})\n\n//console.log(\"T:\"+JSON.stringify(T));\n//Example locations\nvar sanFrancisco = [ '-122.75', '36.8', '-121.75', '37.8' ]\n\nvar madrid = [ '-3.75698', '40.39022' , '-3.62514' , '40.4903']\n//SW: 40.39022, -3.75698\n//NE: 40.4903, -3.62514\n\n\nvar stream = T.stream('statuses/filter', { locations: madrid });\n\nstream.on('error', function (error) {\n  //...\n  console.log(error);\n})\n\nstream.on('tweet', function (tweet) {\n  console.log(tweet);\n  msg.payload=tweet;\n  node.send(msg);\n  \n})\n\n\nif (msg.payload=='stop'){\n    stream.stop();\n}\n//generic for all messages in order not to handle all available events\nstream.on('message', function (message) {\n    console.log(message);\n  \n  //...\n})\n\nreturn msg;","outputs":1,"valid":true,"x":292,"y":304,"z":"61165d51.e79b34","wires":[["119291b1.dbc07e"]]},{"id":"119291b1.dbc07e","type":"function","name":"cropTweet","func":"if (msg.payload.coordinates){\n    \nvar ts=Number(msg.payload.timestamp_ms);\nvar text=msg.payload.text;\nvar lon=msg.payload.coordinates.coordinates[0];\nvar lat=msg.payload.coordinates.coordinates[1];\n//console.log(\"THIS IS THE PRINTED\"+lon);\nvar twitter_id=msg.payload.user.id_str;\nmsg.payload={};//PROPER INITIALIZATION, SHOULD NOT BE DONE WITH JUST \"\"\nmsg.payload.ts=ts;\nmsg.payload.text=text;\nmsg.payload.lon=lon;\nmsg.payload.lat=lat;\nmsg.payload.twitter_id=twitter_id;\nmsg.payload.sentiment=\"\";\nnode.send(msg);\n}","outputs":1,"valid":true,"x":487,"y":205,"z":"61165d51.e79b34","wires":[["12766523.7ee3d3"]]},{"id":"12766523.7ee3d3","type":"function","name":"move payload","func":"\n//This is needed to adapt to the output schema and to the next node\n//which expects the text to be analyzed in msg.payload\nmsg.standard_payload=msg.payload;\nmsg.payload=msg.payload.text;\nreturn msg;","outputs":1,"valid":true,"x":649,"y":200,"z":"61165d51.e79b34","wires":[["e6c9be78.fb3b48"]]},{"id":"e6c9be78.fb3b48","type":"sentiment","name":"","x":814,"y":134,"z":"61165d51.e79b34","wires":[["43f864b1.cae18c"]]},{"id":"43f864b1.cae18c","type":"function","name":"RestorePayload_Add_sentiment_score","func":"msg.payload=msg.standard_payload;\nmsg.standard_payload={};\nmsg.payload.sentiment=msg.sentiment.score;\n\nreturn msg;","outputs":1,"valid":true,"x":1007,"y":192,"z":"61165d51.e79b34","wires":[["3828a31a.8af4dc"]]},{"id":"3828a31a.8af4dc","type":"debug","name":"","active":true,"console":"false","complete":"payload","x":949.75,"y":270.75,"z":"61165d51.e79b34","wires":[]},{"id":"6ee3e199.a54018","type":"comment","name":"README-Needs configuration","info":"Configuration is required in this node. Initially with relation to the Twitter credentials for accessing,\nlines 8-11, and afterwards with relation to the location coordinates (example lines 16 and 18)\nyou want to define for filtering tweets and what you are using (line 23).\n\nYou also need to import tne node.js lib https://github.com/ttezel/twit and follow a process for including\nit in Node-RED mentioned in nodered.org (http://nodered.org/docs/writing-functions)","x":275.75,"y":343.75,"z":"61165d51.e79b34","wires":[]},{"id":"a9532b61.76c9b8","type":"comment","name":"Output of your choice","info":"The output of Restore payload is abiding to this format (you can check it in the debug tab and\nthe debug green node)\n{\n    \"namespace\": \"Twitter.Data\",\n    \"type\": \"record\",\n    \"name\": \"Example\",\n    \"fields\": [{\n            \"name\": \"ts\",\n            \"type\": \"long\"\n        }, {\n            \"name\": \"text\",\n            \"type\": \"string\"\n        },\n                {\n            \"name\": \"lon\",\n            \"type\": \"double\"\n        },\n                {\n            \"name\": \"lat\",\n            \"type\": \"double\"\n        },\n                {\n            \"name\": \"twitter_id\",\n            \"type\": \"string\"\n        },\n                {\n            \"name\": \"sentiment\",\n            \"type\":[\"null\",\"double\"]\n        }\n    ]\n} \n\nand can be pushed by adding a node of your choice to whatever next step (mqtt feed, database -after importing\nthe respective nodered node from the repository-check nodered.org for details on that)","x":1317.75,"y":187.75,"z":"61165d51.e79b34","wires":[]},{"id":"c9959968.8deee8","type":"comment","name":"Start/Stop instructions","info":"For starting, just press the blue button below\n\nFor stopping, replace the payload string with 'stop' and retrigger","x":129.75,"y":135.75,"z":"61165d51.e79b34","wires":[]}]

Flow Info

Created 8 years, 7 months ago
Rating: not yet rated

Owner

Actions

Rate:

Node Types

Core
  • comment (x3)
  • debug (x1)
  • function (x4)
  • inject (x1)
Other
  • sentiment (x1)

Tags

  • Twitter
  • Sentiment
  • filter
  • messages
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option