106 lines
3.4 KiB
Python
106 lines
3.4 KiB
Python
import logging
|
|
|
|
from datetime import datetime
|
|
|
|
import twitter
|
|
import os
|
|
|
|
from app_be.models import Tweet
|
|
from dotenv import load_dotenv
|
|
from django.conf import settings
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
load_dotenv()
|
|
|
|
api = twitter.Api(consumer_key=os.getenv('TWITTER_API_KEY'),
|
|
consumer_secret=os.getenv('TWITTER_API_SECRET'),
|
|
access_token_key=os.getenv('TWITTER_ACCESS_TOKEN'),
|
|
access_token_secret=os.getenv('TWITTER_ACCESS_TOKEN_SECRET'))
|
|
|
|
|
|
class twitter_api:
|
|
|
|
@staticmethod
|
|
def check_credentials():
|
|
logger.debug("{}".format(api.VerifyCredentials()))
|
|
|
|
@staticmethod
|
|
def get_last_six_tweets():
|
|
|
|
last_six = api.GetUserTimeline(screen_name='waecmg4', count=6, exclude_replies=True, include_rts=False)
|
|
logger.debug("type(last_six): {}".format(type(last_six)))
|
|
|
|
response = []
|
|
|
|
for tweet in last_six:
|
|
logger.debug("Tweet: {}".format(tweet))
|
|
|
|
parsed_date = datetime.strptime(tweet.created_at, "%a %b %d %H:%M:%S +0000 %Y")
|
|
tweet_date = datetime.strftime(parsed_date, "%d.%m.%y %H:%M")
|
|
|
|
if tweet.urls:
|
|
twitter_url = tweet.urls[0].expanded_url
|
|
else:
|
|
twitter_url = "No url for tweet found"
|
|
|
|
tmp_text = str(tweet.text)
|
|
end = tmp_text.find(": http")
|
|
tmp_text = tmp_text[0:end]
|
|
backend_tweet: Tweet = Tweet.objects.filter(text=tmp_text)
|
|
|
|
if not backend_tweet:
|
|
icon = "http://localhost:8000/media/default-icon.png"
|
|
else:
|
|
icon = "http://localhost:8000/media/feed-icons/" + os.path.basename(backend_tweet[0].icon.file.name)
|
|
|
|
response.append({'icon': icon, 'text': tweet.text, 'url': twitter_url, 'created_date': tweet_date,
|
|
'tweet_id': tweet.id})
|
|
|
|
return response
|
|
|
|
@staticmethod
|
|
def get_more_tweets(twitter_id):
|
|
|
|
last_six = api.GetUserTimeline(screen_name='waecmg4', count=7, exclude_replies=True, include_rts=False,
|
|
max_id=twitter_id)
|
|
|
|
response = []
|
|
|
|
for tweet in last_six[1:]:
|
|
logger.debug("Tweet: {}".format(tweet))
|
|
|
|
parsed_date = datetime.strptime(tweet.created_at, "%a %b %d %H:%M:%S +0000 %Y")
|
|
tweet_date = datetime.strftime(parsed_date, "%d.%m.%y %H:%M")
|
|
|
|
if tweet.urls:
|
|
twitter_url = tweet.urls[0].expanded_url
|
|
else:
|
|
twitter_url = "No url for tweet found"
|
|
|
|
tmp_text = str(tweet.text)
|
|
end = tmp_text.find(": http")
|
|
tmp_text = tmp_text[0:end]
|
|
backend_tweet: Tweet = Tweet.objects.filter(text=tmp_text)
|
|
|
|
if not backend_tweet:
|
|
icon = "http://localhost:8000/media/default-icon.png"
|
|
else:
|
|
icon = "http://localhost:8000/media/feed-icons/" + os.path.basename(backend_tweet[0].icon.file.name)
|
|
|
|
response.append(
|
|
{'icon': icon, 'text': tweet.text, 'url': twitter_url, 'created_date': tweet_date,
|
|
'tweet_id': tweet.id})
|
|
|
|
return response
|
|
|
|
@staticmethod
|
|
def post_update(text, url):
|
|
tweet = text + ": " + url
|
|
|
|
if len(text) > 140:
|
|
surplus = len(text) - 140
|
|
tweet = text[:len(text) - (surplus + 3)] + "...: " + url
|
|
|
|
api.PostUpdate(tweet)
|