| Conditions | 16 |
| Total Lines | 120 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 46 | def read_data(self, **kwargs): |
||
| 47 | """ |
||
| 48 | get the data from the service |
||
| 49 | |||
| 50 | :param kwargs: contain keyword args : trigger_id at least |
||
| 51 | :type kwargs: dict |
||
| 52 | :rtype: list |
||
| 53 | """ |
||
| 54 | twitter_url = 'https://www.twitter.com/{}/status/{}' |
||
| 55 | now = arrow.utcnow().to(settings.TIME_ZONE) |
||
| 56 | my_tweets = [] |
||
| 57 | search = {} |
||
| 58 | since_id = None |
||
| 59 | trigger_id = kwargs['trigger_id'] |
||
| 60 | date_triggered = kwargs['date_triggered'] |
||
| 61 | |||
| 62 | def _get_tweets(twitter_obj, search): |
||
| 63 | """ |
||
| 64 | get the tweets from twitter and return the filters to use : |
||
| 65 | search and count |
||
| 66 | |||
| 67 | :param twitter_obj: from Twitter model |
||
| 68 | :param search: filter used for twython.search() or |
||
| 69 | twython.get_user_timeline()) |
||
| 70 | :type twitter_obj: Object |
||
| 71 | :type search: dict |
||
| 72 | :return: count that limit the quantity of tweet to retrieve, |
||
| 73 | the filter named search, the tweets |
||
| 74 | :rtype: list |
||
| 75 | """ |
||
| 76 | |||
| 77 | """ |
||
| 78 | explanations about statuses : |
||
| 79 | when we want to track the tweet of a screen |
||
| 80 | statuses contain all of them |
||
| 81 | when we want to track all the tweet matching a tag |
||
| 82 | statuses contain statuses + metadata array |
||
| 83 | this is why we need to do |
||
| 84 | statuses = statuses['statuses'] |
||
| 85 | to be able to handle the result as for screen_name |
||
| 86 | """ |
||
| 87 | |||
| 88 | # get the tweets for a given tag |
||
| 89 | # https://dev.twitter.com/docs/api/1.1/get/search/tweets |
||
| 90 | statuses = '' |
||
| 91 | count = 100 |
||
| 92 | if twitter_obj.tag != '': |
||
| 93 | count = 100 |
||
| 94 | search['count'] = count |
||
| 95 | search['q'] = twitter_obj.tag |
||
| 96 | search['result_type'] = 'recent' |
||
| 97 | # do a search |
||
| 98 | statuses = self.twitter_api.search(**search) |
||
| 99 | # just return the content of te statuses array |
||
| 100 | statuses = statuses['statuses'] |
||
| 101 | |||
| 102 | # get the tweets from a given user |
||
| 103 | # https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline |
||
| 104 | elif twitter_obj.screen != '': |
||
| 105 | count = 200 |
||
| 106 | search['count'] = count |
||
| 107 | search['screen_name'] = twitter_obj.screen |
||
| 108 | # call the user timeline and get his tweet |
||
| 109 | statuses = self.twitter_api.get_user_timeline(**search) |
||
| 110 | |||
| 111 | return count, search, statuses |
||
| 112 | |||
| 113 | if self.token is not None: |
||
| 114 | kw = {'model_name': 'Twitter', 'trigger_id': trigger_id} |
||
| 115 | twitter_obj = super(ServiceTwitter, self).read_data(**kw) |
||
| 116 | |||
| 117 | # https://dev.twitter.com/rest/public/timelines |
||
| 118 | if twitter_obj.since_id is not None and twitter_obj.since_id > 0: |
||
| 119 | since_id = twitter_obj.since_id |
||
| 120 | search = {'since_id': twitter_obj.since_id} |
||
| 121 | |||
| 122 | # first request to Twitter |
||
| 123 | count, search, statuses = _get_tweets(twitter_obj, search) |
||
| 124 | |||
| 125 | if len(statuses) > 0: |
||
| 126 | newest = None |
||
| 127 | for status in statuses: |
||
| 128 | if newest is None: |
||
| 129 | newest = True |
||
| 130 | # first query ; get the max id |
||
| 131 | search['max_id'] = max_id = status['id'] |
||
| 132 | |||
| 133 | since_id = search['since_id'] = statuses[-1]['id'] - 1 |
||
| 134 | |||
| 135 | count, search, statuses = _get_tweets(twitter_obj, search) |
||
| 136 | |||
| 137 | newest = None |
||
| 138 | if len(statuses) > 0: |
||
| 139 | my_tweets = [] |
||
| 140 | for s in statuses: |
||
| 141 | if newest is None: |
||
| 142 | newest = True |
||
| 143 | max_id = s['id'] - 1 |
||
| 144 | screen_name = s['user']['screen_name'] |
||
| 145 | # get the text of the tweet + url to this one |
||
| 146 | url = twitter_url.format(screen_name, |
||
| 147 | s['id_str']) |
||
| 148 | title = _('Tweet from @{}'.format(screen_name)) |
||
| 149 | # Wed Aug 29 17:12:58 +0000 2012 |
||
| 150 | my_date = arrow.get(s['created_at'], |
||
| 151 | 'ddd MMM DD HH:mm:ss Z YYYY') |
||
| 152 | published = arrow.get(my_date).to(settings.TIME_ZONE) |
||
| 153 | if date_triggered is not None and \ |
||
| 154 | published is not None and \ |
||
| 155 | now >= published >= date_triggered: |
||
| 156 | my_tweets.append({'title': title, |
||
| 157 | 'content': s['text'], |
||
| 158 | 'link': url, |
||
| 159 | 'my_date': my_date}) |
||
| 160 | cache.set('th_twitter_' + str(trigger_id), my_tweets) |
||
| 161 | Twitter.objects.filter(trigger_id=trigger_id).update( |
||
| 162 | since_id=since_id, |
||
| 163 | max_id=max_id, |
||
| 164 | count=count) |
||
| 165 | return my_tweets |
||
| 166 | |||
| 266 |