Skip to content

Commit

Permalink
Merge pull request #12 from offish/v1.2.9
Browse files Browse the repository at this point in the history
v1.2.9
  • Loading branch information
offish authored Jul 28, 2021
2 parents c19eb37 + b21b8be commit dac2563
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 31 deletions.
2 changes: 1 addition & 1 deletion express/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__title__ = "tf2-express"
__author__ = "offish"
__license__ = "MIT"
__version__ = "1.2.8"
__version__ = "1.2.9"
7 changes: 4 additions & 3 deletions express/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
bots = [
BOTS = [
{
"name": "bot1",
"username": "username",
Expand All @@ -23,5 +23,6 @@
},
]

owners = ["76511111111111111"]
timeout = 30
OWNERS = ["76511111111111111"]
TIMEOUT = 30
DEBUG = True
4 changes: 4 additions & 0 deletions express/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def get_items() -> list:
return [item["name"] for item in prices.find()]


def get_autopriced_items() -> list:
return [item["name"] for item in prices.find() if item.get("autoprice")]


def get_item(name: str) -> dict:
return prices.find_one({"name": name})

Expand Down
1 change: 1 addition & 0 deletions express/files/logs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

39 changes: 34 additions & 5 deletions express/logging.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,41 @@
from datetime import datetime
from datetime import date, datetime

from .config import DEBUG

from colorama import Fore as f
from colorama import init

init()

logs = []


def write_to_txt_file(strings: list):
to_write = ""

for string in strings:
to_write += "\n" + string

with open("express/files/logs.txt", "a") as f:
f.write(to_write)


def log(color: int, sort: str, bot: str, text: str, offer_id: str = ""):
name = f" {f.GREEN}[{bot}]" if bot else ""
name = f" {f.WHITE}[{f.GREEN}{bot}{f.WHITE}]" if bot else ""
text = f"({f.YELLOW}{offer_id}{f.WHITE}) {text}" if offer_id else text
time = datetime.now().time().strftime("%H:%M:%S")
print(
f"{f.GREEN}tf2-express | {f.WHITE}{time} - {color + sort}{name}{f.WHITE}: {text}{f.WHITE}"
)
string = "{}tf2-express | {}" + time + " - {}" + sort + "{}{}: " + text + "{}"

if DEBUG:
logs.append(
f"{date.today().strftime('%d/%m/%Y')} @ {time} | {sort} {bot}: {text}"
)

if len(logs) >= 10:
write_to_txt_file(logs)
logs.clear()

print(string.format(f.GREEN, f.WHITE, color, name, f.WHITE, f.WHITE))


class Log:
Expand All @@ -28,3 +51,9 @@ def error(self, text: str):

def trade(self, text: str, offer_id: str = ""):
log(f.MAGENTA, "trade", self.bot, text, self.offer_id or offer_id)

def debug(self, text: str, offer_id: str = ""):
log(f.CYAN, "debug", self.bot, text, self.offer_id or offer_id)

def close(self):
write_to_txt_file(logs)
4 changes: 2 additions & 2 deletions express/offer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .settings import decline_trade_hold
from .prices import get_price, get_key_price
from .config import owners
from .config import OWNERS
from .utils import Item, to_scrap

from steampy.models import TradeOfferState
Expand Down Expand Up @@ -95,4 +95,4 @@ def get_partner(self) -> str:
return account_id_to_steam_id(self.offer["accountid_other"])

def is_from_owner(self) -> bool:
return self.get_partner() in owners
return self.get_partner() in OWNERS
1 change: 1 addition & 0 deletions express/settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
accept_donations = True
decline_bad_trade = False
decline_trade_hold = True
decline_scam_offers = True
allow_craft_hats = True
Expand Down
4 changes: 4 additions & 0 deletions gui.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@echo off
title tf2-express-gui
python -m express.ui.panel
pause
59 changes: 39 additions & 20 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from express.settings import *
from express.logging import Log, f
from express.prices import update_pricelist
from express.config import bots, timeout
from express.config import *
from express.client import Client
from express.offer import Offer, valuate
from express.utils import to_refined, refinedify
Expand All @@ -23,10 +23,11 @@ def run(bot: dict) -> None:
processed = []
values = {}

log.info(f"Polling offers every {timeout} seconds")
log.info(f"Polling offers every {TIMEOUT} seconds")

while True:

log = Log(bot["name"])
log.debug("Polling offers...")
offers = client.get_offers()

for offer in offers:
Expand Down Expand Up @@ -86,7 +87,12 @@ def run(bot: dict) -> None:
client.accept(offer_id)

else:
client.decline(offer_id)
if decline_bad_trade:
client.decline(offer_id)
else:
log.trade(
"Ignoring offer as automatic decline is disabled"
)

else:
log.trade("Offer is invalid")
Expand Down Expand Up @@ -124,27 +130,33 @@ def run(bot: dict) -> None:

processed.remove(offer_id)

sleep(timeout)
sleep(TIMEOUT)

except BaseException as e:
log.info(f"Caught {type(e).__name__}")

try:
client.logout()
except:
pass

except KeyboardInterrupt:
client.logout()
log.info(f"Stopping")
log.info(f"Stopped")


def database() -> None:
try:
items = get_items()
items_in_database = get_items()
log = Log()

while True:
if not items == get_items():
if not items_in_database == get_items():
log.info("Item(s) were added or removed from the database")
items = get_items()
update_pricelist(items)
items_in_database = get_items()
update_pricelist(items_in_database)
log.info("Successfully updated all prices")
sleep(10)

except KeyboardInterrupt:
except BaseException:
pass


Expand All @@ -159,6 +171,8 @@ def database() -> None:
update_pricelist(items)
log.info("Successfully updated all prices")

del items

@socket.event
def connect():
socket.emit("authentication")
Expand All @@ -171,7 +185,7 @@ def authenticated(data):
@socket.event
def price(data):
if data["name"] in get_items():
update_price(data["name"], data["buy"], data["sell"])
update_price(data["name"], True, data["buy"], data["sell"])

@socket.event
def unauthorized(sid):
Expand All @@ -180,15 +194,20 @@ def unauthorized(sid):
socket.connect("https://api.prices.tf")
log.info("Listening to Prices.TF for price updates")

Process(target=database).start()
process = Process(target=database)
process.start()

with Pool(len(bots)) as p:
p.map(run, bots)
with Pool(len(BOTS)) as p:
p.map(run, BOTS)

except KeyboardInterrupt as e:
log.info(f"Caught {type(e).__name__}")
except BaseException as e:
if e:
log.error(e)

finally:
socket.disconnect()
process.terminate()
t2 = time()
log.info(f"Done. Bot ran for {round(t2-t1, 1)} seconds")
exit()
log.close()
quit()
4 changes: 4 additions & 0 deletions start.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@echo off
title tf2-express
python main.py
pause

0 comments on commit dac2563

Please sign in to comment.