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