-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add .gitignore and Twitter class implementation
- Loading branch information
Showing
2 changed files
with
51 additions
and
10 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 |
---|---|---|
@@ -1,10 +1,9 @@ | ||
venv/ | ||
venv/ # Ignore virtual environment folder | ||
*.pyc # Ignore compiled Python files | ||
__pycache__/ # Ignore Python cache folder | ||
*.egg-info/ # Ignore Python package metadata | ||
dist/ # Ignore distribution folder | ||
build/ # Ignore build artifacts | ||
*.log # Ignore log files | ||
.env # Ignore environment file | ||
.streamlit/secrets.toml # Ignore Streamlit secrets file | ||
venv/ | ||
*.pyc | ||
__pycache__/ | ||
*.egg-info/ | ||
dist/ | ||
build/ | ||
*.log | ||
.env | ||
.streamlit/secrets.toml |
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,42 @@ | ||
import os | ||
from requests_oauthlib import OAuth1Session | ||
import json | ||
|
||
|
||
class Twitter: | ||
|
||
def __init__(self): | ||
self.api_key = os.environ.get('CONSUMER_KEY') | ||
self.api_secret = os.environ.get('CONSUMER_SECRET') | ||
self.access_token = os.environ.get('ACCESS_TOKEN') | ||
self.access_token_secret = os.environ.get('ACCESS_TOKEN_SECRET') | ||
|
||
self.oauth = OAuth1Session( | ||
self.api_key, | ||
client_secret=self.api_secret, | ||
resource_owner_key=self.access_token, | ||
resource_owner_secret=self.access_token_secret | ||
) | ||
|
||
def post_tweet(self, tweet): | ||
# Twitter API v2 endpoint for posting tweets | ||
url = 'https://api.twitter.com/2/tweets' | ||
|
||
|
||
payload = { | ||
'text': tweet | ||
} | ||
|
||
headers = { | ||
'Content-Type': 'application/json' | ||
} | ||
|
||
print(json.dumps(payload)) # Optional: For debugging | ||
|
||
response = self.oauth.post(url, headers=headers, data=json.dumps(payload)) | ||
|
||
if response.status_code == 201: # Check for successful tweet creation | ||
return response.json() | ||
else: | ||
print("Tweet failed:", response.text) | ||
return None |