From c903441e41d113cce4196a5bd8d938287c5a1d5b Mon Sep 17 00:00:00 2001 From: Andrew Perez-Ledo Date: Fri, 21 Jun 2024 21:09:52 -0400 Subject: [PATCH 1/3] Finished Adding Twitch --- src/scrape_up/twitch/twitch.py | 59 ++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/scrape_up/twitch/twitch.py diff --git a/src/scrape_up/twitch/twitch.py b/src/scrape_up/twitch/twitch.py new file mode 100644 index 00000000..6619df55 --- /dev/null +++ b/src/scrape_up/twitch/twitch.py @@ -0,0 +1,59 @@ +from bs4 import BeautifulSoup + +import requests + +''' + WARNING!!! + Smaller twitch channels with low stream time results is generic return value: + "Twitch is the world's leading video platform and community for gamers." + + Steps for use: + + 1. Create "Twitch_Scraper()" class instance + 2. call the instance's function "scrape_title_description()" providing the channel name as a string + 3. Channel stream title is returned (if live), Channel Description is returned (if offline) + + Example: using KaiCenat's twitch channel + + scraper = Twitch_Scraper() + title = scraper.scrape_title_description("kaicenat") + print(title) + + ''' +class Twitch_Scraper(): + + def __init__(self): + self.status = None + + # this function just gets the text within the quotes for the descrption or title + def get_in_quotes(self, input_string): + if input_string is None: + return None + start_index = 15 + end_index = input_string.find('"', start_index) + try: + result = input_string[start_index:end_index] + return result + + except: + return "Error: No suitable title/description" + + # This function returns either the title of the stream if the channel is live, or the channel description if it's offline + def scrape_title_description(self, channel: str): + url = f"https://www.twitch.tv/{channel}" + try: + response = requests.get(url) + if response.status_code != 200: + print(f"Failed to retrieve the page. This channel might not exist. Status code: {response.status_code}") + return None + # make soup to parse html + soup = BeautifulSoup(response.content, 'html.parser') + # find the property contain the title or description + meta_tag_2 = soup.find('meta', {'property': "og:description"}) + # return just the string text itself + return self.get_in_quotes(str(meta_tag_2)) + + except: + print("Failed to scrape.") + return None + From d2e0513c764ba2502ede6b275c6ccd8e7c4384bc Mon Sep 17 00:00:00 2001 From: Andrew Perez-Ledo Date: Fri, 21 Jun 2024 21:51:03 -0400 Subject: [PATCH 2/3] Added twitch.md --- docs/modules/twitch.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 docs/modules/twitch.md diff --git a/docs/modules/twitch.md b/docs/modules/twitch.md new file mode 100644 index 00000000..c34395e6 --- /dev/null +++ b/docs/modules/twitch.md @@ -0,0 +1,32 @@ +```python +from scrape_up import twitch +``` + +### Scrape + +First, create an object of class `Twitch_Scraper` + +```python +twitch_scraper = Twitch_Scraper() +``` + +| Methods | Details | +| -------------------------- | --------------------------------------------- | +| `.scrape_title_description(channel)` | Returns: Stream Title (if Live) or Channel Description (if Offline). | + + +--- + + Example: using KaiCenat's twitch channel +```python + scraper = Twitch_Scraper() + title = scraper.scrape_title_description("kaicenat") + print(title) +``` + +Output (if Live): ⚔️100+ HOUR STREAM⚔️ELDEN RING DLC MARATHON⚔️CLICK HERE⚔️LORD DWARF⚔️ELITE GAMER⚔️FOCUS⚔️ + + + **WARNING!** + Smaller twitch channels with low stream time results is generic return value: + "Twitch is the world's leading video platform and community for gamers." From c30f4759a644b134712f7a36b5bf50001bcc8006 Mon Sep 17 00:00:00 2001 From: Andrew Perez-Ledo Date: Fri, 21 Jun 2024 22:09:33 -0400 Subject: [PATCH 3/3] Adding Twitch Stuff --- docs/modules/twitch.md | 6 +++--- documentation.md | 18 ++++++++++++++++++ src/scrape_up/twitch/twitch.py | 6 +++--- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/docs/modules/twitch.md b/docs/modules/twitch.md index c34395e6..8e55bb59 100644 --- a/docs/modules/twitch.md +++ b/docs/modules/twitch.md @@ -4,10 +4,10 @@ from scrape_up import twitch ### Scrape -First, create an object of class `Twitch_Scraper` +First, create an object of class `TwitchScraper` ```python -twitch_scraper = Twitch_Scraper() +twitch_scraper = TwitchScraper() ``` | Methods | Details | @@ -19,7 +19,7 @@ twitch_scraper = Twitch_Scraper() Example: using KaiCenat's twitch channel ```python - scraper = Twitch_Scraper() + scraper = TwitchScraper() title = scraper.scrape_title_description("kaicenat") print(title) ``` diff --git a/documentation.md b/documentation.md index cec75950..24d7fe1a 100644 --- a/documentation.md +++ b/documentation.md @@ -733,3 +733,21 @@ boxoffice = imdb.BoxOffice() | Methods | Details | | --------------- | ------------------------------------------------------------------------------- | | `.top_movies()` | Returns the top box office movies, weekend and total gross, and weeks released. | + +### Twitch + +```py +from scrape_up import twitch +``` + +Create an instance of `TwitchScraper` class + +```python +twitch_scraper = TwitchScraper() +``` + +| Method | Details | +| --------------------------- | -------------------------------------------------------------------- | +| `scrape_title_description(channel)` | Returns: Stream Title (if Live) or Channel Description (if Offline). | + +--- \ No newline at end of file diff --git a/src/scrape_up/twitch/twitch.py b/src/scrape_up/twitch/twitch.py index 6619df55..74d076ed 100644 --- a/src/scrape_up/twitch/twitch.py +++ b/src/scrape_up/twitch/twitch.py @@ -9,18 +9,18 @@ Steps for use: - 1. Create "Twitch_Scraper()" class instance + 1. Create "TwitchScraper()" class instance 2. call the instance's function "scrape_title_description()" providing the channel name as a string 3. Channel stream title is returned (if live), Channel Description is returned (if offline) Example: using KaiCenat's twitch channel - scraper = Twitch_Scraper() + scraper = TwitchScraper() title = scraper.scrape_title_description("kaicenat") print(title) ''' -class Twitch_Scraper(): +class TwitchScraper(): def __init__(self): self.status = None