Skip to content

Instantly share code, notes, and snippets.

@vessaldaneshvar
Last active October 28, 2020 12:10
Show Gist options
  • Save vessaldaneshvar/3474fbbecb1a6d70488e09251b65c023 to your computer and use it in GitHub Desktop.
Save vessaldaneshvar/3474fbbecb1a6d70488e09251b65c023 to your computer and use it in GitHub Desktop.
Get data from API Twitter and using for Data Analysis
from neo4j import GraphDatabase
import tweepy
import json
# Connect To DataBase Neo4j
uri = "bolt://localhost:7687"
driver = GraphDatabase.driver(uri, auth=("neo4j", "twitterdata"))
consumer_key = "consumer_key"
consumer_secret = "consumer_secret"
access_token = "access_token"
access_token_secret = "access_token_secret"
# Authenticate and create Api object
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token,access_token_secret)
api = tweepy.API(auth)
class MyStreamListener(tweepy.StreamListener):
def on_data(self, data):
# Get Data by Streamer
json_data = json.loads(data)
# Save On DataBase
with driver.session() as session:
session.run("""CREATE (a:User
{id:$id,
id_str:$id_str,
name:$name,
screen_name:$screen_name,
location:$location,
followers_count:$followers_count,
friends_count:$friends_count,
favourites_count:$favourites_count,
statuses_count:$statuses_count,
created_at:$created_at})""",json_data["user"])
# continue getting data
return True
def on_status(self, status):
print(status.text)
def on_error(self,status_code):
print(status_code)
return False
myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth = api.auth, listener=myStreamListener)
myStream.filter(languages=['fa'],track=['در','به','با','از','اگر','شاید','هست','رو','را','تو','اگه','پس','که','چرا'])
driver.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment