Passed
Push — master ( 9ef2f9...de54f1 )
by KAMI
04:29
created

osm_poi_matchmaker.dataproviders.hu_benu.hu_benu.process()   C

Complexity

Conditions 10

Size

Total Lines 45
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 41
nop 1
dl 0
loc 45
rs 5.9999
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like osm_poi_matchmaker.dataproviders.hu_benu.hu_benu.process() 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 -*-
2
3
try:
4
    import logging
5
    import sys
6
    import os
7
    import json
8
    from osm_poi_matchmaker.dao.data_handlers import insert_poi_dataframe
9
    from osm_poi_matchmaker.libs.soup import save_downloaded_soup
10
    from osm_poi_matchmaker.libs.address import extract_street_housenumber_better_2, clean_city, clean_phone_to_str, \
11
        PATTERN_FULL_URL
12
    from osm_poi_matchmaker.libs.geo import check_hu_boundary
13
    from osm_poi_matchmaker.libs.osm_tag_sets import POS_HU_GEN, PAY_CASH
14
    from osm_poi_matchmaker.utils.data_provider import DataProvider
15
    from osm_poi_matchmaker.utils.enums import FileType
16
except ImportError as err:
17
    logging.error('Error %s import module: %s', __name__, err)
18
    logging.exception('Exception occurred')
19
20
    sys.exit(128)
21
22
23
class hu_benu(DataProvider):
24
25
    def constains(self):
26
        self.link = 'https://benu.hu/wordpress-core/wp-admin/admin-ajax.php?action=asl_load_stores&nonce=1900018ba1&load_all=1&layout=1'
27
        self.tags = {'brand': 'Benu gyógyszertár', 'dispensing': 'yes',
28
                     'contact:facebook': 'https://www.facebook.com/BENUgyogyszertar',
29
                     'contact:youtube': 'https://www.youtube.com/channel/UCBLjL10QMtRHdkak0h9exqg',
30
                     'air_conditioning': 'yes', }
31
        self.tags.update(POS_HU_GEN)
32
        self.tags.update(PAY_CASH)
33
        self.filetype = FileType.json
34
        self.filename = '{}.{}'.format(
35
            self.__class__.__name__, self.filetype.name)
36
37
    def types(self):
38
        hubenupha = {'amenity': 'pharmacy'}
39
        hubenupha.update(self.tags)
40
        self.__types = [
41
            {'poi_code': 'hubenupha', 'poi_name': 'Benu gyógyszertár', 'poi_type': 'pharmacy',
42
             'poi_tags': hubenupha, 'poi_url_base': 'https://benu.hu',
43
             'poi_search_name': '(benu gyogyszertár|benu)',
44
             'osm_search_distance_perfect': 2000, 'osm_search_distance_safe': 200,
45
             'osm_search_distance_unsafe': 20, 'preserve_original_name': True},
46
        ]
47
        return self.__types
48
49
    def process(self):
50
        try:
51
            soup = save_downloaded_soup('{}'.format(self.link), os.path.join(self.download_cache, self.filename),
52
                                        self.filetype)
53
            if soup is not None:
54
                text = json.loads(str(soup))
55
                for poi_data in text:
56
                    try:
57
                        if 'BENU Gyógyszertár' not in poi_data.get('title'):
58
                            self.data.name = poi_data.get('title').strip()
59
                            self.data.branch = None
60
                        else:
61
                            self.data.name = 'Benu gyógyszertár'
62
                            self.data.branch = poi_data.get('title').strip()
63
                        self.data.code = 'hubenupha'
64
                        if poi_data.get('description') is not None:
65
                            pu_match = PATTERN_FULL_URL.match(
66
                                poi_data.('description'))
0 ignored issues
show
introduced by
invalid syntax (<unknown>, line 66)
Loading history...
67
                            self.data.website = pu_match.group(
68
                                0).strip() if pu_match is not None else None
69
                        else:
70
                            self.data.website = None
71
                        self.data.city = clean_city(poi_data.get('city'))
72
                        self.data.postcode = poi_data.get(
73
                            'postal_code').strip()
74
                        self.data.lat, self.data.lon = check_hu_boundary(
75
                            poi_data.get('lat'), poi_data.get('lng'))
76
                        self.data.street, self.data.housenumber, self.data.conscriptionnumber = extract_street_housenumber_better_2(
77
                            poi_data['street'])
78
                        self.data.original = poi_data['street']
79
                        if 'phone' in poi_data and poi_data['phone'] != '':
80
                            self.data.phone = clean_phone_to_str(
81
                                poi_data['phone'])
82
                        else:
83
                            self.data.phone = None
84
                        self.data.public_holiday_open = False
85
                        self.data.add()
86
                    except Exception as e:
87
                        logging.error(e)
88
                        logging.error(poi_data)
89
                        logging.exception('Exception occurred')
90
91
        except Exception as e:
92
            logging.error(e)
93
            logging.exception('Exception occurred')
94