This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pip install datasift |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Include the DataSift library | |
import datasift | |
# Create a client | |
client = datasift.Client('DATASIFT_USERNAME', 'DATASIFT_API_KEY') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python [yourscript].py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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" | |
}''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
OlderNewer