faucet/gift.py

171 lines
5.4 KiB
Python
Raw Normal View History

2023-11-07 23:04:54 +11:00
import os
import dotenv
import json
2023-11-08 12:32:05 +11:00
import requests
import pyotp
import time
2023-11-07 23:04:54 +11:00
dotenv.load_dotenv()
loaded = False
gifts = []
2023-11-08 12:32:05 +11:00
nbcookie = os.getenv('cookie')
cookies = {"namebase-main": nbcookie}
nb_endpoint = "https://www.namebase.io/"
2023-11-08 12:48:17 +11:00
max_price = 5 # Max price to buy a domain at (in HNS)
2023-11-08 14:25:39 +11:00
previous_gifts = []
max_gifts_per_interval = 24 # Max gifts per interval
interval = 60*60*24 # 24 hours
2023-11-07 23:04:54 +11:00
2023-11-08 14:25:39 +11:00
if os.getenv('max_price') == 'true':
max_price = int(os.getenv('max_price'))
if os.getenv('max_gifts_per_interval') == 'true':
max_gifts_per_interval = int(os.getenv('max_gifts_per_interval'))
if os.getenv('interval') == 'true':
interval = int(os.getenv('interval'))
2023-11-07 23:24:09 +11:00
2023-11-08 15:55:38 +11:00
def gift(name,email,referer, ip,api=False):
2023-11-07 23:04:54 +11:00
global loaded
global gifts
2023-11-08 14:25:39 +11:00
global previous_gifts
recent_gifts = 0
for gift in previous_gifts:
if previous_gifts['time'] > time.time() - interval:
recent_gifts += 1
if recent_gifts > max_gifts_per_interval and ip != os.getenv('admin_ip'):
return "Too many gifts recently<br>Check back in a few minutes"
2023-11-07 23:04:54 +11:00
2023-11-07 23:11:08 +11:00
print("Name: " + name,flush=True)
print("Email: " + email,flush=True)
print("Referer: " + referer,flush=True)
print("IP: " + ip,flush=True)
2023-11-07 23:04:54 +11:00
path = '/data/gifts.json'
if os.getenv('local') == 'true':
path = './gifts.json'
# If the file doesn't exist, create it
if not os.path.isfile(path):
with open(path, 'w') as f:
f.write('[]')
# Load the file
if not loaded:
with open(path, 'r') as f:
gifts = json.load(f)
loaded = True
# Check if the user has already submitted
2023-11-08 15:55:38 +11:00
if ip != os.getenv('admin_ip') and not api:
2023-11-07 23:24:09 +11:00
for gift in gifts:
if gift['email'] == email:
return "You have already submitted a gift request"
if gift['ip'] == ip:
return "You have already submitted a gift request"
2023-11-07 23:04:54 +11:00
# Add the user to the list
gifts.append({
'name': name,
'email': email,
'referer': referer,
'ip': ip
})
2023-11-08 14:25:39 +11:00
previous_gifts.append({
'time': time.time()
})
2023-11-07 23:04:54 +11:00
# Save the file
with open(path, 'w') as f:
json.dump(gifts, f)
2023-11-07 23:11:08 +11:00
2023-11-08 12:32:05 +11:00
headers = {"Accept": "application/json", "Content-Type": "application/json"}
params = {"recipientEmail": email, "senderName": "Woodburn Faucet",
"note": "Enjoy your free domain! - Woodburn Faucet"}
names = requests.get(nb_endpoint + "/api/user/domains/owned?offset=0&sortKey=acquiredAt&sortDirection=desc&limit=100"
,headers=headers, cookies=cookies)
if names.status_code != 200:
return "Error getting names:<br>" + names.text
names = names.json()
if len(names['domains']) == 0:
domains_market = requests.get(nb_endpoint + "/api/domains/marketplace?offset=0&buyNowOnly=true&sortKey=price&sortDirection=asc&exclude=%2Cnumbers%2Chyphens%2Cunderscores&maxLength=10&offersOnly=false"
,headers=headers, cookies=cookies)
if domains_market.status_code != 200:
return "Error getting names:<br>" + domains_market.text
domains_market = domains_market.json()
if len(domains_market['domains']) == 0:
return "No domains available to gift<br>Check back in a few minutes"
domain = domains_market['domains'][0]['name']
print("Buying: " + domain,flush=True)
2023-11-08 13:01:40 +11:00
price = int(domains_market['domains'][0]['amount'])
2023-11-08 12:32:05 +11:00
if price > max_price*1000000:
return "Domain price too high<br>Check back in a few minutes"
payload = {
"listingId": domains_market['domains'][0]['id']
}
buy = requests.post(nb_endpoint + "/api/v0/marketplace/"+domain+"/buynow",headers=headers,data=json.dumps(payload), cookies=cookies)
if buy.status_code != 200:
return "Error buying name:<br>" + buy.text
else:
domain = names['domains'][0]['name']
print("Gifting: " + domain,flush=True)
# Add this name to gifts record
gifts[-1]['domain'] = domain
2023-11-08 12:48:17 +11:00
# Save the file
with open(path, 'w') as f:
json.dump(gifts, f)
2023-11-08 12:32:05 +11:00
send_name = requests.post(nb_endpoint + "/api/gift/" + domain.strip(),headers=headers,data=json.dumps(params), cookies=cookies)
if send_name.status_code != 200:
return "Error sending gift:<br>" + send_name.text
2023-11-08 14:31:52 +11:00
discord(domain,email,ip,referer)
2023-11-08 12:32:05 +11:00
2023-11-08 12:55:10 +11:00
return True
def balance():
headers = {"Accept": "application/json", "Content-Type": "application/json"}
user_info = requests.get(nb_endpoint + "/api/user",headers=headers, cookies=cookies)
if user_info.status_code != 200:
return "Error getting user info:<br>" + user_info.text
user_info = user_info.json()
hns_balance = user_info['hns_balance']
hns_balance = int(hns_balance)/1000000
return hns_balance
2023-11-08 14:31:52 +11:00
def discord(domain, email,ip,referer):
url = os.getenv('discord_webhook')
if url == None:
return "No webhook set"
payload = {
"content": "New gift request: " + domain + "\nSent to " + email + "\nIP: " + ip + "\nReferer: " + referer
}
response = requests.post(url, data=json.dumps(payload), headers={'Content-Type': 'application/json'})
# Check if the message was sent successfully
if response.status_code == 204:
print("Message sent successfully!")
else:
print(f"Failed to send message. Status code: {response.status_code}")
print(response.text)