diff --git a/backend/app_be/views/twitter.py b/backend/app_be/views/twitter.py index b643389..b26726e 100644 --- a/backend/app_be/views/twitter.py +++ b/backend/app_be/views/twitter.py @@ -1,9 +1,10 @@ import twitter import os -import sched, time - -s = sched.scheduler(time.time(), time.sleep) +import sched, time, datetime +import asyncio +import sys +from app_be.models import Tweet from dotenv import load_dotenv load_dotenv() @@ -22,11 +23,13 @@ class twitter_api: @staticmethod def get_last_six_tweets(): - print(api.GetUserTimeline(screen_name='WAECMG4', count=6)) + last_six = Tweet.objects.all().order_by('-id')[:6] + print(last_six) @staticmethod def get_last_twelve_tweets(): - print(api.GetUserTimeline(screen_name='WAECMG4', count=12)) + last_twelve = Tweet.objects.all().order_by('-id')[:12] + print(last_twelve) @staticmethod def post_update(text, url): @@ -37,14 +40,29 @@ class twitter_api: class twitter_bot: def scan_active_feed(self): + starttime = time.time() while True: - # Search RSS Feed + # Search RSS Feeds TODO: for looping over feeds + print("Looking for new results in active feeds") + new_tweet = Tweet() + new_tweet.icon = None + new_tweet.text = "Test 1" + new_tweet.date_time = datetime.datetime.now() + new_tweet.url = "www.google.com" + + # if tweet not in db yet + if Tweet.objects.filter(text=new_tweet.text) == []: + # call twitter api to post tweet and store to db + print("Posting update on Twitter") + twitter_api.post_update(new_tweet.text, new_tweet.url) + new_tweet.save() time.sleep(60.0 - ((time.time() - starttime) % 60.0)) -twitter_bot = twitter_bot() -twitter_bot.scan_active_feed() +if 'runserver' in sys.argv: + twitter_bot = twitter_bot() + twitter_bot.scan_active_feed()