-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
121 lines (94 loc) · 3.73 KB
/
bot.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from flask import Flask, jsonify
import tweepy
import requests
from datetime import datetime, timedelta
from pytz import UTC
from dotenv import load_dotenv
from time import sleep
from urllib.parse import quote
import os
app = Flask(__name__)
# Load environment variables
load_dotenv()
bearer_token = os.getenv("BEARER_TOKEN")
consumer_key = os.getenv("CONSUMER_KEY")
consumer_secret = os.getenv("CONSUMER_SECRET")
access_token = os.getenv("ACCESS_TOKEN_KEY")
access_token_secret = os.getenv("ACCESS_TOKEN_SECRET")
opensea_api_key = os.getenv("X_API_KEY")
# Authenticate with Twitter using tweepy.Client
client = tweepy.Client(
bearer_token=bearer_token,
consumer_key=consumer_key,
consumer_secret=consumer_secret,
access_token=access_token,
access_token_secret=access_token_secret,
wait_on_rate_limit=True
)
posted_tweets = set()
# Function to check if a tweet is a duplicate
def is_duplicate_tweet(tweet_text):
return tweet_text in posted_tweets
# Function to format and send tweet
def format_and_send_tweet(event):
asset = event.get('nft', {})
asset_name = asset.get('name', 'Unknown Asset')
opensea_link = asset.get('opensea_url', '')
total_price = event.get('payment', {}).get('quantity', 0)
# Convert total_price to float if it's a string
if isinstance(total_price, str):
total_price = float(total_price)
total_price_in_eth = total_price / 1e18 # Convert total price to Ether
asset_symbol = event.get('payment', {}).get('symbol', 'Matic')
# Use current time if 'created_date' is not available in the API response
created = datetime.now(UTC)
# Use quote from urllib.parse
opensea_link = quote(opensea_link)
tweet_text = f"{asset_name} bought for ({total_price_in_eth} {asset_symbol}) {opensea_link}"
if not is_duplicate_tweet(tweet_text):
try:
# Post tweet using tweepy.Client
tweet = client.create_tweet(text=tweet_text)
print("Tweet created successfully:", tweet_text)
# Add the tweet content to the set of posted tweets
posted_tweets.add(tweet_text)
except tweepy.TweepError as te:
print(f"Error creating tweet: {te}")
else:
print("Duplicate content. Skipping tweet.")
# Poll OpenSea every 60 seconds & retrieve all sales for a given collection
# in either the time since the last sale OR in the last minute
def poll_opensea():
last_sale_time = datetime.now(UTC) - timedelta(seconds=59)
max_retries = 5 # Maximum number of retries
specific_unix_timestamp = 1704945107
while max_retries > 0:
try:
response = requests.get(
f"https://api.opensea.io/api/v2/events/collection/cool-bullz-2?after={int(last_sale_time.timestamp())}&event_type=sale",
headers={"X-API-KEY": opensea_api_key},
)
response.raise_for_status()
events = response.json().get('asset_events', [])
sorted_events = sorted(events, key=lambda x: x.get('created_date', ''))
print(f"{len(events)} sales since the last one...")
for event in sorted_events:
format_and_send_tweet(event)
except requests.RequestException as re:
print(f"Error in OpenSea API request: {re}")
max_retries -= 1
sleep(10)
continue
# Sleep for 60 seconds before the next iteration
sleep(60)
print("Max retries reached. Exiting.")
# Define Flask routes
@app.route("/")
def home():
return "Flask app is running!"
@app.route("/start_polling")
def start_polling():
poll_opensea()
return "Polling started!"
if __name__ == "__main__":
app.run(debug=True, port=int(os.environ.get("PORT", 5000)))