@@ -29,15 +29,15 @@ def login(self, username: str, password: str, timeout: Optional[float] = 10000)
2929 self .page .get_by_label ('Password' ).press ('Enter' )
3030 self .page .wait_for_url ('https://twitter.com/home' )
3131
32- def get_all_liked_tweets (self , username : str , scroll_timeout : float = 0.8 ) -> list [str ]:
32+ def get_all_liked_video_tweets (self , username : str , scroll_timeout : float = 0.8 ) -> list [str ]:
3333 """Get the username's all liked tweets
3434 Returns the list of links of liked tweets
3535 """
36- return self .get_liked_tweets_until (
36+ return self .get_liked_video_tweets_until (
3737 username , 'nothing' , scroll_timeout
3838 ) # 'nothing' was intended because the given `until_link` would be never found on the links list
3939
40- def get_liked_tweets_until (self , username : str , until_link : str , scroll_timeout : float = 0.8 ) -> list [str ]:
40+ def get_liked_video_tweets_until (self , username : str , until_link : str , scroll_timeout : float = 0.8 ) -> list [str ]:
4141 """Scrolling down the list of liked tweets until the given `until_link` found
4242 Returns the list of links of liked tweets
4343 """
@@ -57,7 +57,7 @@ def get_liked_tweets_until(self, username: str, until_link: str, scroll_timeout:
5757 break
5858 previous_height = self .page_current_height
5959
60- new_links = self ._get_article_links_in_current_screen ()
60+ new_links = self ._get_video_tweets_in_current_screen ()
6161 links .extend (new_links )
6262 links = list (set (links ))
6363
@@ -70,7 +70,7 @@ def get_liked_tweets_until(self, username: str, until_link: str, scroll_timeout:
7070
7171 def get_recent_liked_tweet (self , username : str ) -> str :
7272 self ._goto_liked_tweets (username )
73- return self ._get_article_links_in_current_screen ()[0 ]
73+ return self ._get_tweets_in_current_screen ()[0 ]
7474
7575 def get_video_of_tweet (self , link : str , timeout : Optional [float ] = 10000 ) -> list [tuple [str , str ]]:
7676 video_links : list [str ] = []
@@ -97,7 +97,26 @@ def _goto_liked_tweets(self, username: str) -> None:
9797 self .page .goto (f'https://twitter.com/{ username } /likes' )
9898 self .page .wait_for_selector ('article' )
9999
100- def _get_article_links_in_current_screen (self ) -> list [str ]:
100+ def _get_video_tweets_in_current_screen (self ) -> list [str ]:
101+ links : list [str ] = []
102+
103+ while True :
104+ articles = self .page .locator ('article:has(video)' )
105+ article_length = articles .count ()
106+ try :
107+ links = [
108+ 'https://twitter.com' +
109+ (articles .nth (i ).locator ('div' ).locator ('a' ).nth (3 ).get_attribute ('href' , timeout = 500 ) or '' )
110+ for i in range (article_length )
111+ ]
112+ break
113+ except Error : # if articles in the page are not reachable
114+ self .page .mouse .wheel (0 , 500 ) # scrolling down to refresh the articles
115+ self .page .mouse .wheel (0 , - 500 ) # scrolling down to refresh the articles
116+
117+ return links
118+
119+ def _get_tweets_in_current_screen (self ) -> list [str ]:
101120 links : list [str ] = []
102121
103122 while True :
0 commit comments