Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
frandier committed Oct 9, 2022
0 parents commit f0d9795
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .env.example
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 = "###############"
46 changes: 46 additions & 0 deletions .gitignore
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# spamerio
Empty file added cleaner/__init__.py
Empty file.
9 changes: 9 additions & 0 deletions cleaner/cleaner.py
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
50 changes: 50 additions & 0 deletions main.py
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 added twitter/__init__.py
Empty file.
24 changes: 24 additions & 0 deletions twitter/twitter.py
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

0 comments on commit f0d9795

Please sign in to comment.