Skip to content

Instantly share code, notes, and snippets.

@richcaudle
Created June 5, 2014 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richcaudle/58bfc378db71ea5ac7aa to your computer and use it in GitHub Desktop.
Save richcaudle/58bfc378db71ea5ac7aa to your computer and use it in GitHub Desktop.
# 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"
OR interaction.content contains_all "step,white,tile"
)
// Exclude noise
AND NOT interaction.content contains "banned,blocked"
AND NOT interaction.content wildcard "cheat*"
AND NOT interaction.content wildcard "trick*"
AND NOT interaction.content wildcard "hack*"'''
# Stream V2 definition
stream_v2 = '''// Mentions relating to games
(
interaction.content contains_any "2048,FarmVille 2,Swamp Attack,Smash,Trials Frontier"
OR interaction.content contains_all "step,white,tile"
OR interaction.content contains_near "Clash,Clans:5"
)
// Exclude noise
AND NOT interaction.content contains "banned,blocked"
AND NOT interaction.content wildcard "cheat*"
AND NOT interaction.content wildcard "trick*"
AND NOT interaction.content wildcard "hack*"'''
############ Methods ###############
# 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']
# Pulls data from stream buffer
def pull_data_from_buffer(subscription_id):
data = client.pull(subscription_id)
# TODO: Do something useful with data here, for now just print it!
print data
# Stops a subscription
def stop_subscription(subscription_id):
client.push.stop(subscription_id)
############ Main script ###############
# Create a client
# TODO: Insert your API credentials
client = datasift.Client('YOUR_USERNAME', 'YOUR_API_KEY')
print 'Compiling and streaming version 1'
hash_v1 = compile_stream(stream_v1)
subscription_id_v1 = create_pull_subscription(hash_v1)
print 'Sleeping...'
time.sleep(10)
print 'Pulling data from buffer'
pull_data_from_buffer(subscription_id_v1)
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)
print 'Stopping stream v1'
stop_subscription(subscription_id_v1)
# In practice you'll keep streaming V2 here, but we'll clean it up for this demo
print 'Clean up: Stopping stream v2'
stop_subscription(subscription_id_v2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment