import requests
from lxml import html
from time import sleep
import hashlib
from random import randint as randomInteger
from tkinter import *
import threading
userItemValues = []
def login(username, password):
global loginUsername, loginPassword, requestsSession, userID
loginUsername = username
loginPassword = password
loginUrl = "https://forum.toribash.com/tori_market.php"
requestsSession = requests.session()
loginData = dict(username=loginUsername,
password="",
securitytoken="guest",
vb_login_md5password=hashlib.md5(loginPassword.encode('utf-8')).hexdigest(),
vb_login_md5password_utf=hashlib.md5(loginPassword.encode('utf-8')).hexdigest(),
submit="Login")
requestsSession.post(loginUrl, data=loginData)
print("Logged in as {}".format(loginUsername))
response = requestsSession.get("https://forum.toribash.com/tori_stats.php?format=json")
userStats = response.json()
userID = userStats["userid"]
print("ID:", userID)
if userID == "0":
print("Login failed")
exit()
"https://forum.toribash.com/tori_stats.php?format=json"
window.destroy()
main_window()
def start_thread():
t = threading.Thread(target=sell_from_set)
t.start()
def sell_from_set_setup():
global inventoryItems, sets, listbox, progressListbox
mainWindow.destroy()
listWindow = Tk()
# set up canvas
canvas = Canvas(listWindow, width=500, height=500)
canvas.configure(background="#e6e6e6")
#
Label(listWindow,
fg="black",
font=("Segoe UI", "14", "bold"),
text="Select a set, then click 'OK'").pack()
# warning
Label(listWindow,
fg="red",
font=("Segoe UI", "14", "bold"),
text="Warning: all items will be removed from the set if you stop the program early!").pack()
# list of sets
listbox = Listbox(listWindow, width=75)
listbox.pack(side="left", expand=1, fill="both")
# progress list
progressListbox = Listbox(listWindow, width=75)
progressListbox.pack(side="right", expand=1, fill="both")
# "go" button
sellButton = Button(listWindow,
text="OK",
width="30",
command=(lambda: start_thread()))
canvas.create_window(250, 250, window=sellButton)
canvas.pack()
response = requestsSession.get("http://forum.toribash.com/tori_api.php?action=user_inventory&mode=1&username=" + loginUsername)
inventoryItems = response.json()
c = 1
sets = {}
# add list of sets to listbox
for itemName in inventoryItems["inventory"]:
if itemName["itemname"] == "Set" and itemName["setname"] != "":
sets[c] = itemName["inventid"]
listbox.insert(END, "Set: {} ({})".format(itemName["setname"], itemName["inventid"]))
listbox.pack()
listWindow.mainloop()
def sell_from_set():
# figure out which set the user picked; resolve it to set ID
selectedSetID = listbox.get(listbox.curselection())
selectedSetID = selectedSetID[selectedSetID.index("(")+1:-1]
itemsToSell = []
# save items from set
for itemName in inventoryItems["inventory"]:
if itemName["setid"] == selectedSetID:
itemsToSell.append(itemName["inventid"])
itemsToSell.append(itemName["itemname"])
i = 0
# data to be sent with post request
removeData = {
"sid": "0",
"toUsername": "",
"message": "",
"inventory_length": "100",
"check[]": []
}
postUrl = "https://forum.toribash.com/tori_inventory.php?action=move&viewsid={}&userid={}&sortid=0".format(selectedSetID, userID)
# add all items to post request for optimum zoom
while i < len(itemsToSell):
sellItem = itemsToSell[i]
removeData["check[]"].append(sellItem)
i += 2
# send that shit
requestsSession.post(postUrl, data=removeData)
progressListbox.insert(END, "Removed all items from Set {}".format(selectedSetID))
progressListbox.update_idletasks()
totalValue = 0
itemsWithNoPrice = 0
w = 1
while w < len(itemsToSell):
item = itemsToSell[w] # references item ID
nextItem = itemsToSell[w - 1] # item name
sleepTime = randomInteger(3, 6) # DO NOT REMOVE THIS OR YOU WILL BE AUTOMATICALLY BANNED FROM TORIBASH
sleep(sleepTime) # DO NOT REMOVE THIS OR YOU WILL BE AUTOMATICALLY BANNED FROM TORIBASH
# get content of market search
tree = html.fromstring(str(requestsSession.get(str(
"https://forum.toribash.com/tori_market.php?action=search&item=" + str(
item.replace(" ", "+")) + "&username=&max=0&maxQi=10000")).content))
foundPrice = False
# get price
for i in tree.xpath("//*[contains(@title, 'Torishop price')]"):
try:
if item != "Set":
userItemValues.extend((item, int(nextItem), int(i.text_content().split(" ")[0]) - 1))
progressListbox.insert(END, "Sell {} ({}) for {} TC".format(item, nextItem, int(i.text_content().split(" ")[0]) - 1))
progressListbox.update_idletasks()
sleep(sleepTime) # DO NOT REMOVE THIS OR YOU WILL BE AUTOMATICALLY BANNED FROM TORIBASH
totalValue += int(i.text_content().split(" ")[0]) - 1
foundPrice = True
break # don't need to look at more than 1 price per item; break loop
except:
continue # i have no idea why this is here but it works
if not foundPrice:
progressListbox.insert(END, "No price for {} ({})".format(item, nextItem))
progressListbox.update_idletasks()
itemsWithNoPrice += 1
w += 2
progressListbox.insert(END, "Attempting to return items to set")
progressListbox.update_idletasks()
o = 0
# data to be sent with post request
addData = {
"sid": selectedSetID,
"toUsername": "",
"message": "",
"inventory_length": "100",
"check[]": []
}
postUrl = "https://forum.toribash.com/tori_inventory.php?action=move&viewsid={}&userid={}&sortid=0".format(
selectedSetID, userID)
# add all items to post request for optimum zoom
while o < len(itemsToSell):
sellItem = itemsToSell[o]
addData["check[]"].append(sellItem)
o += 2
# send that shit
requestsSession.post(postUrl, data=addData)
progressListbox.insert(END, "\nTotal value: {} TC, and {} items that could not be sold".format(totalValue, itemsWithNoPrice))
progressListbox.update_idletasks()
# set up the tkinter window that prompts user for username and password
def login_window():
global window
window = Tk()
# set up canvas
canvas = Canvas(window, width=225, height=95)
canvas.configure(background="#e6e6e6")
canvas.pack()
# input label -- username
canvas.create_text(50, 15, fill="black", font=("Segoe UI", "14", "bold"), text="Username")
# input box -- username
usernameInput = Entry(window)
canvas.create_window(160, 17, window=usernameInput)
# input label -- password
canvas.create_text(48, 45, fill="black", font=("Segoe UI", "14", "bold"), text="Password")
# input box -- password
passwordInput = Entry(window, show="•")
canvas.create_window(160, 47, window=passwordInput)
# login button
loginButton = Button(window, text="Login", width="30", command=(lambda: login(usernameInput.get(), passwordInput.get())))
window.bind("<Return>", (lambda x: login(usernameInput.get(), passwordInput.get())))
canvas.create_window(113, 80, window=loginButton)
window.mainloop()
# set up main menu window
def main_window():
global mainWindow
mainWindow = Tk()
# set up canvas
canvas = Canvas(mainWindow, width=500, height=500)
canvas.configure(background="#e6e6e6")
canvas.pack()
# button -- option 1
sellFromSetButton = Button(mainWindow, text="Get value of a set", width="25", height="10", command=(lambda: sell_from_set_setup()))
canvas.create_window(100, 70, window=sellFromSetButton)
mainWindow.mainloop()
login_window()