-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtweet.py
70 lines (60 loc) · 2.12 KB
/
tweet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import fetch
import time
from datetime import datetime, timedelta
# Returns datetime object of current time in UTC
def curTime():
return datetime.utcnow().replace(microsecond=0) - timedelta(
seconds=datetime.utcnow()
.replace(hour=0, minute=0, second=0, microsecond=0)
.time()
.second,
minutes=datetime.utcnow()
.replace(hour=0, minute=0, second=0, microsecond=0)
.time()
.minute,
hours=datetime.utcnow()
.replace(hour=0, minute=0, second=0, microsecond=0)
.time()
.hour,
)
# Given a string of format 'day month date hh:mm:ss year', returns a datetime object
def getTimeString(timeString):
if isinstance(timeString, list):
dateString = " ".join(timeString)
dateList = datetime.strptime(dateString, "%a %b %d %H:%M:%S %Y")
return dateList
# Returns datetime object of tweet time given {tweet} dict
def getTweetTime(tweet):
tweetTimeString = tweet["time"]
tweetTime = tweetTimeString.split()
del tweetTime[4]
return getTimeString(tweetTime)
# Main script, which fetches and checks for tweets
def fetchTweets(userDict, sleepTime):
startTime = curTime()
print("\nChecking for tweets...")
while True:
# Tweet Checker
tweetFound = False
for username, userID in userDict.items():
tweetList = fetch.getTweets(userID, 2)
for tweet in tweetList:
tweetTime = getTweetTime(tweet)
if tweetTime > startTime:
print(
f"New Tweet by {username}.\n> Contents: {tweet['contents']}\n"
)
tweetFound = True
if not tweetFound:
print("No new tweets.")
timeLeft = sleepTime
# Countdown Timer
while timeLeft:
minutesLeft, secondsLeft = divmod(timeLeft, 60)
countdownTmer = "Refreshing in: {:02d}:{:02d}".format(
minutesLeft, secondsLeft
)
print(countdownTmer, end="\r")
timeLeft -= 1
time.sleep(1)
print("\n\nChecking for tweets...")