-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expand file tree
/
Copy pathelement.py
More file actions
55 lines (42 loc) · 2.19 KB
/
element.py
File metadata and controls
55 lines (42 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.remote.webelement import WebElement
from bandcamp.web.base import Track, WebComponent
from bandcamp.web.locators import HomePageLocator, TrackLocator
class TrackElement(WebComponent, TrackLocator):
"""Model a playable track in Bandcamp's Discover section."""
def play(self) -> None:
"""Play the track."""
if not self.is_playing():
self._get_play_button().click()
self._wait.until(lambda _: self.is_playing())
def is_playing(self) -> bool:
return "playing" in self._get_play_button().get_attribute("class")
def _get_track_info(self) -> Track:
"""Create a representation of the track's relevant information."""
full_url = self._parent.find_element(*self.ALBUM).get_attribute("href")
# Cut off the referrer query parameter
clean_url = full_url.split("?")[0] if full_url else ""
return Track(
album=self._parent.find_element(*self.ALBUM).text,
artist=self._parent.find_element(*self.ARTIST).text,
genre=self._parent.find_element(*self.GENRE).text,
url=clean_url,
)
def _get_play_button(self):
return self._parent.find_element(*self.PLAY_BUTTON)
class DiscoverTrackList(WebComponent, HomePageLocator):
"""Model the track list in Bandcamp's Discover section."""
def __init__(self, parent: WebElement, driver: WebDriver = None) -> None:
super().__init__(parent, driver)
self.available_tracks = self._get_available_tracks()
def load_more(self) -> None:
"""Load additional tracks in the Discover section."""
self._get_next_page_button().click()
self.available_tracks = self._get_available_tracks()
def _get_available_tracks(self) -> list:
"""Find all currently available tracks in the Discover section."""
all_tracks = self._driver.find_elements(*self.TRACK)
return [track for track in all_tracks if track.is_displayed()]
def _get_next_page_button(self):
"""Locate and return the 'Next' button that loads more results."""
return self._driver.find_elements(*self.PAGINATION_BUTTON)[-1]