Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Created Workers #3

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions workers/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#TWITTER API TOKENS AND KEYS
BEARER_TOKEN = AAAAAAAAAAAAAAAAAAAAAPGxCQEAAAAADbQQiAp5ZEJ%2BoV1Ze7fmQwY27Xk%3DdqnLLg4GcUrhVz5zYpkplPxQ7DZNnBlICYOhngVeVrtcHQabQh
ACC_TOKEN = 1226371381165576193-q5jurLd4dAqCNF2dpZPSmqbdxFr5RJ
ACC_TOKEN_SECRET = 4ThKuRMbP538csiDvfLCM5EF9eeLsErKQXcJEd9eFt4wy
3 changes: 2 additions & 1 deletion workers/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ verify_ssl = true

[packages]
requests = "*"
base64 = "*"
psycopg2 = "*"
python-dotenv = "*"

[requires]
python_version = "3.8"
29 changes: 28 additions & 1 deletion workers/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 30 additions & 19 deletions workers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,44 @@
## Installation
Ensure that pip and python is installed

`````
install pipenv for dependencies management
install pipenv for dependencies management. List of dependencies will be listed on Pipfile. Make sure the pipenv path is added to the system.
```
pip install --user pipenv
make sure the path is added to the system
`````
```

install Requests library for HTTP request
```
pipenv install requests
list of dependencies will be listed on Pipfile
```

install psycopg2 for library to access PostgreSQL Database
```
pipenv install psycopg2
```

install python-dotenv to add environment variables into the app
```
pip install python-dotenv
```

## Authentication for Twitter
Create an account folloed by an app
https://developer.twitter.com/en/account/get-started
`````
Create an account followed by application creation on Twitter Developer Account. More information can be found in https://developer.twitter.com/en/account/get-started
Get your application tokens at "Keys and Access Tokens"
consumer key, consumer secret key, access token, access token secret
`````
Use get_bearer_token.py to get Bearer Token
- consumer key
- consumer secret key
- access token
- access token secret

Beare Token can be generated from get_bearer_token.py
Bearer Token is required to use the Request lib for Twitter API
`````

`````
## Fetch Data from API
Insert the Bearer Token into worker.py
`````
- Insert the Bearer Token into worker.py
- Insert params for the Twitter API
Info about the endpoint of Twitter API https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets#

## Environment variables set up
- Create a .env file and input the variables
- Call dotenv library and os to get the desired variables


Insert params for the Twitter API
Info about the endpoint of Twitter API
https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets#
When the response status is 200, JSON formatted data will be returned
14 changes: 14 additions & 0 deletions workers/connect_to_postgres.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import psycopg2

def connection() :
try :
connection_details = "dbname='makeupdb' user='icebear' host='localhost' port='5432'"
Icebearbear marked this conversation as resolved.
Show resolved Hide resolved
connection = psycopg2.connect(connection_details)
connection.autocommit = True
cursor = connection.cursor()
except :
print('connection unsuccessful')


if __name__ == '__main__' :
connection()
13 changes: 7 additions & 6 deletions workers/worker.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import requests
import os
from dotenv import load_dotenv
load_dotenv()

base_url = 'https://api.twitter.com/'
search_url = '{}1.1/search/tweets.json'.format(base_url)
access_token = 'AAAAAAAAAAAAAAAAAAAAAPGxCQEAAAAADbQQiAp5ZEJ%2BoV1Ze7fmQwY27Xk%3DdqnLLg4GcUrhVz5zYpkplPxQ7DZNnBlICYOhngVeVrtcHQabQh'

access_token = os.getenv('BEARER_TOKEN')

search_headers = {
'Authorization': 'Bearer {}'.format(access_token)
Expand All @@ -15,14 +17,13 @@
'count': 2
}


search_resp = requests.get(search_url, headers=search_headers, params=search_params)

if search_resp.status_code == 200 :
tweet_data = search_resp.json()
for tweet in tweet_data["statuses"] :
# print(tweet['entities'])
for t in tweet['entities']['user_mentions'] :
print(t)
print(tweet['entities'])
# for t in tweet['entities']['user_mentions'] :
# print(t)
else :
print('Result for' , search_url , 'is unsuccesful')