-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtooGoodToGo.py
More file actions
57 lines (44 loc) · 1.83 KB
/
tooGoodToGo.py
File metadata and controls
57 lines (44 loc) · 1.83 KB
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
#! /usr/bin/python3
import requests
import json
from tgtg import TgtgClient
import os
CONFIG_FILE = f"{os.path.dirname(os.path.realpath(__file__))}/config.json"
def send_notification(subject, article_name):
s = requests.Session()
headers = {
"Title": "Panier disponible !",
"Priority": "urgent",
"Tags": "tada"
}
data = f"Un nouveau panier est disponible : {article_name}".encode("utf-8")
s.post(f"https://ntfy.sh/{subject}", headers=headers, data=data)
# Read config file to get all last available articles (avoid spam)
# Recover account config
subject, last_available_articles, access_token, refresh_token, user_id = None, None, None, None, None
with open(CONFIG_FILE, "r") as f:
config = json.load(f)
subject = config["subject"]
access_token = config["access_token"]
refresh_token = config["refresh_token"]
user_id = config["user_id"]
last_available_articles = config["available"]
# Create tgtg client object
client = TgtgClient(access_token=access_token, refresh_token=refresh_token, user_id=user_id)
# Query favourites
items = client.get_items()
# For each item, check if there is at least one available, and if it was not already available previously
# If a previously availabe article is not availabe anymore, remove from availibility list
for item in items:
nb_items = int(item["items_available"])
item_name = item["display_name"]
item_id = int(item["item"]["item_id"])
if nb_items > 0 and item_id not in last_available_articles:
last_available_articles.append(item_id)
send_notification(subject, item_name)
elif nb_items == 0 and item_id in last_available_articles:
last_available_articles.remove(item_id)
# Update config file
config["available"] = last_available_articles
with open(CONFIG_FILE, "w") as f:
json.dump(config, f)