Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Querying the twitter graph
// Sandbox instance
https://10-0-1-157-32898.neo4jsandbox.com/browser/
readonly / twitter
// find tweets that quote each other
MATCH tweets = (t:Tweet)-[:QUOTED*]->(t2:Tweet) WHERE not exists((t2)-[:QUOTED]->())
RETURN tweets;
// find the longest chain
MATCH path = shortestpath((start:Tweet)-[:QUOTED*]->(origin))
RETURN path
ORDER BY LENGTH(path) DESC
LIMIT 1
// longest chain + users
MATCH path = shortestpath((start:Tweet)-[:QUOTED*]->(origin))
WITH path
ORDER BY LENGTH(path) DESC
LIMIT 1
UNWIND NODES(path) AS tweet
MATCH (tweet)<-[:POSTED]->(user)
RETURN tweet, user
WITH 'https://api.twitter.com/1.1/search/tweets.json?count=100&result_type=recent&lang=en&q=' as url, {bearer} as bearer
CALL apoc.load.jsonParams(url + "%22my%20name%22%20is%22%20%22I%20work%22",{Authorization:"Bearer "+bearer},null) yield value
UNWIND value.statuses as status
WITH status, status.user as u, status.entities as e
WHERE status.quoted_status_id is not null
MERGE (t:Tweet {id:status.id}) ON CREATE SET t.text=status.text,t.created_at=status.created_at,t.retweet_count=status.retweet_count, t.favorite_count=status.favorite_count
MERGE (p:User {name:u.screen_name})
MERGE (p)-[:POSTED]->(t)
FOREACH (m IN e.user_mentions | MERGE (mu:User {name:m.screen_name}) MERGE (t)-[:MENTIONED]->(mu))
MERGE (q:Tweet {id:status.quoted_status_id})
MERGE (t)–[:QUOTED]->(q)
WITH t as t0, status.quoted_status as status WHERE status is not null
WITH t0, status, status.user as u, status.entities as e
MERGE (t:Tweet {id:status.id}) SET t.text=status.text,t.created_at=status.created_at,t.retweet_count=status.retweet_count, t.favorite_count=status.favorite_count
MERGE (t0)-[:QUOTED]->(t)
MERGE (p:User {name:u.screen_name})
MERGE (p)-[:POSTED]->(t)
FOREACH (m IN e.user_mentions | MERGE (mu:User {name:m.screen_name}) MERGE (t)-[:MENTIONED]->(mu))
MERGE (q:Tweet {id:status.quoted_status_id})
MERGE (t)–[:QUOTED]->(q);
// fill in missing tweets
match (tweet:Tweet)
WHERE NOT exists(tweet.text)
WITH 'https://api.twitter.com/1.1/statuses/lookup.json?count=100&result_type=recent&lang=en&id=' + tweet.id as url, {bearer} as bearer
CALL apoc.load.jsonParams(url,{Authorization:"Bearer "+bearer},null) yield value
WITH value as status
WITH status, status.user as u, status.entities as e
WHERE status.quoted_status_id is not null
MERGE (t:Tweet {id:status.id}) ON CREATE SET t.text=status.text,t.created_at=status.created_at,t.retweet_count=status.retweet_count, t.favorite_count=status.favorite_count
MERGE (p:User {name:u.screen_name})
MERGE (p)-[:POSTED]->(t)
FOREACH (m IN e.user_mentions | MERGE (mu:User {name:m.screen_name}) MERGE (t)-[:MENTIONED]->(mu))
MERGE (q:Tweet {id:status.quoted_status_id})
MERGE (t)–[:QUOTED]->(q)
WITH t as t0, status.quoted_status as status WHERE status is not null
WITH t0, status, status.user as u, status.entities as e
MERGE (t:Tweet {id:status.id}) SET t.text=status.text,t.created_at=status.created_at,t.retweet_count=status.retweet_count, t.favorite_count=status.favorite_count
MERGE (t0)-[:QUOTED]->(t)
MERGE (p:User {name:u.screen_name})
MERGE (p)-[:POSTED]->(t)
FOREACH (m IN e.user_mentions | MERGE (mu:User {name:m.screen_name}) MERGE (t)-[:MENTIONED]->(mu))
MERGE (q:Tweet {id:status.quoted_status_id})
MERGE (t)–[:QUOTED]->(q);
// fill in missing tweets
match (tweet:Tweet)
WHERE NOT exists(tweet.text)
WITH 'https://api.twitter.com/1.1/statuses/lookup.json?count=100&result_type=recent&lang=en&id=' + tweet.id as url, {bearer} as bearer
CALL apoc.load.jsonParams(url,{Authorization:"Bearer "+bearer},null) yield value
WITH value as status
WITH status, status.user as u, status.entities as e
MERGE (t:Tweet {id:status.id}) ON CREATE SET t.text=status.text,t.created_at=status.created_at,t.retweet_count=status.retweet_count, t.favorite_count=status.favorite_count
MERGE (p:User {name:u.screen_name})
MERGE (p)-[:POSTED]->(t)
FOREACH (m IN e.user_mentions | MERGE (mu:User {name:m.screen_name}) MERGE (t)-[:MENTIONED]->(mu))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment