Skip to content

Commit

Permalink
Merge pull request #1014 from kart2004/codeforces_contests
Browse files Browse the repository at this point in the history
Added Codeforces Contest Schedule/data
  • Loading branch information
nikhil25803 authored May 19, 2024
2 parents e585d2d + c11f4ca commit e25dc52
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 13 deletions.
11 changes: 4 additions & 7 deletions dev-documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -1635,13 +1635,12 @@ olympics = Olympics()

## Codeforces

Create an instance of `Users` class
Create an instance of `Codeforces` class

```python
from scrape_up import codeforces

codeforces_user = codeforces.Users(username="tourist")
codeforces_user.get_user_data()
codeforces = Codeforces()
```

Methods
Expand All @@ -1650,6 +1649,7 @@ Methods
| Methods | Details |
| -------------------------- | ---------------------------------- |
| `.get_user_data(username)` | Fetches user data from CodeForces. |
| `get_contests()` | Returns information on contests. |
```

---
Expand Down Expand Up @@ -1745,9 +1745,7 @@ result = steam.ScrapeGames(n0Games=5, tags=["Discounts", "F2P"])
| ----------------------------- | ------------------------------------------- |
| `.ScrapeGames(n0Games, tags)` | Scrapes game data for each specified filter |


-------

---

## Lichess

Expand Down Expand Up @@ -1853,4 +1851,3 @@ trek=Indiantrekking("hidden-lakes-of-kashmir")
| `outline_day_to_day_itinerary` | returns the ouline of the day to day itinerary |

---

3 changes: 1 addition & 2 deletions src/scrape_up/ambitionBox/company.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# import requests
# from bs4 import BeautifulSoup

Expand Down Expand Up @@ -78,4 +77,4 @@

# if __name__ == "__main__":
# c = Comapiens(10)
# c.scrape_companies()
# c.scrape_companies()
4 changes: 4 additions & 0 deletions src/scrape_up/codeforces/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .user import Users
from .contests import Contest

__all__ = ["Users", "Contest"]
84 changes: 84 additions & 0 deletions src/scrape_up/codeforces/contests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from bs4 import BeautifulSoup
import json
from scrape_up.config.request_config import RequestConfig, get


class Contest:
"""
First, create an object of class `Contest`
```python
codeforces = Contest()
```
| Methods | Details |
| ---------------------------- | ----------------------------------------------------------------------------------------- |
| `get_contests()` | Returns information on active contests like title, start, and duration |
"""

def __init__(self, *, config: RequestConfig = RequestConfig()):
headers = {"User-Agent": "scrapeup"}
self.config = config
if self.config.headers == {}:
self.config.set_headers(headers)

def get_contests(self):
"""
Method to fetch the list of active contests on Codeforces using web scraping.
Example
-------
```python
codeforces = Contest()
codeforces.get_contests()
```
Returns
-------
{
"data": [
{
"name": "Codeforces Round #731 (Div. 3)",
"start": "Aug/08/2021 17:35",
"length": "2 hrs"
},
...
],
"message": "Found contest list"
}
"""
codeforces_url = "https://codeforces.com/contests"
response = get(codeforces_url, self.config)

if response.status_code != 200:
json.dumps({"data": None, "message": "Cannot load Contest"})

soup = BeautifulSoup(response.text, "html.parser")
contest_list = []

try:

upcoming_contests = soup.find("div", {"class": "datatable"}).find_all("tr")
for contest in upcoming_contests:
columns = contest.find_all("td")
if len(columns) == 6:
name = columns[0].text.strip()
start_time_str = columns[2].text.strip()
duration_str = columns[3].text.strip()

name = " ".join(
line.strip() for line in name.splitlines() if line.strip()
)
name = name.replace("Enter »", "").strip()

contest_list.append(
{
"name": name,
"start": start_time_str,
"length": duration_str,
}
)

return contest_list
except Exception:
return None
2 changes: 1 addition & 1 deletion src/scrape_up/indiantrekking/trek.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ def outline_day_to_day_itinerary(self):
outline = self.soup.find("div", class_="itinerary").text
return outline
except:
return None
return None
5 changes: 2 additions & 3 deletions src/scrape_up/lichess/lichess.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __fetch_page_games(self, page_num):
game_list.append(game_info)
return game_list

def fetch_games(self,start_page=1,end_page=4):
def fetch_games(self, start_page=1, end_page=4):
"""
Fetch all the games data for the specified username.
Expand All @@ -53,7 +53,7 @@ def fetch_games(self,start_page=1,end_page=4):
```python
# Default usage:
games = scraper.fetch_games()
# Custom usage:
games = scraper.fetch_games(start_page=5, end_page=8)
```
Expand Down Expand Up @@ -130,4 +130,3 @@ def __get_pgn(self, game_data):
pgn_request = requests.get(f"https://lichess.org{gameUrl}")._content
parsed_pgn = BeautifulSoup(pgn_request, "lxml")
return parsed_pgn.find("div", {"class": "pgn"}).text

0 comments on commit e25dc52

Please sign in to comment.