billboard
This module contains scrapers for the Billboard music charts site.
Example Usage:
from billboard import CHART_TYPE
result = CHART_TYPE(YYYY-MM-DD)
print(result.chart)
1"""This module contains scrapers for the Billboard music charts site. 2 3Example Usage: 4.. code-block:: python 5 from billboard import CHART_TYPE 6 result = CHART_TYPE(YYYY-MM-DD) 7 print(result.chart) 8 9""" 10 11from .album import AlbumChart 12from .artists import ArtistChart 13from .songs import BillboardChart, GlobalChart, SongCharts 14 15__all__ = [ 16 "SongCharts", 17 "BillboardChart", 18 "GlobalChart", 19 "ArtistChart", 20 "AlbumChart", 21]
12class SongCharts: 13 """Class to manage all song charts available. 14 15 Attributes: 16 date (str): The date for the charts in ISO 8601 format (YYYY-MM-DD) 17 hot100 (BillboardChart): The Hot 100 chart. 18 global200 (GlobalChart): The Global 200 chart. 19 """ 20 21 def __init__(self, date: Optional[str] = None) -> None: 22 """The constructor for the SongCharts. 23 24 Args: 25 date: An optional date (YYYY-MM-DD); if none is provided, yesterday is used. 26 """ 27 self.hot100 = BillboardChart(date) 28 self.global200 = GlobalChart(date) 29 30 @property 31 def hot_chart(self) -> List[SongEntry]: 32 """Return the Hot 100 chart. 33 34 Returns: 35 A list containing SongEntry elements. 36 """ 37 return self.hot100.chart 38 39 @property 40 def global_chart(self) -> List[SongEntry]: 41 """Return the Global 200 chart. 42 43 Returns: 44 A list containing SongEntry elements. 45 """ 46 return self.global200.chart
Class to manage all song charts available.
Attributes:
- date (str): The date for the charts in ISO 8601 format (YYYY-MM-DD)
- hot100 (BillboardChart): The Hot 100 chart.
- global200 (GlobalChart): The Global 200 chart.
21 def __init__(self, date: Optional[str] = None) -> None: 22 """The constructor for the SongCharts. 23 24 Args: 25 date: An optional date (YYYY-MM-DD); if none is provided, yesterday is used. 26 """ 27 self.hot100 = BillboardChart(date) 28 self.global200 = GlobalChart(date)
The constructor for the SongCharts.
Arguments:
- date: An optional date (YYYY-MM-DD); if none is provided, yesterday is used.
30 @property 31 def hot_chart(self) -> List[SongEntry]: 32 """Return the Hot 100 chart. 33 34 Returns: 35 A list containing SongEntry elements. 36 """ 37 return self.hot100.chart
Return the Hot 100 chart.
Returns:
A list containing SongEntry elements.
39 @property 40 def global_chart(self) -> List[SongEntry]: 41 """Return the Global 200 chart. 42 43 Returns: 44 A list containing SongEntry elements. 45 """ 46 return self.global200.chart
Return the Global 200 chart.
Returns:
A list containing SongEntry elements.
12class BillboardChart(SongChart): 13 """A data structure representing a week of Billboard Hot 100's charts. 14 15 Attributes: 16 date: The date for this chart in ISO 8601 format (YYYY-MM-DD). 17 chart: The chart for the given date containing all chart data. 18 auto_date: Determines if the object will auto update the date to the 19 previous week if the chosen one does not exist. 20 oldest_date: The oldest date allowed for a given chart. 21 """ 22 23 def __init__(self, date: Optional[str] = None, auto_date: bool = True) -> None: 24 """The constructor for a BillboardChart object. 25 26 Args: 27 date: An optional date (YYYY-MM-DD); if none is provided, yesterday is used. 28 auto_date: Determines if the object will auto update the date to the 29 previous week if the choosen one does not exist. 30 """ 31 super().__init__(date, auto_date, "1958-08-04") 32 33 def _generate_chart(self): 34 """Generate the chart for the given week.""" 35 response = make_request("hot-100", self.date) 36 if (data := parse_titled_request(response)) == [] and self.auto_date is True: 37 week_ago = datetime.fromisoformat(self.date) - timedelta(weeks=1) 38 self.date = week_ago.strftime("%Y-%m-%d") 39 else: 40 self.chart = data
A data structure representing a week of Billboard Hot 100's charts.
Attributes:
- date: The date for this chart in ISO 8601 format (YYYY-MM-DD).
- chart: The chart for the given date containing all chart data.
- auto_date: Determines if the object will auto update the date to the previous week if the chosen one does not exist.
- oldest_date: The oldest date allowed for a given chart.
23 def __init__(self, date: Optional[str] = None, auto_date: bool = True) -> None: 24 """The constructor for a BillboardChart object. 25 26 Args: 27 date: An optional date (YYYY-MM-DD); if none is provided, yesterday is used. 28 auto_date: Determines if the object will auto update the date to the 29 previous week if the choosen one does not exist. 30 """ 31 super().__init__(date, auto_date, "1958-08-04")
The constructor for a BillboardChart object.
Arguments:
- date: An optional date (YYYY-MM-DD); if none is provided, yesterday is used.
- auto_date: Determines if the object will auto update the date to the previous week if the choosen one does not exist.
12class GlobalChart(SongChart): 13 """A data structure representing a week of Billboard Global 200's charts. 14 15 Attributes: 16 date: The date for this chart in ISO 8601 format (YYYY-MM-DD). 17 chart: The chart for the given date containing all chart data. 18 auto_date: Determines if the object will auto update the date to the 19 previous week if the chosen one does not exist. 20 oldest_date: The oldest date allowed for a given chart. 21 """ 22 23 def __init__(self, date: Optional[str] = None, auto_date: bool = True) -> None: 24 """The constructor for a GlobalChart object. 25 26 Args: 27 date: An optional date (YYYY-MM-DD); if none is provided, yesterday is used. 28 auto_date: Determines if the object will auto update the date to the 29 previous week if the choosen one does not exist. 30 """ 31 super().__init__(date, auto_date, "2020-09-12") 32 33 def _generate_chart(self): 34 """Generate the chart for the given week.""" 35 response = make_request("billboard-global-200", self.date) 36 if (data := parse_titled_request(response)) == [] and self.auto_date is True: 37 week_ago = datetime.fromisoformat(self.date) - timedelta(weeks=1) 38 self.date = week_ago.strftime("%Y-%m-%d") 39 else: 40 self.chart = data
A data structure representing a week of Billboard Global 200's charts.
Attributes:
- date: The date for this chart in ISO 8601 format (YYYY-MM-DD).
- chart: The chart for the given date containing all chart data.
- auto_date: Determines if the object will auto update the date to the previous week if the chosen one does not exist.
- oldest_date: The oldest date allowed for a given chart.
23 def __init__(self, date: Optional[str] = None, auto_date: bool = True) -> None: 24 """The constructor for a GlobalChart object. 25 26 Args: 27 date: An optional date (YYYY-MM-DD); if none is provided, yesterday is used. 28 auto_date: Determines if the object will auto update the date to the 29 previous week if the choosen one does not exist. 30 """ 31 super().__init__(date, auto_date, "2020-09-12")
The constructor for a GlobalChart object.
Arguments:
- date: An optional date (YYYY-MM-DD); if none is provided, yesterday is used.
- auto_date: Determines if the object will auto update the date to the previous week if the choosen one does not exist.
12class ArtistChart(Chart): 13 """ 14 A data structure representing a week of the Artist 100 Billboard Chart. 15 16 Attributes: 17 date: The date for this chart in ISO 8601 format (YYYY-MM-DD). 18 chart: The chart for the given date containing all chart data. 19 auto_date: Determines if the object will auto update the date to the 20 previous week if the chosen one does not exist. 21 oldest_date: The oldest date allowed for a given chart. 22 """ 23 24 def __init__(self, date: Optional[str] = None, auto_date: bool = True) -> None: 25 """The constructor for a ArtistChart object. 26 27 Args: 28 date: An optional date (YYYY-MM-DD); if none is provided, yesterday is used. 29 auto_date: Determines if the object will auto update the date to the 30 previous week if the choosen one does not exist. 31 """ 32 super().__init__(date, auto_date, "2014-07-19") 33 34 @property 35 def top_spot(self) -> ArtistEntry: 36 """Get the top spot from this week. 37 38 Returns: 39 A data structure containing the top spot information. 40 """ 41 return self.chart[0] 42 43 def _generate_chart(self): 44 """Generate the chart for the given week.""" 45 response = make_request("artist-100", self.date) 46 if (data := parse_request(response)) == [] and self.auto_date is True: 47 week_ago = datetime.fromisoformat(self.date) - timedelta(weeks=1) 48 self.date = week_ago.strftime("%Y-%m-%d") 49 else: 50 self.chart = data
A data structure representing a week of the Artist 100 Billboard Chart.
Attributes:
- date: The date for this chart in ISO 8601 format (YYYY-MM-DD).
- chart: The chart for the given date containing all chart data.
- auto_date: Determines if the object will auto update the date to the previous week if the chosen one does not exist.
- oldest_date: The oldest date allowed for a given chart.
24 def __init__(self, date: Optional[str] = None, auto_date: bool = True) -> None: 25 """The constructor for a ArtistChart object. 26 27 Args: 28 date: An optional date (YYYY-MM-DD); if none is provided, yesterday is used. 29 auto_date: Determines if the object will auto update the date to the 30 previous week if the choosen one does not exist. 31 """ 32 super().__init__(date, auto_date, "2014-07-19")
The constructor for a ArtistChart object.
Arguments:
- date: An optional date (YYYY-MM-DD); if none is provided, yesterday is used.
- auto_date: Determines if the object will auto update the date to the previous week if the choosen one does not exist.
34 @property 35 def top_spot(self) -> ArtistEntry: 36 """Get the top spot from this week. 37 38 Returns: 39 A data structure containing the top spot information. 40 """ 41 return self.chart[0]
Get the top spot from this week.
Returns:
A data structure containing the top spot information.
12class AlbumChart(Chart): 13 """Data structure representing a week of the Album 200 Billboard Chart. 14 15 Attributes: 16 date: The date for this chart in ISO 8601 format (YYYY-MM-DD). 17 chart: The chart for the given date containing all chart data. 18 auto_date: Determines if the object will auto update the date to the 19 previous week if the chosen one does not exist. 20 oldest_date: The oldest date allowed for a given chart. 21 """ 22 23 def __init__(self, date: Optional[str] = None, auto_date: bool = True) -> None: 24 """The constructor for a AlbumChart object. 25 26 Args: 27 date: An optional date (YYYY-MM-DD); if none is provided, yesterday is used. 28 auto_date: Determines if the object will auto update the date to the 29 previous week if the choosen one does not exist. 30 """ 31 super().__init__(date, auto_date, "1963-08-17") 32 33 @property 34 def top_spot(self) -> AlbumEntry: 35 """Get the top spot from this week. 36 37 Returns: 38 A data structure containing the top spot information. 39 """ 40 return self.chart[0] 41 42 def _generate_chart(self): 43 """Generate the chart for the given week.""" 44 response = make_request("billboard-200", self.date) 45 if (data := parse_titled_request(response)) == [] and self.auto_date is True: 46 week_ago = datetime.fromisoformat(self.date) - timedelta(weeks=1) 47 self.date = week_ago.strftime("%Y-%m-%d") 48 else: 49 self.chart = data
Data structure representing a week of the Album 200 Billboard Chart.
Attributes:
- date: The date for this chart in ISO 8601 format (YYYY-MM-DD).
- chart: The chart for the given date containing all chart data.
- auto_date: Determines if the object will auto update the date to the previous week if the chosen one does not exist.
- oldest_date: The oldest date allowed for a given chart.
23 def __init__(self, date: Optional[str] = None, auto_date: bool = True) -> None: 24 """The constructor for a AlbumChart object. 25 26 Args: 27 date: An optional date (YYYY-MM-DD); if none is provided, yesterday is used. 28 auto_date: Determines if the object will auto update the date to the 29 previous week if the choosen one does not exist. 30 """ 31 super().__init__(date, auto_date, "1963-08-17")
The constructor for a AlbumChart object.
Arguments:
- date: An optional date (YYYY-MM-DD); if none is provided, yesterday is used.
- auto_date: Determines if the object will auto update the date to the previous week if the choosen one does not exist.
33 @property 34 def top_spot(self) -> AlbumEntry: 35 """Get the top spot from this week. 36 37 Returns: 38 A data structure containing the top spot information. 39 """ 40 return self.chart[0]
Get the top spot from this week.
Returns:
A data structure containing the top spot information.