본문 바로가기

Python/[Project] 트윗 캡쳐 투 비디오

트윗 캡쳐 후 비디오 만들기 6 - tweepy implementation

안녕하세요. 카이랏입니다.

 

드디어 다른 이로부터 받은 키를 갖고 트윗을 검색하는 기능을 만들 것입니다.

 

1. tweepy 설치

사용되는 파이썬 모듈은 tweepy 입니다.

아래와 같은 명령어를 터미널에서 입력하고 수행하면 자동으로 설치됩니다.

 

pip3 install tweepy

 

2. 검색 조건

1). 몇 개의 트윗을 가져올 것인가?

- 하나의 해시태그로 검색한 트윗 중 몇 개의 트윗을 가져올 지를 숫자로 입력하여야 합니다.

 

2). 어떻게 정렬을 할 것인가?

- "좋아요"개수 또는 "리트윗"개수 중 어느 것을 우선으로 정렬할 것인가?

- 두 가지 모두 많은 트윗도 있지만 그렇지 않고 하나만 더 많은 경우도 있습니다.

 

3). 검색 우선 순위는 어떻게 할 것인가?

- 검색 우선 순위는 총 3가지가 있는데 "popular", "recent", "mixed" 입니다.

- 먼저 많이 본 트윗을 검색하려고 했으나 이 조건은 검색할 파라미티에 없었습니다.

- 물론 "popular", "recent", "mixed"의 판단 조건은 공개되어 있지 않습니다. 다만 그러한 경향으로 검색한다 정도로만 알면 될 것 같습니다.

 

4). 리트윗을 검색할 것인가?

- 리트윗을 검색할 지 말지를 결정하는 변수가 있습니다.

- 검색하지 않을 경우 hashtag 뒤에 " exclude:retweets"를 붙이면 됩니다.

 

3. 위의 조건들을 넣은 코드

아래는 코드입니다~

def get_urls_from_twitter(self, 
                              twit_count_each_tag, 
                              eSorting_method = eSorting.eSORTING_OF_FAVORITES, 
                              eSearching_priority = ePriority.ePRIORITY_POPULAR,
                              include_retweets = False):
        
        try:   
            urls = []         
            
            ''' TEST CODE
            urls.append("https://twitter.com/jack/status/20")
            urls.append("https://twitter.com/jack/status/20")
            urls.append("https://twitter.com/jack/status/20")
            urls.append("https://twitter.com/jack/status/20")
            urls.append("https://twitter.com/jack/status/20")
            return urls'''
        
            print("########### START_GET_URL ###########")
            
            ################################
            # options for working
            count = 0
            rt_included_string = ""
            if include_retweets == True:
                rt_included_string = " exclude:retweets"
            
            searching_priority = ""
            if eSorting_method == eSorting.eSORTING_OF_FAVORITES:
                sorting_method = 0
            elif eSorting_method == eSorting.eSORTING_OF_RETWEETS:
                sorting_method = 1
            else:
                raise Exception("There is no sorting method")
            
            if eSearching_priority == ePriority.ePRIORITY_POPULAR:
                searching_priority = "popular"
            elif eSearching_priority == ePriority.ePRIORITY_RECENT:
                searching_priority = "recent"
            elif eSearching_priority == ePriority.ePRIORITY_MIXED:
                searching_priority = "mixed"
            else:
                raise Exception("There is no priority method")
            ################################
            
            # Search for tweets with the given hashtags
            for hashtag in self.hashtags:
                tweets = tweepy.Cursor(self.api.search_tweets, 
                                        q=hashtag + rt_included_string,
                                        result_type = searching_priority,
                                        tweet_mode = "extended", 
                                        lang = "en").items(twit_count_each_tag)
                
                temp_urls = {} # 리트윗수, 좋아요수로 정렬을 하기 위해 딕셔너리로 지정한다.
                # Take screenshots of top tweets
                for tweet in tweets:
                    
                    status = self.api.get_status(tweet.id)
                    # Navigate to tweet's URL
                    url = f"https://twitter.com/{tweet.user.screen_name}/status/{tweet.id}"
                    
                    # key : url of tweet
                    # 1st value : favorite count
                    # 2nd value : retweets count
                    temp_urls.update({url:[status.favorite_count, status.retweet_count]})
                    
                    count += 1
                
                # Sorted by 1st value favorite count
                sorted_dict = {
                    key: value for key, value in sorted(
                        # sorting_method 가 0 일 경우 favorite count, 1일 경우 retweets count
                        temp_urls.items(), key=lambda item: item[1][sorting_method], reverse=True 
                    )
                }
                list(map(lambda x: print(x[0], "/f=", x[1][0], "/rt=", x[1][1]), sorted_dict.items()))
                urls.extend(list(sorted_dict.keys())) # input sorted url to url list.
                
                print(f"## End of \"{hashtag}\" ##")
                    
            print("########### _END__GET_URL ###########")

        except Exception as e:
            print(f'Error={e}, Func={self.get_urls_from_twitter.__name__}')
        
        return urls

