Skip to content

Instantly share code, notes, and snippets.

@stephenhouser
Last active September 28, 2018 18:37
Show Gist options
  • Save stephenhouser/f1de5174682d746d09cb55bbcff8322a to your computer and use it in GitHub Desktop.
Save stephenhouser/f1de5174682d746d09cb55bbcff8322a to your computer and use it in GitHub Desktop.
Python-Twitter-OSC

Python Twitter to OSC Scraper

Example Python (v3) code to monitor Streaming Twitter API and send OSC messages when a matching tweet is seen.

Python3 Setup

pip install tweepy
pip install python-osc

Twitter Application and API Key

This Digitial Ocean article How To Authenticate a Python Application with Twitter using Tweepy on Ubuntu 14.04 steps 2 through 4 explain how to get the secret keys needed to access the Twitter API.

#!/usr/bin/env python
# Import the necessary methods from tweepy library
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
# Import parsing JSON (from Tweepy)
import json
# Import OSC methods
from pythonosc import udp_client
from pythonosc import osc_message_builder
#Variables that contains the user credentials to access Twitter API
consumer_key = "blopcPoc2zSOjIAuYJFjDAgJZ"
consumer_secret = "9l6jZb1JRbJJTCswGevdqeeQKLQZsbhmvK6YslqlTaqMEhT4zq"
access_token = "16710472-cbFdznVBkFExYcGPfGNA5FouE1sTh2hp4DhHIySCd"
access_token_secret = "S6DVaUKT046gJo9lvP53S9acB4puzAqWO2PuqzvtF7Boh"
# Change these to be the hashtags you want to monitor
searchtags = ['#florence', '#friday']
# Create the OSC connection
oscSender = udp_client.UDPClient("localhost", 2222)
#This is a basic listener that just prints received tweets to stdout and OSC
class StdOutListener(StreamListener):
def on_data(self, data):
tweet = json.loads(data)
for hashtag in tweet['entities']['hashtags']:
msg = osc_message_builder.OscMessageBuilder(address = '/' + hashtag['text'].lower())
msg.add_arg(tweet["text"])
print(hashtag['text'].lower(), ':', tweet['text'])
oscSender.send(msg.build())
return True
def on_error(self, status):
print("ERROR", status)
if __name__ == '__main__':
# Twitter authetication and Twitter Streaming API
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
# Start monitoring the stream API and watch for our searchtags
stream.filter(track=searchtags)
Python example to monitor Streaming Twitter API and send OSC messages when a matching tweet is found.
{
"patcher" : {
"fileversion" : 1,
"appversion" : {
"major" : 7,
"minor" : 3,
"revision" : 3,
"architecture" : "x86",
"modernui" : 1
}
,
"rect" : [ 62.0, 104.0, 640.0, 480.0 ],
"bglocked" : 0,
"openinpresentation" : 0,
"default_fontsize" : 12.0,
"default_fontface" : 0,
"default_fontname" : "Arial",
"gridonopen" : 1,
"gridsize" : [ 15.0, 15.0 ],
"gridsnaponopen" : 1,
"objectsnaponopen" : 1,
"statusbarvisible" : 2,
"toolbarvisible" : 1,
"lefttoolbarpinned" : 0,
"toptoolbarpinned" : 0,
"righttoolbarpinned" : 0,
"bottomtoolbarpinned" : 0,
"toolbars_unpinned_last_save" : 0,
"tallnewobj" : 0,
"boxanimatetime" : 200,
"enablehscroll" : 1,
"enablevscroll" : 1,
"devicewidth" : 0.0,
"description" : "",
"digest" : "",
"tags" : "",
"style" : "",
"subpatcher_template" : "",
"boxes" : [ {
"box" : {
"id" : "obj-6",
"linecount" : 7,
"maxclass" : "message",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 80.0, 326.0, 250.0, 102.0 ],
"style" : "",
"text" : "\"RT @StockMonsterUSA: Calm down, get back to you're seat, I'll handle this !\n\n#NewUnitedAirlinesMottos\n\n#TuesdayThought #TuesdayMotivation\nh…\""
}
}
, {
"box" : {
"id" : "obj-4",
"maxclass" : "newobj",
"numinlets" : 1,
"numoutlets" : 2,
"outlettype" : [ "", "" ],
"patching_rect" : [ 103.0, 197.0, 103.0, 22.0 ],
"style" : "",
"text" : "OSC-route /tweet"
}
}
, {
"box" : {
"id" : "obj-1",
"maxclass" : "newobj",
"numinlets" : 1,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 103.0, 93.0, 99.0, 22.0 ],
"style" : "",
"text" : "udpreceive 2222"
}
}
],
"lines" : [ {
"patchline" : {
"destination" : [ "obj-4", 0 ],
"disabled" : 0,
"hidden" : 0,
"source" : [ "obj-1", 0 ]
}
}
, {
"patchline" : {
"destination" : [ "obj-6", 1 ],
"disabled" : 0,
"hidden" : 0,
"source" : [ "obj-4", 0 ]
}
}
],
"dependency_cache" : [ {
"name" : "OSC-route.mxo",
"type" : "iLaX"
}
],
"autosave" : 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment