Last active
December 12, 2019 01:22
-
-
Save jexp/dc59ea550186d49e5e17ff3a08d5ec5b to your computer and use it in GitHub Desktop.
Twitter Import Neo4j using https://neo4j.com/try-neo4j
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
create constraint on (u:User) assert u.screen_name is unique; | |
create constraint on (t:Tweet) assert t.id is unique; | |
create constraint on (t:Tag) assert t.name is unique; | |
create constraint on (l:Link) assert l.url is unique; | |
create constraint on (t:Tag) assert t.name is unique; |
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
WITH | |
"GE2019+-RT" as query, | |
"https://api.twitter.com/1.1/search/tweets.json?count=100&result_type=recent&lang=en&q=" as url, | |
"AAAAA....MkE" as bearer | |
CALL apoc.load.jsonParams(url + query,{Authorization:"Bearer "+bearer},null) yield value | |
UNWIND value.statuses as t | |
WITH t | |
ORDER BY t.id | |
WITH t, | |
t.entities AS e, | |
t.user AS u, | |
t.retweeted_status AS retweet | |
// RETURN t.text, [h IN t.entities.hashtags | h.text] as tags, u.screen_name | |
MERGE (tweet:Tweet {id:t.id}) | |
SET tweet.text = t.text, | |
tweet.created = t.created_at, | |
tweet.favorites = t.favorite_count | |
MERGE (user:User {screen_name:u.screen_name}) | |
SET user.name = u.name, | |
user.location = u.location, | |
user.followers = u.followers_count, | |
user.following = u.friends_count, | |
user.statuses = u.statuses_count, | |
user.profile_image_url = u.profile_image_url | |
MERGE (user)-[:POSTED]->(tweet) | |
FOREACH (h IN e.hashtags | | |
MERGE (tag:Tag {name:LOWER(h.text)}) | |
MERGE (tag)<-[:TAGGED]-(tweet) | |
) | |
FOREACH (u IN e.urls | | |
MERGE (url:Link {url:u.expanded_url}) | |
MERGE (tweet)-[:LINKED]->(url) | |
) | |
FOREACH (m IN e.user_mentions | | |
MERGE (mentioned:User {screen_name:m.screen_name}) | |
ON CREATE SET mentioned.name = m.name | |
MERGE (tweet)-[:MENTIONED]->(mentioned) | |
) | |
FOREACH (r IN [r IN [t.in_reply_to_status_id] WHERE r IS NOT NULL] | | |
MERGE (reply_tweet:Tweet {id:r}) | |
MERGE (tweet)-[:REPLIED_TO]->(reply_tweet) | |
) | |
FOREACH (retweet_id IN [x IN [retweet.id] WHERE x IS NOT NULL] | | |
MERGE (retweet_tweet:Tweet {id:retweet_id}) | |
MERGE (tweet)-[:RETWEETED]->(retweet_tweet) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
see also: https://adamcowley.co.uk/neo4j/loading-tweets-into-neo4j-with-apoc/