Skip to content

Commit

Permalink
Add .gitignore and Twitter class implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
aliirz committed Mar 29, 2024
1 parent 5fcf3e2 commit 4859773
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
19 changes: 9 additions & 10 deletions .gitignore
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
42 changes: 42 additions & 0 deletions classes/twitter/__init__.py
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

0 comments on commit 4859773

Please sign in to comment.