-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhn500-to-simplepush.py
executable file
·37 lines (32 loc) · 1.47 KB
/
hn500-to-simplepush.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
#!/usr/bin/python3
import requests
import json
import sys, os
from simplepush import send
#TODO: Speed this script up. It currently takes ~28 seconds to finish.
# Fetches the IDs of top HN stories at the moment.
r = requests.get('https://hacker-news.firebaseio.com/v0/topstories.json')
simplepush_id = "{{ SIX CHARACTER SIMPLEPUSH ID HERE }}"
hn_id = ""
i = 0
score = 0
# Fetches the details from the first 30 story IDs (since the HN homepage contains 30 stories).
while i < 30:
hn_id = json.loads(r.text)[i]
r2 = requests.get('https://hacker-news.firebaseio.com/v0/item/' + str(hn_id) + '.json?print=pretty')
# Gets the score of the ID
score = json.loads(r2.text)['score']
# Checks if the score is bigger than 500 and proceeds if it is.
if score > 500:
print ("\nHN ID: " + str(hn_id))
# Open the history file
with open(".hn-to-simplepush.history", "r+") as history_file:
# Checks if the ID is already in the notification history.
line_found = any(str(hn_id) in line for line in history_file)
print("line_found = " + str(line_found))
if not line_found:
# Append the ID to a file so that this ID wouldn't trigger notifications over and over again every time you run the script.
history_file.write(str(hn_id) + "\n")
# Send a Simplepush notification
send(simplepush_id, json.loads(r2.text)['title'], json.loads(r2.text)['url'])
i = i + 1