| Conditions | 22 |
| Total Lines | 153 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like ServiceTwitter.read_data() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # coding: utf-8 |
||
| 61 | def read_data(self, **kwargs): |
||
| 62 | """ |
||
| 63 | get the data from the service |
||
| 64 | |||
| 65 | :param kwargs: contain keyword args : trigger_id at least |
||
| 66 | :type kwargs: dict |
||
| 67 | :rtype: list |
||
| 68 | """ |
||
| 69 | twitter_status_url = 'https://www.twitter.com/{}/status/{}' |
||
| 70 | twitter_fav_url = 'https://www.twitter.com/{}/status/{}' |
||
| 71 | now = arrow.utcnow().to(settings.TIME_ZONE) |
||
| 72 | my_tweets = [] |
||
| 73 | search = {} |
||
| 74 | since_id = None |
||
| 75 | trigger_id = kwargs['trigger_id'] |
||
| 76 | date_triggered = arrow.get(kwargs['date_triggered']) |
||
| 77 | |||
| 78 | def _get_tweets(twitter_obj, search): |
||
| 79 | """ |
||
| 80 | get the tweets from twitter and return the filters to use : |
||
| 81 | search and count |
||
| 82 | |||
| 83 | :param twitter_obj: from Twitter model |
||
| 84 | :param search: filter used for twython.search() or |
||
| 85 | twython.get_user_timeline()) |
||
| 86 | :type twitter_obj: Object |
||
| 87 | :type search: dict |
||
| 88 | :return: count that limit the quantity of tweet to retrieve, |
||
| 89 | the filter named search, the tweets |
||
| 90 | :rtype: list |
||
| 91 | """ |
||
| 92 | |||
| 93 | """ |
||
| 94 | explanations about statuses : |
||
| 95 | when we want to track the tweet of a screen |
||
| 96 | statuses contain all of them |
||
| 97 | when we want to track all the tweet matching a tag |
||
| 98 | statuses contain statuses + metadata array |
||
| 99 | this is why we need to do |
||
| 100 | statuses = statuses['statuses'] |
||
| 101 | to be able to handle the result as for screen_name |
||
| 102 | """ |
||
| 103 | |||
| 104 | # get the tweets for a given tag |
||
| 105 | # https://dev.twitter.com/docs/api/1.1/get/search/tweets |
||
| 106 | statuses = '' |
||
| 107 | count = 100 |
||
| 108 | if twitter_obj.tag: |
||
| 109 | count = 100 |
||
| 110 | search['count'] = count |
||
| 111 | search['q'] = twitter_obj.tag |
||
| 112 | search['result_type'] = 'recent' |
||
| 113 | # do a search |
||
| 114 | statuses = self.twitter_api.search(**search) |
||
| 115 | # just return the content of te statuses array |
||
| 116 | statuses = statuses['statuses'] |
||
| 117 | |||
| 118 | # get the tweets from a given user |
||
| 119 | # https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline |
||
| 120 | elif twitter_obj.screen: |
||
| 121 | count = 200 |
||
| 122 | search['count'] = count |
||
| 123 | search['screen_name'] = twitter_obj.screen |
||
| 124 | |||
| 125 | # call the user timeline and get his tweet |
||
| 126 | try: |
||
| 127 | if twitter_obj.fav: |
||
| 128 | count = 20 |
||
| 129 | search['count'] = 20 |
||
| 130 | # get the favorites https://dev.twitter.com/rest/ |
||
| 131 | # reference/get/favorites/list |
||
| 132 | statuses = self.twitter_api.get_favorites(**search) |
||
| 133 | else: |
||
| 134 | statuses = self.twitter_api.get_user_timeline(**search) |
||
| 135 | except TwythonAuthError as e: |
||
| 136 | logger.error(e.msg, e.error_code) |
||
| 137 | update_result(trigger_id, msg=e.msg, status=False) |
||
| 138 | |||
| 139 | return count, search, statuses |
||
| 140 | |||
| 141 | if self.token is not None: |
||
| 142 | kw = {'app_label': 'th_twitter', |
||
| 143 | 'model_name': 'Twitter', |
||
| 144 | 'trigger_id': trigger_id} |
||
| 145 | twitter_obj = super(ServiceTwitter, self).read_data(**kw) |
||
| 146 | |||
| 147 | # https://dev.twitter.com/rest/public/timelines |
||
| 148 | if twitter_obj.since_id is not None and twitter_obj.since_id > 0: |
||
| 149 | since_id = twitter_obj.since_id |
||
| 150 | search = {'since_id': twitter_obj.since_id} |
||
| 151 | |||
| 152 | # first request to Twitter |
||
| 153 | count, search, statuses = _get_tweets(twitter_obj, search) |
||
| 154 | |||
| 155 | if len(statuses) > 0: |
||
| 156 | newest = None |
||
| 157 | for status in statuses: |
||
| 158 | if newest is None: |
||
| 159 | newest = True |
||
| 160 | # first query ; get the max id |
||
| 161 | search['max_id'] = max_id = status['id'] |
||
| 162 | |||
| 163 | since_id = search['since_id'] = statuses[-1]['id'] - 1 |
||
| 164 | |||
| 165 | count, search, statuses = _get_tweets(twitter_obj, search) |
||
| 166 | |||
| 167 | newest = None |
||
| 168 | if len(statuses) > 0: |
||
| 169 | my_tweets = [] |
||
| 170 | for s in statuses: |
||
| 171 | if newest is None: |
||
| 172 | newest = True |
||
| 173 | max_id = s['id'] - 1 |
||
| 174 | screen_name = s['user']['screen_name'] |
||
| 175 | # get the text of the tweet + url to this one |
||
| 176 | if twitter_obj.fav: |
||
| 177 | url = twitter_fav_url.format(screen_name, |
||
| 178 | s['id_str']) |
||
| 179 | title = _('Tweet Fav from @{}'.format(screen_name)) |
||
| 180 | else: |
||
| 181 | url = twitter_status_url.format(screen_name, |
||
| 182 | s['id_str']) |
||
| 183 | title = _('Tweet from @{}'.format(screen_name)) |
||
| 184 | # Wed Aug 29 17:12:58 +0000 2012 |
||
| 185 | my_date = arrow.get(s['created_at'], |
||
| 186 | 'ddd MMM DD HH:mm:ss Z YYYY') |
||
| 187 | published = arrow.get(my_date).to(settings.TIME_ZONE) |
||
| 188 | if date_triggered is not None and \ |
||
| 189 | published is not None and \ |
||
| 190 | now >= published >= date_triggered: |
||
| 191 | if s.get('extended_entities'): |
||
| 192 | # get a media |
||
| 193 | extended_entities = s['extended_entities'] |
||
| 194 | if extended_entities.get('media'): |
||
| 195 | medias = extended_entities.get('media') |
||
| 196 | for media in medias: |
||
| 197 | text = s['text'] + ' ' + \ |
||
| 198 | media.get('media_url_https') |
||
| 199 | else: |
||
| 200 | text = s['text'] |
||
| 201 | |||
| 202 | my_tweets.append({'title': title, |
||
| 203 | 'content': text, |
||
| 204 | 'link': url, |
||
| 205 | 'my_date': my_date}) |
||
| 206 | # digester |
||
| 207 | self.send_digest_event(trigger_id, title, url) |
||
| 208 | cache.set('th_twitter_' + str(trigger_id), my_tweets) |
||
| 209 | Twitter.objects.filter(trigger_id=trigger_id).update( |
||
| 210 | since_id=since_id, |
||
| 211 | max_id=max_id, |
||
| 212 | count=count) |
||
| 213 | return my_tweets |
||
| 214 | |||
| 343 |