-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopensea.py
165 lines (123 loc) · 4.68 KB
/
opensea.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.remote.command import Command
from webdriver_manager.chrome import ChromeDriverManager
import json
import time
import os
import copy
path = './nfts/'
path_images = path + 'images/'
path_meta = path + 'metadata/'
image_extension = '.jpg'
json_extension = '.json'
TIMEOUT = 30
PING = 5
BASE_METADATA_JSON = {
"name": "",
"description": "",
"attributes": [],
}
opensea = 'https://testnets.opensea.io'
options = Options()
options.add_argument("--disable-infobars")
options.add_argument("--enable-file-cookies")
options.add_argument("user-data-dir=selenium")
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_extension('./metamask-ext/extension_10_22_2_0.crx')
browser = None
def wait_any_element_to_have_text(xpath, text, wait_time=TIMEOUT):
secs_passed = 0
while secs_passed < wait_time:
elements = browser.find_elements(By.XPATH, xpath)
for element in elements:
if element.text == text:
return element
time.sleep(PING)
secs_passed += PING
def center_and_click(element):
script = """
var viewPortHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
var elementTop = arguments[0].getBoundingClientRect().top;
window.scrollBy(0, elementTop-(viewPortHeight / 2));
"""
browser.execute_script(script, element)
element.click()
return element
def write_text(element, text):
script = """
var element = arguments[0], txt = arguments[1];
element.value = txt;
element.innerHTML = txt;
element.dispatchEvent(new Event('change'));
"""
text = text.replace("\\n", "\n")
browser.execute_script(script, element, text)
element.send_keys(" " + Keys.BACKSPACE)
def read_token_info(token):
return json.load(open(path_meta + str(token) + json_extension, "r").read())
def mint_token(image_path, metadata):
# Add media
media = browser.find_element(By.ID, "media")
browser.execute_script("arguments[0].style.display = 'block';", media)
media.send_keys(image_path)
# Parse metadata
token_name = metadata['name']
token_description = metadata['description']
token_properties = metadata['properties']
# Add name
name = browser.find_element(By.ID, "name")
write_text(name, token_name)
# Add description
description = browser.find_element(By.ID, "description")
write_text(description, token_description)
# Create
create = browser.find_element(By.XPATH, "//button[text()='Create']")
center_and_click(create)
time.sleep(PING)
# And wait for confirmation
minted = wait_any_element_to_have_text(
"//h4", 'You created ' + token_name + '!')
return (minted is not None)
def generate_image_metadata_file(name, description, attributes=[]):
if not os.path.exists(path_meta):
os.makedirs(path_meta)
item_json = copy.copy(BASE_METADATA_JSON)
item_json['name'] = name
item_json['description'] = description
item_json_path = os.path.join(
path_meta, item_json['name'] + json_extension)
with open(item_json_path, 'w') as f:
json.dump(item_json, f)
return item_json
def generate_image_metadata_dict(name, description, attributes=[]):
return locals()
def upload_image(collection, image_path, metadata):
global browser
try:
browser.get(opensea + '/collection/' + collection + '/assets/create')
except Exception as ex:
# browser is None or is_driver_dead(browser) or
# not driver.service.assert_process_still_running() or
# user closed initial tabs
#
if browser:
browser.quit()
browser = webdriver.Chrome(service=Service(
ChromeDriverManager().install()), options=options)
browser.get(opensea + '/collection/' + collection + '/assets/create')
print("At this point you need to open a new tab and log into MetaMask to import your wallet."
"Once you do that, go back to the OpenSea tab and connect MetaMask wallet to your account.")
input("Press any key after connecting the wallet...")
time.sleep(PING)
mint_token(image_path, metadata)
if __name__ == '__main__':
collection = input("Enter collection name: ")
# Read token's metadata
token = '2'
token_info = read_token_info(token)
image_path = os.path.abspath(path_images + token + image_extension)
upload_image(collection, image_path, token_info)