Skip to content

Commit

Permalink
Merge pull request #932 from Arnav17Sharma/main
Browse files Browse the repository at this point in the history
Feat: Add new module with cricket Live Scores and news. #896
  • Loading branch information
nikhil25803 authored May 16, 2024
2 parents a314fa9 + d90e003 commit cc699bf
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 0 deletions.
17 changes: 17 additions & 0 deletions dev-documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,23 @@ espn = ESPN()
| `get_tournaments()` | Fetches and returns information about football tournaments. |
| `get_teams()` | Fetches and returns information about football teams. |

### ESPNCricinfo

```py
from scrape_up import espncricinfo
```

Create an instance of `Espncricinfo` class.

```python
obj = espncricinfo.Espncricinfo()
```

| Methods | Details |
| ------------------- | ------------------------------------------------- |
| `.get_news()` | Returns a latest news from ESPNCricinfo. |
| `.get_livescores()` | Returns a list of live matches from ESPNCricinfo. |

# Magic Bricks

Create an instance of `MagicBricks` class
Expand Down
3 changes: 3 additions & 0 deletions src/scrape_up/espncricinfo/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .espncricinfo import Espncricinfo

__all__ = ["Espncricinfo"]
103 changes: 103 additions & 0 deletions src/scrape_up/espncricinfo/espncricinfo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import requests
from bs4 import BeautifulSoup


class Espncricinfo:
"""
Create an instance of `Espncricinfo` class.
```python
obj = Espncricinfo()
```
| Methods | Details |
| ---------------------------- | --------------------------------------------------|
| `.get_news()` | Returns a latest news from ESPNCricinfo. |
| `.get_livescores()` | Returns a list of live matches from ESPNCricinfo. |
"""

def __init__(self):
self.session = requests.Session()
self.BASE_URL = "https://www.espncricinfo.com"

def get_news(self):
news = []
URL = self.BASE_URL + "/cricket-news"
try:
res = self.session.get(URL)
if res.status_code != 200:
return [{"error": "Unable to fetch data from ESPN"}]
soup = BeautifulSoup(res.text, "html.parser")
search_paras = {"class": "ds-flex ds-flex-col"}
news_list = soup.find_all("div", attrs=search_paras)
for i in news_list:
details = {"headlines": i.find("h2").text}
news.append(details)
return news
except:
return news

def get_livescores(self):
live_scores = []
URL = self.BASE_URL + "/live-cricket-score"
try:
res = self.session.get(URL)
if res.status_code != 200:
return [{"error": "Unable to fetch data from ESPN"}]
soup = BeautifulSoup(res.text, "html.parser")
search_paras = {"class": "ds-flex ds-flex-wrap"}
match_box_container = soup.find("div", attrs=search_paras)
match_boxes1 = soup.find_all(
"div",
attrs={"class": "ds-border-b ds-border-line ds-border-r ds-w-1/2"},
)
match_boxes2 = soup.find_all(
"div", attrs={"class": "ds-border-b ds-border-line ds-w-1/2"}
)
match_boxes = match_boxes1 + match_boxes2
for match in match_boxes:
curr_status = match.find(
"span",
attrs={
"class": "ds-text-tight-xs ds-font-bold ds-uppercase ds-leading-5"
},
).text
if curr_status == "Live":
about = match.find(
"div",
attrs={
"class": "ds-text-tight-xs ds-truncate ds-text-typo-mid3"
},
).text
teams = match.find_all(
"div",
attrs={
"class": "ci-team-score ds-flex ds-justify-between ds-items-center ds-text-typo ds-my-1"
},
)
team1_details = teams[0].find_all("div")
team2_details = teams[1].find_all("div")

team1_name = team1_details[0].text
team1_score = team1_details[1].text
team2_name = team2_details[0].text
team2_score = team2_details[1].text

match_status = match.find(
"p",
attrs={
"class": "ds-text-tight-s ds-font-medium ds-truncate ds-text-typo"
},
).text
match_details = {
"current": curr_status,
"about": about,
"team1": team1_name,
"team2": team2_name,
"team1_score": team1_score,
"team2_score": team2_score,
"status": match_status,
}
live_scores.append(match_details)
return live_scores
except:
return live_scores
36 changes: 36 additions & 0 deletions src/test/espncricinfo_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import unittest
from scrape_up.espncricinfo import Espncricinfo


class ESPNTest(unittest.TestCase):

def test_connection(self):
instance = Espncricinfo()
self.assertTrue(
instance,
"ESPN:__init__ - connection failed",
)

def test_get_news(self):
instance = Espncricinfo()
method_response = instance.get_news()

self.assertIsInstance(
method_response,
list,
"ESPN:get_news - invalid response",
)

def test_get_livescores(self):
instance = Espncricinfo()
method_response = instance.get_livescores()

self.assertIsInstance(
method_response,
list,
"ESPN:get_livescores - invalid response",
)


if __name__ == "__main__":
unittest.main()

0 comments on commit cc699bf

Please sign in to comment.