Created
February 27, 2021 23:48
-
-
Save apple1417/f11652012b2fc90a2937de95db71f2de to your computer and use it in GitHub Desktop.
Scrapes the BL3 hunt webpage and writes your points/rank to a file, which obs can use as a text source so that you don't have to update it manually.
This file contains hidden or 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
import traceback | |
from datetime import timedelta | |
from time import sleep | |
import requests | |
from bs4 import BeautifulSoup | |
""" | |
==================================================================================================== | |
Only edit these constants between the two lines | |
""" | |
# The url of your hunter profile page | |
HUNTER_URL = "https://bl3hunt.com/hunter/<YOUR URL>/" | |
# The file to write to - use this as the input to a text source in obs | |
FILE_PATH = "hunt_info.txt" | |
# What to write to the file. | |
# `{points}` and `{rank}` will be replaced with your points and rank respectively | |
# You can also add standard escape sequences such as `\n` to write a new line | |
FORMAT_STR = "{points}/1145 Points (#{rank})" | |
""" | |
==================================================================================================== | |
""" | |
UPDATE_INTERVAL = timedelta(minutes=1) | |
def scrape_page(header_name: str, soup: BeautifulSoup) -> str: | |
for header in soup("h3"): | |
contents = header.contents | |
if len(contents) < 2: | |
continue | |
string = contents[1].string | |
if header_name.lower() in contents[0].lower() and string is not None: | |
return str(string) | |
return "Unknown" | |
def get_points(soup: BeautifulSoup) -> str: | |
return scrape_page("Points", soup) | |
def get_rank(soup: BeautifulSoup) -> str: | |
return scrape_page("Rank", soup) | |
def update_file() -> None: | |
soup = BeautifulSoup( | |
requests.get(HUNTER_URL).text, | |
features="html.parser" | |
) | |
rank = get_rank(soup) | |
points = get_points(soup) | |
with open(FILE_PATH, "w") as file: | |
file.write(FORMAT_STR.format(rank=rank, points=points)) | |
while True: | |
try: | |
update_file() | |
except Exception: | |
traceback.print_exc() | |
sleep(UPDATE_INTERVAL.total_seconds()) |
This file contains hidden or 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
requests==2.25.1 | |
beautifulsoup4==4.9.3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment