-
Notifications
You must be signed in to change notification settings - Fork 166
/
Copy pathnotifier.py
102 lines (81 loc) · 2.79 KB
/
notifier.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# -*- coding: utf-8 -*-
import json
import random
import sys
import time
from hashlib import md5
from os import isatty
import twitter
from enums.emoji import Emoji
MAX_TWEET_LENGTH = 279
DELAY_FILE_TEMPLATE = "next_{}.txt"
DELAY_TIME = 1800
CREDENTIALS_FILE = "twitter_credentials.json"
def _create_tweet(tweet, tc):
tweet = tweet[:MAX_TWEET_LENGTH]
api = twitter.Api(
consumer_key=tc["consumer_key"],
consumer_secret=tc["consumer_secret"],
access_token_key=tc["access_token_key"],
access_token_secret=tc["access_token_secret"],
)
resp = api.PostUpdate(tweet)
# api.CreateFavorite(resp)
print("The following was tweeted: ")
print()
print(tweet)
def main(args, stdin):
with open(CREDENTIALS_FILE) as f:
tc = json.load(f)
# Janky simple argument parsing.
if len(args) != 2:
print("Please provide the user you want to tweet at!")
sys.exit(1)
user = args[1].replace("@", "")
first_line = next(stdin)
first_line_hash = md5(first_line.encode("utf-8")).hexdigest()
delay_file = DELAY_FILE_TEMPLATE.format(first_line_hash)
try:
with open(delay_file, "r") as f:
call_time = int(f.read().rstrip())
except:
call_time = 0
if call_time + random.randint(DELAY_TIME - 30, DELAY_TIME + 30) > int(
time.time()
):
print("It is too soon to tweet again")
sys.exit(0)
if "Something went wrong" in first_line:
_create_tweet("{}, I'm broken! Please help :'(".format(user), tc)
sys.exit()
available_site_strings = generate_availability_strings(stdin)
if available_site_strings:
tweet = generate_tweet_str(available_site_strings, first_line, user)
_create_tweet(tweet, tc)
with open(delay_file, "w") as f:
f.write(str(int(time.time())))
sys.exit(0)
else:
print("No campsites available, not tweeting 😞")
sys.exit(1)
def generate_tweet_str(available_site_strings, first_line, user):
tweet = "@{}!!! ".format(user)
tweet += first_line.rstrip()
tweet += " 🏕🏕🏕\n"
tweet += "\n".join(available_site_strings)
tweet += "\n" + "🏕" * random.randint(5, 20) # To avoid duplicate tweets.
return tweet
def generate_availability_strings(stdin):
available_site_strings = []
for line in stdin:
line = line.strip()
if Emoji.SUCCESS.value in line:
park_name_and_id = " ".join(line.split(":")[0].split(" ")[1:])
num_available = line.split(":")[1][1].split(" ")[0]
s = "{} site(s) available in {}".format(
num_available, park_name_and_id
)
available_site_strings.append(s)
return available_site_strings
if __name__ == "__main__":
main(sys.argv, sys.stdin)