| Conditions | 19 |
| Total Lines | 139 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 ServiceMastodon.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 |
||
| 36 | def read_data(self, **kwargs): |
||
| 37 | """ |
||
| 38 | get the data from the service |
||
| 39 | |||
| 40 | :param kwargs: contain keyword args : trigger_id at least |
||
| 41 | :type kwargs: dict |
||
| 42 | :rtype: list |
||
| 43 | """ |
||
| 44 | now = arrow.utcnow().to(settings.TIME_ZONE) |
||
| 45 | my_toots = [] |
||
| 46 | search = {} |
||
| 47 | since_id = None |
||
| 48 | trigger_id = kwargs['trigger_id'] |
||
| 49 | date_triggered = arrow.get(kwargs['date_triggered']) |
||
| 50 | |||
| 51 | def _get_toots(toot_api, toot_obj, search): |
||
| 52 | """ |
||
| 53 | get the toots from mastodon and return the filters to use : |
||
| 54 | search and count |
||
| 55 | |||
| 56 | :param toot_obj: from Mastodon model |
||
| 57 | :param search: filter used for MastodonAPI.search() |
||
| 58 | :type toot_obj: Object |
||
| 59 | :type search: dict |
||
| 60 | :return: count that limit the quantity of tweet to retrieve, |
||
| 61 | the filter named search, the tweets |
||
| 62 | :rtype: list |
||
| 63 | """ |
||
| 64 | |||
| 65 | # get the toots for a given tag |
||
| 66 | statuses = '' |
||
| 67 | count = 100 |
||
| 68 | if toot_obj.tag: |
||
| 69 | count = 100 |
||
| 70 | search['count'] = count |
||
| 71 | search['q'] = toot_obj.tag |
||
| 72 | search['result_type'] = 'recent' |
||
| 73 | # do a search |
||
| 74 | statuses = toot_api.search(**search) |
||
| 75 | # just return the content of te statuses array |
||
| 76 | statuses = statuses['statuses'] |
||
| 77 | |||
| 78 | # get the tweets from a given user |
||
| 79 | elif toot_obj.tooter: |
||
| 80 | count = 200 |
||
| 81 | search['count'] = count |
||
| 82 | search['username'] = toot_obj.tooter |
||
| 83 | |||
| 84 | # call the user timeline and get his toot |
||
| 85 | if toot_obj.fav: |
||
| 86 | count = 20 |
||
| 87 | search['count'] = 20 |
||
| 88 | statuses = toot_api.favourites(max_id=max_id, |
||
| 89 | since_id=since_id, |
||
| 90 | limit=count) |
||
| 91 | else: |
||
| 92 | statuses = toot_api.account_statuses( |
||
| 93 | id=toot_obj.tooter, |
||
| 94 | max_id=max_id, |
||
| 95 | since_id=since_id, |
||
| 96 | limit=count) |
||
| 97 | |||
| 98 | return count, search, statuses |
||
| 99 | |||
| 100 | if self.token is not None: |
||
| 101 | kw = {'app_label': 'th_mastodon', 'model_name': 'Mastodon', |
||
| 102 | 'trigger_id': trigger_id} |
||
| 103 | toot_obj = super(ServiceMastodon, self).read_data(**kw) |
||
| 104 | |||
| 105 | us = UserService.objects.get(user=self.user, |
||
| 106 | token=self.token, |
||
| 107 | name='ServiceMastodon') |
||
| 108 | try: |
||
| 109 | toot_api = MastodonAPI( |
||
| 110 | client_id=us.client_id, |
||
| 111 | client_secret=us.client_secret, |
||
| 112 | access_token=self.token, |
||
| 113 | api_base_url=us.host |
||
| 114 | ) |
||
| 115 | except ValueError as e: |
||
| 116 | logger.error(e) |
||
| 117 | update_result(trigger_id, msg=e, status=False) |
||
| 118 | |||
| 119 | if toot_obj.since_id is not None and toot_obj.since_id > 0: |
||
| 120 | since_id = toot_obj.since_id |
||
| 121 | search = {'since_id': toot_obj.since_id} |
||
| 122 | |||
| 123 | # first request to Mastodon |
||
| 124 | count, search, statuses = _get_toots(toot_api, toot_obj, search) |
||
| 125 | |||
| 126 | if len(statuses) > 0: |
||
| 127 | newest = None |
||
| 128 | for status in statuses: |
||
| 129 | if newest is None: |
||
| 130 | newest = True |
||
| 131 | # first query ; get the max id |
||
| 132 | search['max_id'] = max_id = status['id'] |
||
| 133 | |||
| 134 | since_id = search['since_id'] = statuses[-1]['id'] - 1 |
||
| 135 | |||
| 136 | count, search, statuses = _get_toots(toot_api, toot_obj, |
||
| 137 | search) |
||
| 138 | |||
| 139 | newest = None |
||
| 140 | if len(statuses) > 0: |
||
| 141 | my_toots = [] |
||
| 142 | for s in statuses: |
||
| 143 | if newest is None: |
||
| 144 | newest = True |
||
| 145 | max_id = s['id'] - 1 |
||
| 146 | toot_name = s['account']['username'] |
||
| 147 | # get the text of the tweet + url to this one |
||
| 148 | if toot_obj.fav: |
||
| 149 | url = '{0}/api/v1/statuses/{1}'.format( |
||
| 150 | self.api_base_url, s['id']) |
||
| 151 | title = _('Toot Fav from @{}'.format(toot_name)) |
||
| 152 | else: |
||
| 153 | url = '{0}/api/v1/accounts/{1}/statuses'.format( |
||
| 154 | self.api_base_url, s['id']) |
||
| 155 | title = _('Toot from @{}'.format(toot_name)) |
||
| 156 | # Wed Aug 29 17:12:58 +0000 2012 |
||
| 157 | my_date = arrow.get(s['created_at'], |
||
| 158 | 'ddd MMM DD HH:mm:ss Z YYYY') |
||
| 159 | published = arrow.get(my_date).to(settings.TIME_ZONE) |
||
| 160 | if date_triggered is not None and \ |
||
| 161 | published is not None and \ |
||
| 162 | now >= published >= date_triggered: |
||
| 163 | my_toots.append({'title': title, |
||
| 164 | 'content': s['content'], |
||
| 165 | 'link': url, |
||
| 166 | 'my_date': my_date}) |
||
| 167 | # digester |
||
| 168 | self.send_digest_event(trigger_id, title, url) |
||
| 169 | cache.set('th_mastodon_' + str(trigger_id), my_toots) |
||
| 170 | Mastodon.objects.filter(trigger_id=trigger_id).update( |
||
| 171 | since_id=since_id, |
||
| 172 | max_id=max_id, |
||
| 173 | count=count) |
||
| 174 | return my_toots |
||
| 175 | |||
| 324 |