-
-
Save zareshahi/6d9206351168b83584d6ac6b7c7d9aa7 to your computer and use it in GitHub Desktop.
Get data from API Twitter and using for Data Analysis
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
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