61 lines
1.8 KiB
Python
61 lines
1.8 KiB
Python
import threading
|
|
from datetime import datetime
|
|
import time
|
|
|
|
from app_be.models import Tweet, Feed
|
|
from app_be.views.twitter_api import twitter_api
|
|
from feedsearch import search
|
|
|
|
|
|
class twitter_bot(threading.Thread):
|
|
|
|
def __init__(self):
|
|
threading.Thread.__init__(self)
|
|
|
|
def run(self):
|
|
|
|
posted = False
|
|
|
|
while True:
|
|
|
|
print("Looking for new results in active feeds")
|
|
feed_set = Feed.objects.filter(active=True)
|
|
|
|
for feed in feed_set:
|
|
if posted:
|
|
break
|
|
|
|
#load feed
|
|
feeds = search(feed.url)
|
|
|
|
#search single posts
|
|
for current in feeds:
|
|
print(current)
|
|
|
|
#TODO: split keywords and search
|
|
#looking for keywords
|
|
if feed.keywords in current.title:
|
|
if posted:
|
|
break
|
|
|
|
#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 tweet not in db yet
|
|
if not Tweet.objects.filter(text=new_tweet.text):
|
|
# call twitter api to post tweet and store to db
|
|
print("Posting update on Twitter")
|
|
print(new_tweet)
|
|
|
|
#TODO: uncomment
|
|
#twitter_api.post_update(new_tweet.text, new_tweet.url)
|
|
new_tweet.save()
|
|
posted = True
|
|
|
|
time.sleep(60)
|
|
posted = False
|