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 @staticmethod
def get_last_six_tweets(): def get_last_six_tweets():
last_six = Tweet.objects.all().order_by('-id')[:6] last_six = api.GetUserTimeline('waecmg4',count=6)
print(last_six) print(last_six)
@staticmethod @staticmethod
def get_last_twelve_tweets(): 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) print(last_twelve)
@staticmethod @staticmethod
def post_update(text, url): def post_update(text, url):
update = text + ": " + url tweet = text + ": " + url
api.PostUpdate(update)
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.models import Tweet, Feed
from app_be.views.twitter_api import twitter_api from app_be.views.twitter_api import twitter_api
from feedsearch import search import feedparser
from dateutil import parser
class twitter_bot(threading.Thread): class twitter_bot(threading.Thread):
@ -26,25 +27,35 @@ class twitter_bot(threading.Thread):
break break
# load feed # load feed
feeds = search(feed.url) feeds = feedparser.parse(feed.url)
keywords = list(x.strip() for x in feed.keywords.split(','))
# search single posts # search single posts
for current in feeds: for current in feeds.entries:
print(current)
# TODO: split keywords and search if not 'title' in current.keys():
# split_keywords = [x.strip() for x in feed.keywords.split(',')] continue
# looking for keywords
if feed.keywords in current.title: if posted:
if posted: break
break
if search_keywords(keywords, current, feed.match_all_keywords):
print("Keyword found")
# preparing tweet # preparing tweet
new_tweet = Tweet() new_tweet = Tweet()
new_tweet.icon = None new_tweet.icon = None
new_tweet.text = current.title 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 tweet not in db yet
if not Tweet.objects.filter(text=new_tweet.text): if not Tweet.objects.filter(text=new_tweet.text):
@ -52,10 +63,25 @@ class twitter_bot(threading.Thread):
print("Posting update on Twitter") print("Posting update on Twitter")
print(new_tweet) 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() new_tweet.save()
posted = True posted = True
time.sleep(60) time.sleep(65)
posted = False 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