feat(socialmedia):added twitter service

This commit is contained in:
bobbyvish
2024-07-17 00:37:06 +05:30
parent d670a18599
commit 29011dff06
3 changed files with 46 additions and 2 deletions

View File

@@ -1,5 +1,6 @@
import random
import googlemaps
import tweepy
from django.conf import settings
from django.core.files.uploadedfile import UploadedFile
from django.core.mail import EmailMessage
@@ -745,7 +746,7 @@ class GoogleMapsservice:
def get_place_id_from_coordinates(self, latitude, longitude):
"""
Get the place ID of the given coordinates.
:param latitude: Latitude of the location
:param longitude: Longitude of the location
:return: Place ID
@@ -817,3 +818,39 @@ class GoogleMapsservice:
# queryset = queryset.order_by(preserved_order)
return queryset
class TwitterAPI:
def __init__(self):
self.api_key = settings.TWITTER_API_KEY
self.api_secret_key = settings.TWITTER_API_SECRET_KEY
self.access_token = settings.TWITTER_ACCESS_TOKEN
self.access_token_secret = settings.TWITTER_ACCESS_TOKEN_SECRET
self.auth = self._setup_auth()
self.api = self._setup_api()
def _setup_auth(self):
auth = tweepy.OAuthHandler(self.api_key, self.api_secret_key)
auth.set_access_token(self.access_token, self.access_token_secret)
return auth
def _setup_api(self):
api = tweepy.API(self.auth)
return api
def post_image_with_caption(self, image_path, caption):
media = self.api.media_upload(image_path)
tweet = self.api.update_status(status=caption, media_ids=[media.media_id])
return tweet
class TwitterPoster:
def __init__(self, twitter_api):
self.twitter_api = twitter_api
def post_tweet(self, image_path, caption):
try:
tweet = self.twitter_api.post_image_with_caption(image_path, caption)
return {'success': True, 'message': 'Tweet posted successfully!'}
except tweepy.TweepError as e:
return {'success': False, 'message': f'Error posting tweet: {e}'}

View File

@@ -339,3 +339,9 @@ PLACES_MAPS_API_KEY = env.str("GOOGLE_MAPS_API_KEY")
PLACES_MAP_WIDGET_HEIGHT = 480
PLACES_MAP_OPTIONS = '{"center": { "lat": 38.971584, "lng": -95.235072 }, "zoom": 10}'
PLACES_MARKER_OPTIONS = '{"draggable": true}'
# twitter keys
TWITTER_API_KEY = env.str("TWITTER_API_KEY")
TWITTER_API_SECRET_KEY = env.str("TWITTER_API_SECRET_KEY")
TWITTER_ACCESS_TOKEN = env.str("TWITTER_ACCESS_TOKEN")
TWITTER_ACCESS_TOKEN_SECRET = env.str("TWITTER_ACCESS_TOKEN_SECRET")

View File

@@ -69,8 +69,9 @@ sniffio==1.3.1
sqlparse==0.4.4
stripe==8.2.0
tqdm==4.66.2
tweepy==4.14.0
Twisted==23.10.0
# twisted-iocpsupport==1.0.4
twisted-iocpsupport==1.0.4
txaio==23.1.1
typing_extensions==4.9.0
tzdata==2024.1