ProvincieZeeland / cps-connector

CPS connector API
0 stars 0 forks source link

POC ophalen iBabs documenten #17

Open wkosten opened 1 month ago

wkosten commented 1 month ago

POC maken (Python worker) voor ophalen van documenten uit iBabs agenda's op basis van SOAP API

Beschrijving iBabs Public WCF Service-2-1.pdf

https://wcf.ibabs.eu/api/Public.svc https://wcf.ibabs.eu/api/Public.svc?singleWsdl

PHP Drupal code: https://gitlab.com/digitalemedia/drupal_core/-/blob/master/sites/all/modules/custom/zl_deployment/services/class.ibabs_soap.inc?ref_type=heads

  1. GetMeetingsByDateRange(Sitename="zeeland", StartDate="2024-09-27T00:00:00", EndDate="2024-09-27T23:59:00", MetaDataOnly=True)
  2. GetMeeting(Sitename="zeeland", MeetingId='2621b87a-97de-4df6-af52-4b66f19f603f')

Python code voor ophalen meetingtypes:

from zeep import Client
wsdl     = 'https://www.mijnbabs.nl:443/iBabsWCFService/Public.svc?wsdl'
client   = Client(wsdl=wsdl)
response = client.service.GetMeetingtypes(Sitename='zeeland')

En het resultaat:

{
    'Message': None,
    'Status': 'OK',
    'Meetingtypes': {
        'iBabsMeetingtype': [
            {
                'Abbreviation': 'PS',
                'Description': 'Provinciale Staten',
                'Id': '10000013',
                'Meetingtype': 'Provinciale Staten'
            },
            {
                'Abbreviation': 'Cie Bestuur',
                'Description': 'Commissie Bestuur',
                'Id': '10000015',
                'Meetingtype': 'Commissie Bestuur'
            },
            {
                'Abbreviation': 'Cie Economie',
                'Description': 'Commissie Economie',
                'Id': '10000016',
                'Meetingtype': 'Commissie Economie'
            },
            {
                'Abbreviation': 'Cie Ruimte',
                'Description': 'Commissie Ruimte',
                'Id': '10000017',
                'Meetingtype': 'Commissie Ruimte'
            },
            {
                'Abbreviation': 'Cie Gezamenlijk',
                'Description': 'Commissie Gezamenlijk',
                'Id': '10000021',
                'Meetingtype': 'Commissie Gezamenlijk'
            },
            {
                'Abbreviation': 'Cie SO',
                'Description': 'Commissie Strategische Opgaven',
                'Id': '100168493',
                'Meetingtype': 'Commissie Strategische Opgaven'
            }
        ]
    }
}
from zeep import Client
import zlib

wsdl     = 'https://www.mijnbabs.nl:443/iBabsWCFService/Public.svc?wsdl'
client   = Client(wsdl=wsdl)
response = client.service.GetMeetingsByDateRange(Sitename="zeeland", StartDate="2024-07-01T00:00:00", EndDate="2024-07-05T23:59:00", MetaDataOnly=False)

# omzetten response json naar string en CRC32 berekenen
response_str = str(response)
new_crc32_hash = zlib.crc32(response_str.encode())

Meetings met geselecteerde opties

from zeep import Client, xsd

# Define the WSDL URL
wsdl_url = 'https://wcf.iBabs.eu/api/Public.svc?wsdl'

# Create a zeep client
client = Client(wsdl=wsdl_url)

# Define the parameters
sitename = 'Zeeland'
meeting_id = '2621b87a-97de-4df6-af52-4b66f19f603f'

# Define the complex type for the key-value pair
iBabsKeyValue_type = xsd.ComplexType(
    xsd.Sequence([
        xsd.Element('{http://schemas.datacontract.org/2004/07/iBabsWCFObjects}Key', xsd.String()),
        xsd.Element('{http://schemas.datacontract.org/2004/07/iBabsWCFObjects}Value', xsd.Boolean())
    ])
)

