From 66fe5be68e15f88827da9b6fb1eed45ae30f6cdb Mon Sep 17 00:00:00 2001 From: Manuel Hude Date: Mon, 3 May 2021 22:06:57 +0200 Subject: [PATCH] Twitter bot finished --- backend/app_be/views/twitter_api.py | 18 +++++++--- backend/app_be/views/twitter_bot.py | 56 +++++++++++++++++++++-------- 2 files changed, 55 insertions(+), 19 deletions(-) diff --git a/backend/app_be/views/twitter_api.py b/backend/app_be/views/twitter_api.py index 066aa84..8690c36 100644 --- a/backend/app_be/views/twitter_api.py +++ b/backend/app_be/views/twitter_api.py @@ -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) \ No newline at end of file + tweet = text + ": " + url + + if len(text) > 140: + surplus = len(text) - 140 + tweet = text[:len(text) - (surplus + 3)] + "...: " + url + + api.PostUpdate(tweet) diff --git a/backend/app_be/views/twitter_bot.py b/backend/app_be/views/twitter_bot.py index 0032e3f..88d2824 100644 --- a/backend/app_be/views/twitter_bot.py +++ b/backend/app_be/views/twitter_bot.py @@ -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