-
-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8a98ba8
commit 20c5f7c
Showing
3 changed files
with
96 additions
and
82 deletions.
There are no files selected for viewing
110 changes: 61 additions & 49 deletions
110
src/gateio_new_coins_announcements_bot/announcement_scrapers/binance_scraper.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,61 @@ | ||
import random | ||
import time | ||
|
||
import requests | ||
|
||
from gateio_new_coins_announcements_bot.logger import logger | ||
from gateio_new_coins_announcements_bot.util.random import random_int | ||
from gateio_new_coins_announcements_bot.util.random import random_str | ||
|
||
|
||
class BinanceScraper: | ||
def __init__(self, http_client=requests): | ||
self.http_client = http_client | ||
|
||
def fetch_latest_announcement(self): | ||
""" | ||
Retrieves new coin listing announcements from binance.com | ||
""" | ||
logger.debug("Pulling announcement page") | ||
request_url = self.__request_url() | ||
response = self.http_client.get(request_url) | ||
|
||
# Raise an HTTPError if status is not 200 | ||
response.raise_for_status() | ||
|
||
if "X-Cache" in response.headers: | ||
logger.debug(f'Response was cached. Contains headers X-Cache: {response.headers["X-Cache"]}') | ||
else: | ||
logger.debug("Hit the source directly (no cache)") | ||
|
||
latest_announcement = response.json() | ||
logger.debug("Finished pulling announcement page") | ||
return latest_announcement["data"]["catalogs"][0]["articles"][0]["title"] | ||
|
||
def __request_url(self): | ||
# Generate random query/params to help prevent caching | ||
queries = [ | ||
"type=1", | ||
"catalogId=48", | ||
"pageNo=1", | ||
f"pageSize={str(random_int(maxInt=200))}", | ||
f"rnd={str(time.time())}", | ||
f"{random_str()}={str(random_int())}", | ||
] | ||
random.shuffle(queries) | ||
return ( | ||
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]}" | ||
) | ||
import random | ||
import time | ||
|
||
import requests | ||
|
||
from gateio_new_coins_announcements_bot.logger import logger | ||
from gateio_new_coins_announcements_bot.rotating_proxy import get_proxy | ||
from gateio_new_coins_announcements_bot.rotating_proxy import is_ready as rotating_proxy_is_ready | ||
from gateio_new_coins_announcements_bot.util.random import random_int | ||
from gateio_new_coins_announcements_bot.util.random import random_str | ||
|
||
|
||
class BinanceScraper: | ||
def __init__(self, http_client=requests): | ||
self.http_client = http_client | ||
|
||
def fetch_latest_announcement(self): | ||
""" | ||
Retrieves new coin listing announcements from binance.com | ||
""" | ||
logger.debug("Pulling announcement page") | ||
request_url = self.__request_url() | ||
|
||
if rotating_proxy_is_ready(): | ||
proxy = get_proxy() | ||
logger.debug(f"Using proxy: {proxy}") | ||
try: | ||
response = self.http_client.get(request_url, proxies={"http": "socks5://" + proxy}) | ||
|
||
except Exception as e: | ||
logger.error(e) | ||
else: | ||
response = self.http_client.get(request_url) | ||
|
||
# Raise an HTTPError if status is not 200 | ||
response.raise_for_status() | ||
|
||
if "X-Cache" in response.headers: | ||
logger.debug(f'Response was cached. Contains headers X-Cache: {response.headers["X-Cache"]}') | ||
else: | ||
logger.debug("Hit the source directly (no cache)") | ||
|
||
latest_announcement = response.json() | ||
logger.debug("Finished pulling announcement page") | ||
return latest_announcement["data"]["catalogs"][0]["articles"][0]["title"] | ||
|
||
def __request_url(self): | ||
# Generate random query/params to help prevent caching | ||
queries = [ | ||
"type=1", | ||
"catalogId=48", | ||
"pageNo=1", | ||
f"pageSize={str(random_int(maxInt=200))}", | ||
f"rnd={str(time.time())}", | ||
f"{random_str()}={str(random_int())}", | ||
] | ||
random.shuffle(queries) | ||
return ( | ||
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]}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters