| Conditions | 17 |
| Total Lines | 124 |
| 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_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 | # call the user timeline and get his tweet |
||
| 125 | try: |
||
| 126 | statuses = self.twitter_api.get_user_timeline(**search) |
||
| 127 | except TwythonAuthError as e: |
||
| 128 | logger.error(e.msg, e.error_code) |
||
| 129 | update_result(trigger_id, msg=e.msg, status=False) |
||
| 130 | |||
| 131 | return count, search, statuses |
||
| 132 | |||
| 133 | if self.token is not None: |
||
| 134 | kw = {'model_name': 'Twitter', 'trigger_id': trigger_id} |
||
| 135 | twitter_obj = super(ServiceTwitter, self).read_data(**kw) |
||
| 136 | |||
| 137 | # https://dev.twitter.com/rest/public/timelines |
||
| 138 | if twitter_obj.since_id is not None and twitter_obj.since_id > 0: |
||
| 139 | since_id = twitter_obj.since_id |
||
| 140 | search = {'since_id': twitter_obj.since_id} |
||
| 141 | |||
| 142 | # first request to Twitter |
||
| 143 | count, search, statuses = _get_tweets(twitter_obj, search) |
||
| 144 | |||
| 145 | if len(statuses) > 0: |
||
| 146 | newest = None |
||
| 147 | for status in statuses: |
||
| 148 | if newest is None: |
||
| 149 | newest = True |
||
| 150 | # first query ; get the max id |
||
| 151 | search['max_id'] = max_id = status['id'] |
||
| 152 | |||
| 153 | since_id = search['since_id'] = statuses[-1]['id'] - 1 |
||
| 154 | |||
| 155 | count, search, statuses = _get_tweets(twitter_obj, search) |
||
| 156 | |||
| 157 | newest = None |
||
| 158 | if len(statuses) > 0: |
||
| 159 | my_tweets = [] |
||
| 160 | for s in statuses: |
||
| 161 | if newest is None: |
||
| 162 | newest = True |
||
| 163 | max_id = s['id'] - 1 |
||
| 164 | screen_name = s['user']['screen_name'] |
||
| 165 | # get the text of the tweet + url to this one |
||
| 166 | url = twitter_url.format(screen_name, |
||
| 167 | s['id_str']) |
||
| 168 | title = _('Tweet from @{}'.format(screen_name)) |
||
| 169 | # Wed Aug 29 17:12:58 +0000 2012 |
||
| 170 | my_date = arrow.get(s['created_at'], |
||
| 171 | 'ddd MMM DD HH:mm:ss Z YYYY') |
||
| 172 | published = arrow.get(my_date).to(settings.TIME_ZONE) |
||
| 173 | if date_triggered is not None and \ |
||
| 174 | published is not None and \ |
||
| 175 | now >= published >= date_triggered: |
||
| 176 | my_tweets.append({'title': title, |
||
| 177 | 'content': s['text'], |
||
| 178 | 'link': url, |
||
| 179 | 'my_date': my_date}) |
||
| 180 | cache.set('th_twitter_' + str(trigger_id), my_tweets) |
||
| 181 | Twitter.objects.filter(trigger_id=trigger_id).update( |
||
| 182 | since_id=since_id, |
||
| 183 | max_id=max_id, |
||
| 184 | count=count) |
||
| 185 | return my_tweets |
||
| 186 | |||
| 315 |