Skip to content

Commit

Permalink
Improved code testability and modularity
Browse files Browse the repository at this point in the history
  • Loading branch information
andreademasi committed Jan 15, 2022
1 parent 2d0a200 commit 1af5fe1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
8 changes: 5 additions & 3 deletions src/gateio_new_coins_announcements_bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
from gateio_new_coins_announcements_bot.store_order import store_order
from gateio_new_coins_announcements_bot.trade_client import get_last_price
from gateio_new_coins_announcements_bot.trade_client import place_order
import rotating_proxy
from gateio_new_coins_announcements_bot.rotating_proxy import init_proxy as init_rotating_proxy
from gateio_new_coins_announcements_bot.rotating_proxy import set_proxy_event


# To add a coin to ignore, add it to the json array in old_coins.json
globals.old_coins = load_old_coins()
Expand All @@ -43,7 +45,7 @@
session = {}

# Init proxy fetching
rotating_proxy.init_proxy()
init_rotating_proxy()

# Keep the supported currencies loaded in RAM so no time is wasted fetching
# currencies.json from disk when an announcement is made
Expand Down Expand Up @@ -501,7 +503,7 @@ def main():
search_and_update()
except KeyboardInterrupt:
logger.info("Stopping Threads")
rotating_proxy.event.set()
set_proxy_event()
globals.stop_threads = True
globals.buy_ready.set()
globals.sell_ready.set()
Expand Down
27 changes: 13 additions & 14 deletions src/gateio_new_coins_announcements_bot/new_listings_scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
from gateio_new_coins_announcements_bot.load_config import load_config
from gateio_new_coins_announcements_bot.logger import logger
from gateio_new_coins_announcements_bot.store_order import load_order
import rotating_proxy
from gateio_new_coins_announcements_bot.rotating_proxy import is_ready as rotating_proxy_is_ready
from gateio_new_coins_announcements_bot.rotating_proxy import get_proxy

config = load_config("config.yml")
client = load_gateio_creds("auth/auth.yml")
Expand Down Expand Up @@ -51,21 +52,19 @@ def get_announcement():
f"https://www.binance.com/gateway-api/v1/public/cms/article/list/query"
f"?{queries[0]}&{queries[1]}&{queries[2]}&{queries[3]}&{queries[4]}&{queries[5]}"
)
if rotating_proxy.is_ready():
proxy = rotating_proxy.get_proxy()
print(f"Using proxy: {proxy}")
try:

latest_announcement = requests.get(request_url, proxies={"http": "socks5://" + proxy})
except Exception as e:
logger.error(e)
if rotating_proxy_is_ready():
proxy = get_proxy()
logger.info(f"Using proxy: {proxy}")
latest_announcement = requests.get(request_url, proxies={"http": "socks5://" + proxy})
else:
latest_announcement = requests.get(request_url)
try:
logger.debug(f'X-Cache: {latest_announcement.headers["X-Cache"]}')
except KeyError:
# No X-Cache header was found - great news, we're hitting the source.
pass

if latest_announcement.status_code == 200:
try:
logger.debug(f'X-Cache: {latest_announcement.headers["X-Cache"]}')
except KeyError:
# No X-Cache header was found - great news, we're hitting the source.
pass

latest_announcement = latest_announcement.json()
logger.debug("Finished pulling announcement page")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@

_proxy_list = {}
_proxy = None
event = threading.Event()
_event = threading.Event()


def init_proxy():
threading.Thread(target=lambda: _every(60 * 10, _fetch_proxies)).start()
# Required for populating the proxy list when starting bot
_fetch_proxies()


def _fetch_proxies():
Expand All @@ -31,7 +33,6 @@ def _fetch_proxies():
).text
except requests.exceptions.RequestException as e:
logger.error(e)
print(proxy_res)

for p in proxy_res.split("\n"):
_proxy_list[p] = p
Expand Down Expand Up @@ -60,12 +61,16 @@ def is_ready() -> bool:
return len(_proxy_list) > 0


def set_proxy_event():
_event.set()


# can be generalized and moved to separate file
def _every(delay: int, task: Callable):
global event
next_time = time.time() + delay
while not globals.stop_threads:
event.wait(max(0, next_time - time.time()))
_event.wait(max(0, next_time - time.time()))
try:
task()
except Exception:
Expand Down Expand Up @@ -100,7 +105,3 @@ def checker(proxy):
pass
print("%s does not respond.\n" % proxy)
return


# Required for populating the proxy list when starting bot
_fetch_proxies()

0 comments on commit 1af5fe1

Please sign in to comment.