| Conditions | 19 |
| Total Lines | 140 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 62 | def read_data(self, **kwargs): |
||
| 63 | """ |
||
| 64 | get the data from the service |
||
| 65 | |||
| 66 | :param kwargs: contain keyword args : trigger_id at least |
||
| 67 | :type kwargs: dict |
||
| 68 | :rtype: list |
||
| 69 | """ |
||
| 70 | twitter_status_url = 'https://www.twitter.com/{}/status/{}' |
||
| 71 | twitter_fav_url = 'https://www.twitter.com/{}/status/{}' |
||
| 72 | now = arrow.utcnow().to(settings.TIME_ZONE) |
||
| 73 | my_tweets = [] |
||
| 74 | search = {} |
||
| 75 | since_id = None |
||
| 76 | trigger_id = kwargs['trigger_id'] |
||
| 77 | date_triggered = arrow.get(kwargs['date_triggered']) |
||
| 78 | |||
| 79 | def _get_tweets(twitter_obj, search): |
||
| 80 | """ |
||
| 81 | get the tweets from twitter and return the filters to use : |
||
| 82 | search and count |
||
| 83 | |||
| 84 | :param twitter_obj: from Twitter model |
||
| 85 | :param search: filter used for twython.search() or |
||
| 86 | twython.get_user_timeline()) |
||
| 87 | :type twitter_obj: Object |
||
| 88 | :type search: dict |
||
| 89 | :return: count that limit the quantity of tweet to retrieve, |
||
| 90 | the filter named search, the tweets |
||
| 91 | :rtype: list |
||
| 92 | """ |
||
| 93 | |||
| 94 | """ |
||
| 95 | explanations about statuses : |
||
| 96 | when we want to track the tweet of a screen |
||
| 97 | statuses contain all of them |
||
| 98 | when we want to track all the tweet matching a tag |
||
| 99 | statuses contain statuses + metadata array |
||
| 100 | this is why we need to do |
||
| 101 | statuses = statuses['statuses'] |
||
| 102 | to be able to handle the result as for screen_name |
||
| 103 | """ |
||
| 104 | |||
| 105 | # get the tweets for a given tag |
||
| 106 | # https://dev.twitter.com/docs/api/1.1/get/search/tweets |
||
| 107 | statuses = '' |
||
| 108 | count = 100 |
||
| 109 | if twitter_obj.tag: |
||
| 110 | count = 100 |
||
| 111 | search['count'] = count |
||
| 112 | search['q'] = twitter_obj.tag |
||
| 113 | search['result_type'] = 'recent' |
||
| 114 | # do a search |
||
| 115 | statuses = self.twitter_api.search(**search) |
||
| 116 | # just return the content of te statuses array |
||
| 117 | statuses = statuses['statuses'] |
||
| 118 | |||
| 119 | # get the tweets from a given user |
||
| 120 | # https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline |
||
| 121 | elif twitter_obj.screen: |
||
| 122 | count = 200 |
||
| 123 | search['count'] = count |
||
| 124 | search['screen_name'] = twitter_obj.screen |
||
| 125 | |||
| 126 | # call the user timeline and get his tweet |
||
| 127 | try: |
||
| 128 | if twitter_obj.fav: |
||
| 129 | count = 20 |
||
| 130 | search['count'] = 20 |
||
| 131 | # get the favorites https://dev.twitter.com/rest/ |
||
| 132 | # reference/get/favorites/list |
||
| 133 | statuses = self.twitter_api.get_favorites(**search) |
||
| 134 | else: |
||
| 135 | statuses = self.twitter_api.get_user_timeline(**search) |
||
| 136 | except TwythonAuthError as e: |
||
| 137 | logger.error(e.msg, e.error_code) |
||
| 138 | update_result(trigger_id, msg=e.msg, status=False) |
||
| 139 | |||
| 140 | return count, search, statuses |
||
| 141 | |||
| 142 | if self.token is not None: |
||
| 143 | kw = {'model_name': 'Twitter', 'trigger_id': trigger_id} |
||
| 144 | twitter_obj = super(ServiceTwitter, self).read_data(**kw) |
||
| 145 | |||
| 146 | # https://dev.twitter.com/rest/public/timelines |
||
| 147 | if twitter_obj.since_id is not None and twitter_obj.since_id > 0: |
||
| 148 | since_id = twitter_obj.since_id |
||
| 149 | search = {'since_id': twitter_obj.since_id} |
||
| 150 | |||
| 151 | # first request to Twitter |
||
| 152 | count, search, statuses = _get_tweets(twitter_obj, search) |
||
| 153 | |||
| 154 | if len(statuses) > 0: |
||
| 155 | newest = None |
||
| 156 | for status in statuses: |
||
| 157 | if newest is None: |
||
| 158 | newest = True |
||
| 159 | # first query ; get the max id |
||
| 160 | search['max_id'] = max_id = status['id'] |
||
| 161 | |||
| 162 | since_id = search['since_id'] = statuses[-1]['id'] - 1 |
||
| 163 | |||
| 164 | count, search, statuses = _get_tweets(twitter_obj, search) |
||
| 165 | |||
| 166 | newest = None |
||
| 167 | if len(statuses) > 0: |
||
| 168 | my_tweets = [] |
||
| 169 | for s in statuses: |
||
| 170 | if newest is None: |
||
| 171 | newest = True |
||
| 172 | max_id = s['id'] - 1 |
||
| 173 | screen_name = s['user']['screen_name'] |
||
| 174 | # get the text of the tweet + url to this one |
||
| 175 | if twitter_obj.fav: |
||
| 176 | url = twitter_fav_url.format(screen_name, |
||
| 177 | s['id_str']) |
||
| 178 | title = _('Tweet Fav from @{}'.format(screen_name)) |
||
| 179 | else: |
||
| 180 | url = twitter_status_url.format(screen_name, |
||
| 181 | s['id_str']) |
||
| 182 | title = _('Tweet from @{}'.format(screen_name)) |
||
| 183 | # Wed Aug 29 17:12:58 +0000 2012 |
||
| 184 | my_date = arrow.get(s['created_at'], |
||
| 185 | 'ddd MMM DD HH:mm:ss Z YYYY') |
||
| 186 | published = arrow.get(my_date).to(settings.TIME_ZONE) |
||
| 187 | if date_triggered is not None and \ |
||
| 188 | published is not None and \ |
||
| 189 | now >= published >= date_triggered: |
||
| 190 | my_tweets.append({'title': title, |
||
| 191 | 'content': s['text'], |
||
| 192 | 'link': url, |
||
| 193 | 'my_date': my_date}) |
||
| 194 | # digester |
||
| 195 | self.send_digest_event(trigger_id, title, url) |
||
| 196 | cache.set('th_twitter_' + str(trigger_id), my_tweets) |
||
| 197 | Twitter.objects.filter(trigger_id=trigger_id).update( |
||
| 198 | since_id=since_id, |
||
| 199 | max_id=max_id, |
||
| 200 | count=count) |
||
| 201 | return my_tweets |
||
| 202 | |||
| 331 |