아래는 그 결과입니다.


Hello. This is Kairat.

 

Finally we'll create a function to retrieve tweets with keys from others.

 

1. Install tweepy

The Python module used is tweepy.

If you enter and run the following command in the terminal, it will be installed automatically.

 

pip3 install tweepy

 

2. Search criteria

1). How many tweets will you fetch?

 

2). How to sort?

- Do you want to sort by number of "Likes" or "Retweets" first?

- there may be many Tweets of both, but there may be more than one.

 

3). How to prioritize search?

- There are three search priorities: "popular", "recent", "mixed" .

- Of course, the conditions for determining "popular", "recent", and "mixed" are not disclosed. However, if you know only to search for such tendencies, I think it will.

 

4). Search retweets?

- There is a variable that determines whether to retrieve retweets or not.

- If you don't want to search, add " exclude:retweets" after the hashtag.

 

3. Code with the above conditions

Below is the code~

def get_urls_from_twitter(self,
                               twit_count_each_tag,
                               eSorting_method = eSorting.eSORTING_OF_FAVORITES,
                               eSearching_priority = ePriority.ePRIORITY_POPULAR,
                               include_retweets = False):
        
         try:
             urls = []
            
             ''' TEST CODE
             urls.append("https://twitter.com/jack/status/20")
             urls.append("https://twitter.com/jack/status/20")
             urls.append("https://twitter.com/jack/status/20")
             urls.append("https://twitter.com/jack/status/20")
             urls.append("https://twitter.com/jack/status/20")
             return urls'''
        
             print("########### START_GET_URL ###########")
            
             ##############################
             # options for working
             count = 0
             rt_included_string = ""
             if include_retweets == True:
                 rt_included_string = " exclude:retweets"
            
             searching_priority=""
             if eSorting_method == eSorting.eSORTING_OF_FAVORITES:
                 sorting_method = 0
             elif eSorting_method == eSorting.eSORTING_OF_RETWEETS:
                 sorting_method = 1
             else:
                 raise Exception("There is no sorting method")
            
             if eSearching_priority == ePriority.ePRIORITY_POPULAR:
                 searching_priority = "popular"elif eSearching_priority == ePriority.ePRIORITY_RECENT:
                 searching_priority = "recent"
             elif eSearching_priority == ePriority.ePRIORITY_MIXED:
                 searching_priority = "mixed"
             else:
                 raise Exception("There is no priority method")
             ##############################
            
             # Search for tweets with the given hashtags
             for hashtag in self. hashtags:
                 tweets = tweepy.Cursor(self.api.search_tweets,
                                         q=hashtag + rt_included_string,
                                         result_type = searching_priority,
                                         tweet_mode = "extended",
                                         lang = "en").items(twit_count_each_tag)
                
                 temp_urls = {} # Specify as a dictionary to sort by the number of retweets and likes.
                 # Take screenshots of top tweets
                 for tweet in tweets:
                    
                     status = self.api.get_status(tweet.id)
                     # Navigate to tweet's URL
                     url = f"https://twitter.com/{tweet.user.screen_name}/status/{tweet.id}"
                    
                     # key : url of tweet
                     # 1st value : favorite count
                     # 2nd value: retweets count
                     temp_urls.update({url:[status.favorite_count, status.retweet_count]})
                    
                     count += 1
                
                 # Sorted by 1st value favorite count
                 sorted_dict = {
                     key: value for key, value in sorted(
                         # Favorite count if sorting_method is 0, retweets count if 1
                         temp_urls.items(), key=lambda item: item[1][sorting_method], reverse=True
                     )
                 }
                 list(map(lambda x: print(x[0], "/f=", x[1][0], "/rt=", x[1][1]), sorted_dict.items()))
                 urls. extend(list(sorted_dict. keys())) # input sorted url to url list.
                
                 print(f"## End of \"{hashtag}\" ##")
                    
             print("########### _END__GET_URL ###########")

         except Exception as e:
             print(f'Error={e}, Func={self.get_urls_from_twitter.__name__}')
        
         return urls

The result is below.