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