|
| 1 | +from hashlib import md5 |
| 2 | +from dataflows import Flow, printer |
| 3 | +import requests |
| 4 | +from pyquery import PyQuery as pq |
| 5 | + |
| 6 | +s = requests.session() |
| 7 | + |
| 8 | +BASE = { |
| 9 | + 'publisher': 'משרד העבודה, הרווחה והשירותים החברתיים', |
| 10 | + 'tender_type': 'call_for_bids', |
| 11 | + 'publication_id': None, |
| 12 | +} |
| 13 | + |
| 14 | +headers = [ |
| 15 | + ('page_title', 'documents'), |
| 16 | + ('target_audience', ), |
| 17 | + ('start_date', ), |
| 18 | + ('claim_date', ), |
| 19 | + ('_', ), |
| 20 | + ('decision', ), |
| 21 | + ('_', 'page_url') |
| 22 | +] |
| 23 | + |
| 24 | +details_headers = [ |
| 25 | + ('reason', '#ctl00_PlaceHolderMain_lblPubAppealSubject'), |
| 26 | + ('publishing_unit', '#ctl00_PlaceHolderMain_lblPubAppealPublisherFactor'), |
| 27 | + ('ordering_unit', '#ctl00_PlaceHolderMain_lblPubAppealOrderFactor'), |
| 28 | + ('ordering_units', '#ctl00_PlaceHolderMain_lblPubAppealOrderFactor li'), |
| 29 | + ('description', '#ctl00_PlaceHolderMain_lblPubAppealSummary'), |
| 30 | + ('contact', '#ctl00_PlaceHolderMain_lblPubAppealHowToAppeal'), |
| 31 | + ('contact_email', '#ctl00_PlaceHolderMain_lblPubAppealHowToAppeal a'), |
| 32 | + ('required_documents', '#ctl00_PlaceHolderMain_lblPubAppealRequiredDocuments li'), |
| 33 | + ('partners', '#ctl00_PlaceHolderMain_lblPubAppealMembers'), |
| 34 | +] |
| 35 | + |
| 36 | + |
| 37 | +def fetch_calls(): |
| 38 | + URL = 'https://www.molsa.gov.il/Publications/Pages/PubAppealNewSearch.aspx' |
| 39 | + |
| 40 | + catalog = pq(s.get(URL).text) |
| 41 | + for row in catalog.find('.ms-listviewtable tr'): |
| 42 | + ret = {} |
| 43 | + ret.update(BASE) |
| 44 | + ret.update(dict( |
| 45 | + (k, None) |
| 46 | + for k, *_ in details_headers |
| 47 | + )) |
| 48 | + cells = pq(row).find('td') |
| 49 | + if len(cells) == len(headers): |
| 50 | + for header, cell in zip(headers, cells): |
| 51 | + cell, main, *anchor = pq(cell), *header |
| 52 | + ret[main] = cell.text() |
| 53 | + if len(anchor) > 0: |
| 54 | + a = cell.find('a') |
| 55 | + if len(a) > 0: |
| 56 | + href = pq(a).attr('href') |
| 57 | + if href.startswith('/'): |
| 58 | + href = 'https://www.molsa.gov.il{}'.format(href) |
| 59 | + ret[anchor[0]] = href |
| 60 | + yield ret |
| 61 | + |
| 62 | + |
| 63 | +def call_details(): |
| 64 | + def func(row): |
| 65 | + details = pq(s.get(row['page_url']).text) |
| 66 | + for key, selector, *_ in details_headers: |
| 67 | + if selector.endswith('li'): |
| 68 | + row[key] = [pq(x).text() for x in details.find(selector)] |
| 69 | + else: |
| 70 | + row[key] = details.find(selector).text() |
| 71 | + return func |
| 72 | + |
| 73 | + |
| 74 | +def resolve_ordering_unit(): |
| 75 | + def func(row): |
| 76 | + if row.get('ordering_unit') and not row.get('ordering_units'): |
| 77 | + row['ordering_units'] = [row['ordering_unit']] |
| 78 | + row['ordering_unit'] = None |
| 79 | + return func |
| 80 | + |
| 81 | + |
| 82 | +def fix_documents(): |
| 83 | + def func(row): |
| 84 | + href = row['documents'] |
| 85 | + title = row['page_title'] |
| 86 | + update_date = row['start_date'] |
| 87 | + row['documents'] = [ |
| 88 | + dict( |
| 89 | + description=title, |
| 90 | + link=href, |
| 91 | + update_date=update_date |
| 92 | + ) |
| 93 | + ] |
| 94 | + return func |
| 95 | + |
| 96 | + |
| 97 | +def calculate_publication_id(): |
| 98 | + def func(row): |
| 99 | + title_hash = int.from_bytes( |
| 100 | + md5( |
| 101 | + (row['publisher'] + row['page_title']).encode('utf8') |
| 102 | + ).digest()[:4], |
| 103 | + 'big' |
| 104 | + ) |
| 105 | + mod = 1000000000 |
| 106 | + title_hash = mod + (title_hash % mod) |
| 107 | + row['publication_id'] = title_hash |
| 108 | + return func |
| 109 | + |
| 110 | + |
| 111 | +def flow(*args): |
| 112 | + return Flow( |
| 113 | + fetch_calls(), |
| 114 | + call_details(), |
| 115 | + resolve_ordering_unit(), |
| 116 | + fix_documents(), |
| 117 | + calculate_publication_id(), |
| 118 | + printer() |
| 119 | + ) |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == '__main__': |
| 123 | + flow().process() |
0 commit comments