# Create the ArrayOfiBabsKeyValue complex type
ArrayOfiBabsKeyValue_type = xsd.ComplexType(
    xsd.Sequence([
        xsd.Element(
            '{http://schemas.datacontract.org/2004/07/iBabsWCFObjects}iBabsKeyValue',
            iBabsKeyValue_type,
            max_occurs='unbounded'
        )
    ])
)

# Construct the options parameter as an array of key-value pairs
options = ArrayOfiBabsKeyValue_type([
    {'Key': 'IncludeDocuments', 'Value': True},  {'Key': 'IncludeMeetingItems', 'Value': True}
])

# Call the GetMeetingWithOptions method
try:
    response = client.service.GetMeetingWithOptions(Sitename=sitename, MeetingId=meeting_id, Options=options)
    print(response)
except Exception as e:
    print(f"An error occurred: {e}")
wkosten commented 1 month ago

Loopen door de response om de documenten op te halen

# Initialize an empty list to store the results
documents_with_parents = []
data = response

# Access 'MeetingItems' safely
meeting = getattr(data, 'Meeting', None)
if meeting:
    meeting_items = getattr(meeting, 'MeetingItems', None)
    if meeting_items:
        iBabs_meeting_items = getattr(meeting_items, 'iBabsMeetingItem', [])

        # Loop through 'iBabsMeetingItem'
        for item in iBabs_meeting_items:
            # Get the parent title and feature
            parent_title = getattr(item, 'Title', None)
            feature = getattr(item, 'Features', None)

            # Get the documents if available
            documents = getattr(getattr(item, 'Documents', None), 'iBabsDocument', [])

            # Loop through each document and append it to the results list
            for document in documents:
                documents_with_parents.append({
                    'ParentTitle': parent_title,
                    'Feature': feature,
                    'Document': document
                })

# Output the result
for entry in documents_with_parents:
    print(f"Parent Title: {entry['ParentTitle']}")
    print(f"Feature: {entry['Feature']}")
    print(f"Document: {entry['Document']}")
    print()  # Print a new line for better readability
wkosten commented 1 month ago

En de code om de agendapunten op te halen en op volgorde te zetten

# Initialize an empty list to store the features
features_list = []
data = response

# Access 'MeetingItems' safely
meeting = getattr(data, 'Meeting', None)
if meeting:
    meeting_items = getattr(meeting, 'MeetingItems', None)
    if meeting_items:
        iBabs_meeting_items = getattr(meeting_items, 'iBabsMeetingItem', [])

        # Loop through 'iBabsMeetingItem'
        for item in iBabs_meeting_items:
            # Get the feature, title, and explanation
            feature = getattr(item, 'Features', None)
            title = getattr(item, 'Title', None)
            explanation = getattr(item, 'Explanation', None)

            # Append to the features list
            if feature is not None:
                features_list.append({
                    'Feature': feature,
                    'Title': title,
                    'Explanation': explanation
                })

# Sort the features list to ensure the parent-child relationship is maintained
features_list.sort(key=lambda x: [int(part) for part in x['Feature'].split('.')])

# Build the parent-child relationship
for feature in features_list:
    feature_number = feature['Feature']
    parts = feature_number.split('.')

    # If it has a minor part, find its parent
    if len(parts) > 1:
        major = parts[0]
        parent_feature_number = major

        # Look for the parent in the list
        parent = next((f for f in features_list if f['Feature'] == parent_feature_number), None)
        if parent:
            feature['Parent'] = parent_feature_number
        else:
            feature['Parent'] = None
    else:
        # No minor part means it is a major feature (top-level)
        feature['Parent'] = None

# Output the result
for feature in features_list:
    print(f"Feature: {feature['Feature']}")
    print(f"Parent: {feature['Parent']}")
    print(f"Title: {feature['Title']}")
    print(f"Explanation: {feature['Explanation']}")
    print()  # Print a new line for better readability