-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
101 lines (90 loc) · 3.35 KB
/
main.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
import requests
import json
import time
import argparse
# Define the command line arguments
parser = argparse.ArgumentParser(description='Monitor the Unisat market and send Discord notifications for new listings and sales')
parser.add_argument('--ticker', type=str, help='the ticker of the token to monitor', required=True)
parser.add_argument('--discord_webhook', type=str, help='the Discord webhook URL to send notifications to', required=True)
parser.add_argument('--event', type=str, help='the type of event to send notifications for (sold, listed)', default='both')
args = parser.parse_args()
# Set up the initial state
prev_listings = []
# Define the URL and payload
url = 'https://market-api.unisat.io/unisat-market-v2/auction/actions'
payload = {
"filter": {"tick": args.ticker},
"start": 0,
"limit": 5
}
# Define a function to send the Discord webhook
def send_discord_webhook(listings, event):
# Create the Discord message
message = {
"channel_id": "CHANNEL_ID",
"content": "",
"tts": False,
"embeds": [
{
"type": "rich",
"title": "New Sales 🚀" if event == "Sold" else "New Listings 👹",
"description": "",
"color": 0x9ecf61 if event == "Sold" else 0x619acf,
"fields": [],
"footer": {
"text": "@muncherverse brc-20 Bot 🤖"
},
"url": f"https://unisat.io/market?tick={args.ticker}&tab=2"
}
]
}
# Add the listings to the message
for listing in listings:
unit_price = listing['unitPrice']
amount = listing['amount']
price = listing['price']
name = listing['name']
field_name = f"{amount:,} ${name}"
field_value = f"{price:,} sats ({unit_price:,} sats/${name})"
message['embeds'][0]['fields'].append({
"name": field_name,
"value": field_value,
"inline": False
})
data = {
"content": "",
"embeds": message['embeds']
}
# Send the webhook
requests.post(args.discord_webhook, json=data)
# Loop forever
while True:
try:
response = requests.post(url, json=payload)
response.raise_for_status()
data = json.loads(response.text)['data']
new_listings = data['list']
except requests.exceptions.RequestException as e:
time.sleep(60)
continue
except json.JSONDecodeError as e:
time.sleep(60)
continue
# Check for new listings
new_sold_listings = []
new_listed_listings = []
for listing in new_listings:
if listing not in prev_listings:
if listing['event'] == 'Sold':
new_sold_listings.append(listing)
elif listing['event'] == 'Listed':
new_listed_listings.append(listing)
# Send the Discord webhooks for new listings
if len(new_sold_listings) > 0 and (args.event == 'sold' or args.event == 'both'):
send_discord_webhook(new_sold_listings, 'Sold')
if len(new_listed_listings) > 0 and (args.event == 'listed' or args.event == 'both'):
send_discord_webhook(new_listed_listings, 'Listed')
# Update the previous listings
prev_listings = new_listings
# Wait for a minute before making the next request
time.sleep(60)