-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f0d9795
Showing
8 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
consumer_key = "t5bxx#######" | ||
consumer_secret = "8CoU8####" | ||
access_token = "735#########" | ||
access_secret = "vKP########" | ||
mongo_uri = "###############" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
*.so | ||
|
||
.Python | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
share/python-wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
MANIFEST | ||
|
||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
htmlcov/ | ||
.tox/ | ||
.nox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
*.py,cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
cover/ | ||
|
||
*.mo | ||
*.pot | ||
__pypackages__/ | ||
.env |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# spamerio |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import re | ||
|
||
class Cleaner: | ||
def clean_tweet(self, tweet): | ||
r = tweet.lower() | ||
r = re.sub("@[A-Za-z0-9_]+","", r) | ||
r = re.sub("#[A-Za-z0-9_]+","", r) | ||
r = re.sub(r'http\S+', '', r) | ||
return r |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import os | ||
import sys | ||
from dotenv import load_dotenv | ||
from pymongo import MongoClient | ||
from twitter import twitter | ||
from cleaner import cleaner | ||
|
||
def main(): | ||
db = new_database() | ||
client = twitter.Twitter( | ||
os.environ.get("consumer_key"), | ||
os.environ.get("consumer_secret"), | ||
os.environ.get("access_token"), | ||
os.environ.get("access_secret") | ||
) | ||
clean = cleaner.Cleaner() | ||
|
||
try: | ||
query = str(input("Enter a search query: ")) | ||
tweets = client.get_tweets(query, 1, "4.570868,-74.297333,100km") | ||
except: | ||
print("Could not get tweets") | ||
sys.exit(1) | ||
|
||
|
||
for tweet in tweets: | ||
tw = { | ||
"text": clean.clean_tweet(tweet.text), | ||
"user": tweet.user.screen_name, | ||
"location": tweet.user.location, | ||
"date": tweet.created_at | ||
} | ||
|
||
try: | ||
tw_id = db.tweets.insert_one(tw).inserted_id | ||
print("Tweet inserted with id: ", tw_id) | ||
except: | ||
print("Could not insert tweet") | ||
|
||
def new_database(): | ||
try: | ||
client = MongoClient(os.environ.get("mongo_uri")) | ||
db = client.test | ||
return db | ||
except: | ||
print("Could not connect to MongoDB") | ||
|
||
if __name__ == "__main__": | ||
load_dotenv() | ||
main() |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import tweepy | ||
|
||
class Twitter: | ||
def __init__(self, consumer_key, consumer_secret, access_token, access_secret): | ||
self.consumer_key = consumer_key | ||
self.consumer_secret = consumer_secret | ||
self.access_token = access_token | ||
self.access_secret = access_secret | ||
self.auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | ||
self.auth.set_access_token(access_token, access_secret) | ||
self.api = tweepy.API(self.auth) | ||
|
||
def get_tweets(self, query, count, geocode, lang="es", result_type="recent"): | ||
filtered = query + "-filter:retweets" | ||
|
||
tweets = tweepy.Cursor( | ||
self.api.search_tweets, | ||
q=filtered, | ||
lang=lang, | ||
result_type=result_type, | ||
geocode=geocode | ||
).items(count) | ||
|
||
return tweets |