69 lines
1.8 KiB
Python
69 lines
1.8 KiB
Python
import twitter
|
|
import os
|
|
import sched, time, datetime
|
|
import asyncio
|
|
import sys
|
|
|
|
from app_be.models import Tweet
|
|
from dotenv import load_dotenv
|
|
|
|
load_dotenv()
|
|
|
|
api = twitter.Api(consumer_key=os.getenv('TWITTER_API_KEY'),
|
|
consumer_secret=os.getenv('TWITTER_API_SECRET'),
|
|
access_token_key=os.getenv('TWITTER_ACCESS_TOKEN'),
|
|
access_token_secret=os.getenv('TWITTER_ACCESS_TOKEN_SECRET'))
|
|
|
|
|
|
class twitter_api:
|
|
|
|
@staticmethod
|
|
def check_credentials():
|
|
print(api.VerifyCredentials())
|
|
|
|
@staticmethod
|
|
def get_last_six_tweets():
|
|
last_six = Tweet.objects.all().order_by('-id')[:6]
|
|
print(last_six)
|
|
|
|
@staticmethod
|
|
def get_last_twelve_tweets():
|
|
last_twelve = Tweet.objects.all().order_by('-id')[:12]
|
|
print(last_twelve)
|
|
|
|
@staticmethod
|
|
def post_update(text, url):
|
|
update = text + ": " + url
|
|
api.PostUpdate(update)
|
|
|
|
|
|
class twitter_bot:
|
|
|
|
def scan_active_feed(self):
|
|
|
|
starttime = time.time()
|
|
|
|
while True:
|
|
# 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))
|
|
|
|
|
|
if 'runserver' in sys.argv:
|
|
twitter_bot = twitter_bot()
|
|
twitter_bot.scan_active_feed()
|