| Conditions | 12 |
| Total Lines | 57 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 2 |
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 ServiceRss.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 |
||
| 26 | def read_data(self, **kwargs): |
||
| 27 | """ |
||
| 28 | get the data from the service |
||
| 29 | |||
| 30 | :param kwargs: contain keyword args : trigger_id and model name |
||
| 31 | :type kwargs: dict |
||
| 32 | :rtype: dict |
||
| 33 | """ |
||
| 34 | date_triggered = kwargs.get('date_triggered') |
||
| 35 | trigger_id = kwargs.get('trigger_id') |
||
| 36 | kwargs['model_name'] = 'Rss' |
||
| 37 | |||
| 38 | # get the URL from the trigger id |
||
| 39 | rss = super(ServiceRss, self).read_data(**kwargs) |
||
| 40 | |||
| 41 | logger.debug("RSS Feeds from %s : url %s", rss.name, rss.url) |
||
| 42 | |||
| 43 | now = arrow.utcnow().to(settings.TIME_ZONE) |
||
| 44 | published = '' |
||
| 45 | my_feeds = [] |
||
| 46 | |||
| 47 | # retrieve the data |
||
| 48 | feeds = Feeds(**{'url_to_parse': rss.url}).datas() |
||
| 49 | |||
| 50 | for entry in feeds.entries: |
||
| 51 | # entry.*_parsed may be None when the date in a RSS Feed is invalid |
||
| 52 | # so will have the "now" date as default |
||
| 53 | if hasattr(entry, 'published_parsed'): |
||
| 54 | if entry.published_parsed is not None: |
||
| 55 | published = datetime.datetime.utcfromtimestamp( |
||
| 56 | time.mktime(entry.published_parsed)) |
||
| 57 | elif hasattr(entry, 'created_parsed'): |
||
| 58 | if entry.created_parsed is not None: |
||
| 59 | published = datetime.datetime.utcfromtimestamp( |
||
| 60 | time.mktime(entry.created_parsed)) |
||
| 61 | elif hasattr(entry, 'updated_parsed'): |
||
| 62 | if entry.updated_parsed is not None: |
||
| 63 | published = datetime.datetime.utcfromtimestamp( |
||
| 64 | time.mktime(entry.updated_parsed)) |
||
| 65 | |||
| 66 | if published == '': |
||
| 67 | published = now |
||
| 68 | else: |
||
| 69 | published = arrow.get(str(published)).to(settings.TIME_ZONE) |
||
| 70 | |||
| 71 | date_triggered = arrow.get( |
||
| 72 | str(date_triggered)).to(settings.TIME_ZONE) |
||
| 73 | |||
| 74 | if date_triggered is not None and\ |
||
| 75 | published is not None and\ |
||
| 76 | now >= published >= date_triggered: |
||
| 77 | my_feeds.append(entry) |
||
| 78 | |||
| 79 | cache.set('th_rss_' + str(trigger_id), my_feeds) |
||
| 80 | cache.set('th_rss_uuid_{}'.format(rss.uuid), my_feeds) |
||
| 81 | # return the data |
||
| 82 | return my_feeds |
||
| 83 |