Twitter bot finished

This commit is contained in:
Manuel Hude 2021-05-03 22:06:57 +02:00
parent 55f41cabb8
commit 66fe5be68e
2 changed files with 55 additions and 19 deletions

View File

@ -19,15 +19,25 @@ class twitter_api:
@staticmethod
def get_last_six_tweets():
last_six = Tweet.objects.all().order_by('-id')[:6]
last_six = api.GetUserTimeline('waecmg4',count=6)
print(last_six)
@staticmethod
def get_last_twelve_tweets():
last_twelve = Tweet.objects.all().order_by('-id')[:12]
last_twelve = api.GetUserTimeline('waecmg4',count=12)
print(last_twelve)
@staticmethod
def get_last_n_tweets(n):
last_twelve = api.GetUserTimeline('waecmg4',count=n)
print(last_twelve)
@staticmethod
def post_update(text, url):
update = text + ": " + url
api.PostUpdate(update)
tweet = text + ": " + url
if len(text) > 140:
surplus = len(text) - 140
tweet = text[:len(text) - (surplus + 3)] + "...: " + url
api.PostUpdate(tweet)

View File

@ -4,7 +4,8 @@ import time
from app_be.models import Tweet, Feed
from app_be.views.twitter_api import twitter_api
from feedsearch import search
import feedparser
from dateutil import parser
class twitter_bot(threading.Thread):
@ -26,25 +27,35 @@ class twitter_bot(threading.Thread):
break
# load feed
feeds = search(feed.url)
feeds = feedparser.parse(feed.url)
keywords = list(x.strip() for x in feed.keywords.split(','))
# search single posts
for current in feeds:
print(current)
for current in feeds.entries:
# TODO: split keywords and search
# split_keywords = [x.strip() for x in feed.keywords.split(',')]
# looking for keywords
if feed.keywords in current.title:
if posted:
break
if not 'title' in current.keys():
continue
if posted:
break
if search_keywords(keywords, current, feed.match_all_keywords):
print("Keyword found")
# preparing tweet
new_tweet = Tweet()
new_tweet.icon = None
new_tweet.text = current.title
new_tweet.date_time = datetime.now()
new_tweet.url = current.url
if 'published' in current:
new_tweet.date_time = parser.parse(current.published)
elif 'upddated' in current:
new_tweet.date_time = parser.parse(current.updated)
else:
new_tweet.date_time = datetime.now()
new_tweet.url = current.link
# if tweet not in db yet
if not Tweet.objects.filter(text=new_tweet.text):
@ -52,10 +63,25 @@ class twitter_bot(threading.Thread):
print("Posting update on Twitter")
print(new_tweet)
# TODO: uncomment
# twitter_api.post_update(new_tweet.text, new_tweet.url)
twitter_api.post_update(new_tweet.text, new_tweet.url)
new_tweet.save()
posted = True
time.sleep(60)
time.sleep(65)
posted = False
def search_keywords(keywords, current_entry, match_all):
if 'title' in current_entry and not 'summary' in current_entry:
if match_all:
return all(keyword.lower() in current_entry.title.lower() for keyword in keywords)
else:
return any(keyword.lower() in current_entry.title.lower() for keyword in keywords)
if 'title' in current_entry and 'summary' in current_entry:
if match_all:
return all(keyword.lower() in current_entry.title.lower() + current_entry.summary.lower() for keyword in keywords)
else:
return any(keyword.lower() in current_entry.title.lower() + current_entry.summary.lower() for keyword in keywords)
return False