Skip to content

Instantly share code, notes, and snippets.

@t0brig01
Last active December 18, 2021 02:04
Show Gist options
  • Save t0brig01/ccb3fca5627ff39d0f7acea7ee1a9bb8 to your computer and use it in GitHub Desktop.
Save t0brig01/ccb3fca5627ff39d0f7acea7ee1a9bb8 to your computer and use it in GitHub Desktop.
import threading
from types import MethodWrapperType
from discord.embeds import Embed
import requests
import discord
import json
import sys
import time
import urllib.parse
from datetime import date, datetime, timedelta
import asyncio
from requests.api import request
TOKEN = "<DISCORD BOT TOKEN HERE>"
client = discord.Client()
def callTwitterAPI(mint, name, image, sold, tx):
headers = {
"nft_mint_address": mint,
"nft_name": name,
"nft_image_url": image,
"nft_sold_price": sold,
"tx_address": tx,
}
try:
resp = requests.post(url="<TWITTER API URL HERE>", headers=headers)
resp.raise_for_status()
except requests.exceptions.HTTPError as err:
raise (err)
async def salesNotification(channel):
meUrl = "https://api-mainnet.magiceden.io/rpc/getGlobalActivitiesByQuery?q=%7B%22%24match%22%3A%7B%22collection_symbol%22%3A%22enigma_expanses%22%7D%2C%22%24sort%22%3A%7B%22blockTime%22%3A-1%7D%2C%22%24skip%22%3A0%7D"
resetTime = datetime.now()
alreadyDone = []
while True:
try:
meData = requests.get(meUrl).json()
for x in meData["results"]:
timeOfSale = datetime.strptime(x["createdAt"], "%Y-%m-%dT%H:%M:%S.%fZ")
q, _ = divmod(((datetime.now() - timeOfSale).total_seconds()), 60)
if 0 < q < 2880: # within the last 3 min
if x["txType"] == "exchange" or x["txType"] == "acceptBid":
if x["mint"] not in alreadyDone:
alreadyDone.append(x["mint"])
details = requests.get(
"https://api-mainnet.magiceden.io/rpc/getNFTByMintAddress/"
+ x["mint"]
).json()["results"]
price = (
float(x["parsedTransaction"]["total_amount"]) / 100000000
)
pageUrl = "https://magiceden.io/item-details/{0}".format(
x["mint"]
)
transactionUrl = "https://solscan.io/tx/{0}".format(
x["transaction_id"]
)
embed = discord.Embed(
title="{0} Sold!".format(details["title"]),
url=pageUrl,
colour=discord.Colour.blue(),
)
embed.add_field(
name="**Sold Price:**",
value="{:.2f} SOL".format(price),
inline=False,
)
embed.add_field(
name="**MagicEden:**",
value="[{0}]({1})".format(pageUrl, pageUrl),
inline=False,
)
embed.add_field(
name="**TX:**",
value="[{0}]({1})".format(transactionUrl, transactionUrl),
inline=False,
)
embed.set_image(url=details["img"])
embed.set_footer(
text="MagicEden Sales Monitor • {0}".format(
date.today().strftime("%m/%d/%y")
)
)
callTwitterAPI(
x["mint"],
details["title"],
details["img"],
price,
x["transaction_id"],
)
await channel.send(embed=embed)
q, _ = divmod(((datetime.now() - resetTime).total_seconds()), 60)
if q == 2880:
alreadyDone = []
resetTime = datetime.now()
except Exception as ex:
print(ex)
continue
return
@client.event
async def on_message(message):
if message.author == client.user:
return
messageContents = message.content.lower()
messageSplit = messageContents.split(" ")
try:
if messageSplit[0] == "!sales":
if messageSplit[1] == "init":
await salesNotification(message.channel)
except IndexError:
pass
except:
print("Unexpected error:", sys.exc_info()[0])
raise
client.run(TOKEN)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment