File size: 3,064 Bytes
83892b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""

api_client.py

─────────────

Uses the official SOAP API to get a list of law IDs + metadata.

Think of this as the "index" — it tells us WHAT exists.

The actual law text is then fetched by html_scraper.py.



Install: pip install suds-community

"""

from suds.client import Client
import time

WSDL_URL = "http://legislatie.just.ro/apiws/FreeWebService.svc?wsdl"


class LegislatieAPIClient:
    def __init__(self):
        print("Connecting to SOAP API...")
        self.client = Client(WSDL_URL)
        self.token = self.client.service.GetToken()
        print(f"Got session token: {self.token[:20]}...")

    def search(self, keyword=None, year=None, doc_type=None, page=0, per_page=20):
        """

        Search the legislation database.



        Parameters:

            keyword  : e.g. "concediu medical", "muncă", "taxe"

            year     : e.g. 2003

            doc_type : e.g. "LEGE", "ORDONANTA", "HOTARARE"

            page     : page number (0-indexed)

            per_page : results per page (max ~50)



        Returns a list of law metadata objects with fields like:

            .Id, .Titlu, .DataVigoare, .TipAct, .NumarAct

        """
        model = self.client.factory.create("SearchModel")
        model.NumarPagina = page
        model.RezultatePagina = per_page

        if keyword:  model.SearchText  = keyword
        if year:     model.SearchAn    = str(year)
        if doc_type: model.SearchTipAct = doc_type

        result = self.client.service.Search(model, self.token)

        if not result or not result.Legi:
            return []
        return result.Legi.Lege  # list of law metadata objects

    def get_all_ids(self, keyword=None, year=None, doc_type=None, max_pages=10):
        """

        Paginate through all results and collect every law ID.



        Example:

            ids = client.get_all_ids(keyword="muncă")

            # → [12345, 23456, 34567, ...]

        """
        all_laws = []
        for page in range(max_pages):
            print(f"  Fetching page {page}...")
            batch = self.search(keyword=keyword, year=year, doc_type=doc_type,
                                page=page, per_page=50)
            if not batch:
                print(f"  No more results at page {page}. Done.")
                break
            all_laws.extend(batch)
            time.sleep(0.5)  # be polite to the server

        print(f"Found {len(all_laws)} laws total.")
        return all_laws


# ── Quick test ──────────────────────────────────────────────────────────────
if __name__ == "__main__":
    client = LegislatieAPIClient()

    # Show all available API methods (useful for exploration)
    # print(client.client)

    laws = client.get_all_ids(keyword="concediu medical", max_pages=3)
    for law in laws[:5]:
        print(f"ID={law.Id}  |  {law.Titlu[:80]}")