Skip to content

Instantly share code, notes, and snippets.

View richcaudle's full-sized avatar

Rich Caudle richcaudle

View GitHub Profile
@richcaudle
richcaudle / mysql-destination-sample
Created February 6, 2014 16:28
Example interaction for MySQL Destination post
{
"interaction": {
"id": "1e3312c45c35ac80e074ddead5649524",
"type": "twitter",
"created_at": "Wed, 09 Oct 2013 21:46:37 +0000",
"content": "I can't wait to talk to the @DataSift guys about how #social data is transforming #marketing",
"hashtags": [
"social",
"marketing"
],
# Include the DataSift library
import datasift
# Create a client
client = datasift.Client('DATASIFT_USERNAME', 'DATASIFT_API_KEY')
# Declare a filter in CSDL, looking for content mentioning brands
csdl = 'interaction.content contains_any "Calvin Klein, GQ, Adidas"'
# Compile the filter
fltr = client.compile(csdl)
# Handler: Message deleted by user
@client.on_delete
def on_delete(interaction):
print "You must delete this to be compliant with T&Cs: ", interaction
# Handler: Connection was closed
@client.on_closed
def on_close(wasClean, code, reason):
print "Stream subscriber shutting down because ", reason
python [yourscript].py
# Declare a filter in CSDL, looking for content mentioning brands
csdl = '''tag.brand "Calvin Klein" { interaction.content contains "Calvin Klein" }
tag.brand "GQ" { interaction.content contains "GQ" }
tag.brand "Adidas" { interaction.content contains "Adidas" }
return
{
interaction.content contains_any "Calvin Klein, GQ, Adidas"
}'''
# Compiles a stream from CSDL and returns the stream hash
def compile_stream(csdl):
fltr = client.compile(csdl)
return fltr['hash']
# Creates a pull subscription given a stream hash
def create_pull_subscription(hash):
subscription = client.push.create_from_hash(hash,'Example pull subscription','pull',{})
return subscription['id']
# Include the DataSift and time libraries
import datasift
import time
############ CSDL Definitions ###############
# Stream V1 definition
stream_v1 = '''// Mentions relating to games
(
interaction.content contains_any "2048,FarmVille 2,Swamp Attack,Smash,Trials Frontier"
print 'Compiling and streaming version 2'
hash_v2 = compile_stream(stream_v2)
subscription_id_v2 = create_pull_subscription(hash_v2)
print 'Sleeping...'
time.sleep(10)
print 'Pulling data from buffer'
pull_data_from_buffer(subscription_id_v2)