Tweet icons on frontend
This commit is contained in:
|
Before Width: | Height: | Size: 10 KiB After Width: | Height: | Size: 10 KiB |
@@ -9,7 +9,7 @@ class User(models.Model):
|
||||
class Feed(models.Model):
|
||||
url = models.TextField(blank=False, null=False, validators=[URLValidator(['http', 'https'])])
|
||||
active = models.BooleanField()
|
||||
icon = models.FileField(upload_to='feed-icons', blank=True, null=False, default='default-icon.svg',
|
||||
icon = models.FileField(upload_to='feed-icons', blank=True, null=False, default='default-icon.png',
|
||||
validators=[FileExtensionValidator(['png', 'svg'])])
|
||||
keywords = models.TextField(blank=False, null=False)
|
||||
match_all_keywords = models.BooleanField(blank=True, default=False)
|
||||
@@ -21,7 +21,7 @@ class FeedEntry(models.Model):
|
||||
|
||||
|
||||
class Tweet(models.Model):
|
||||
feed = models.ForeignKey(Feed, null=True, on_delete=models.SET_NULL)
|
||||
icon = models.FileField(blank=True, null=False, default='default-icon.png')
|
||||
text = models.CharField(max_length=500)
|
||||
date_time = models.DateTimeField()
|
||||
url = models.CharField(max_length=500)
|
||||
|
||||
@@ -2,7 +2,10 @@ from datetime import datetime
|
||||
|
||||
import twitter
|
||||
import os
|
||||
|
||||
from app_be.models import Tweet
|
||||
from dotenv import load_dotenv
|
||||
from django.conf import settings
|
||||
|
||||
load_dotenv()
|
||||
|
||||
@@ -21,34 +24,11 @@ class twitter_api:
|
||||
@staticmethod
|
||||
def get_last_six_tweets():
|
||||
|
||||
last_six = api.GetUserTimeline(screen_name='waecmg4',count=6,exclude_replies=True,include_rts=False)
|
||||
last_six = api.GetUserTimeline(screen_name='waecmg4', count=6, exclude_replies=True, include_rts=False)
|
||||
print(type(last_six))
|
||||
|
||||
response = []
|
||||
|
||||
for tweet in last_six:
|
||||
print(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"
|
||||
|
||||
response.append({'icon': None,'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=6, exclude_replies=True, include_rts=False, max_id=twitter_id)
|
||||
|
||||
response = []
|
||||
|
||||
for tweet in last_six:
|
||||
print(tweet)
|
||||
|
||||
@@ -60,8 +40,52 @@ class twitter_api:
|
||||
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=6, exclude_replies=True, include_rts=False,
|
||||
max_id=twitter_id)
|
||||
|
||||
response = []
|
||||
|
||||
for tweet in last_six:
|
||||
print(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': None, 'text': tweet.text, 'url': twitter_url, 'created_date': tweet_date,
|
||||
{'icon': icon, 'text': tweet.text, 'url': twitter_url, 'created_date': tweet_date,
|
||||
'tweet_id': tweet.id})
|
||||
|
||||
return response
|
||||
|
||||
@@ -46,7 +46,7 @@ class twitter_bot(threading.Thread):
|
||||
|
||||
# preparing tweet
|
||||
new_tweet = Tweet()
|
||||
new_tweet.feed = feed
|
||||
new_tweet.icon = feed.icon
|
||||
new_tweet.text = current.title
|
||||
|
||||
if 'published' in current:
|
||||
|
||||
Reference in New Issue
Block a user