Total Complexity | 10 |
Total Lines | 40 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | # -*- coding: utf-8 -*- |
||
|
|||
2 | |||
3 | try: |
||
4 | import logging |
||
5 | import sys |
||
6 | import requests |
||
7 | import os |
||
8 | from lxml import etree |
||
9 | from osm_poi_matchmaker.utils import config |
||
10 | except ImportError as err: |
||
11 | logging.error('Error %s import module: %s', __name__, err) |
||
12 | logging.exception('Exception occurred') |
||
13 | |||
14 | sys.exit(128) |
||
15 | |||
16 | |||
17 | def download_xml(link, verify_link=config.get_download_verify_link()): |
||
18 | try: |
||
19 | page = requests.get(link, verify=verify_link) |
||
20 | except requests.exceptions.ConnectionError as e: |
||
21 | logging.warning('Unable to open connection.') |
||
22 | return None |
||
23 | return page.content if page.status_code == 200 else None |
||
24 | |||
25 | |||
26 | def save_downloaded_xml(link, file, verify=config.get_download_verify_link()): |
||
27 | if config.get_download_use_cached_data() == True and os.path.isfile(file): |
||
28 | with open(file, 'r', encoding='utf-8') as content_file: |
||
29 | page = content_file.read() |
||
30 | else: |
||
31 | page = download_xml(link, verify) |
||
32 | if page is not None: |
||
33 | if not os.path.exists(config.get_directory_cache_url()): |
||
34 | os.makedirs(config.get_directory_cache_url()) |
||
35 | with open(file, mode='w', encoding='utf-8') as code: |
||
36 | code.write(page.decode('utf-8')) |
||
37 | else: |
||
38 | logging.warning('Skipping dataset.') |
||
39 | return page |
||
40 |