Skip to content

Commit

Permalink
afhankelijkheid naar wget verwijderd
Browse files Browse the repository at this point in the history
  • Loading branch information
ZuinigeRijder committed May 3, 2023
1 parent 28f6ab4 commit 4b4b4a0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 55 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
- [rdwapihulpmiddelen python versie](#rdwapihulpmiddelen-python-versie)
- [rdw.py](#rdwpy)
- [rdw\_utils.py](#rdw_utilspy)

# rdwapihulpmiddelen python versie
Python RDW API hulpmiddelen voor de IONIQ 5, misschien dat het ook gebruikt kan worden ter inspiratie voor andere auto's. Maar kan natuurlijk ook gebruikt worden om nieuwe kentekens te vinden die nog niet op naam staan.
Expand All @@ -11,12 +12,6 @@ Er is 1 python script:

De tools worden gedraaid op Windows 10 en geschikt voor Python 3.9, Python versie die ik gebruik: 3.9.13

Het python script gebruikt wget om data op te halen, wget versie die ik gebruik:
````
wget --version
GNU Wget 1.20.3 built on mingw32.
````

# rdw.py

3 aanroepmogelijkheden:
Expand All @@ -30,6 +25,11 @@ Er zijn 3 input/output bestanden:
- nognietopnaam.txt
- opnaam.txt

Ook wordt er een backup gemaakt van deze files met een datum/tijd in de naam, bijvoorbeeld:
- nognietopnaam.txt.2023.04.21_09.42.25.txt

Wanneer er niets gewijzigd is in de backup file, wordt de backup file weer verwijderd.

Via RDW worden alle IONIQ5 kentekens opgehaald met de metadata in x.kentekens. Wanneer deze niet in exported.txt, nognietopnaam.txt of opnaam.txt voorkomen, wordt deze aan het einde als nieuw kenteken getoond. Ook wordt er getoond wanneer een kenteken van nognietopnaam.txt naar opnaam.txt of naar exported.txt verhuisd is. Aan het eind wordt dan de volgende delta's gerapporteerd bij "python rdw.py" zonder parameters:
- Eerder gevonden kenteken op naam gezet
- Nieuw kenteken op naam gezet
Expand All @@ -38,3 +38,7 @@ Via RDW worden alle IONIQ5 kentekens opgehaald met de metadata in x.kentekens. W

Voorbeelden van uitvoer kun je op tweakers ["Het Hyundai Ioniq 5 leveringen topic"](https://gathering.tweakers.net/forum/list_messages/2073194/2?data%5Bfilter_pins%5D=1) terugvinden.


# rdw_utils.py

Hulp functies, gebruikt door rdw.py, onder andere om de juiste variant te vinden aan de hand van fiscale prijs, kleur en type.
7 changes: 3 additions & 4 deletions rdw.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import sys
from rdw_utils import (
arg_has,
execute_command,
get_kentekens,
fill_prices,
get_variant,
my_die,
Expand Down Expand Up @@ -39,7 +39,7 @@ def rename_with_timestamp(filename: str) -> str:
return new_filename


def delete_second_file_if_content_same(filename1: str, filename2: str) -> str:
def delete_second_file_if_content_same(filename1: str, filename2: str):
"""delete second file if content same"""
if filename2 != "" and filecmp.cmp(filename1, filename2, shallow=False):
print(f"INFO: Deleting {filename2}")
Expand Down Expand Up @@ -216,8 +216,7 @@ def main():
print("Getting IONIQ5 kentekens")
if os.path.exists(xkentekensfilename):
os.remove(xkentekensfilename)
cmd = 'wget --quiet --output-document=x.kentekens "https://opendata.rdw.nl/api/id/m9d7-ebf2.json?$select=*&$order=`:id`+ASC&$limit=8000&$offset=0&$where=(%60handelsbenaming%60%20%3D%20%27IONIQ5%27)&$$read_from_nbe=true&$$version=2.1"' # noqa
execute_command(cmd, D, retry=False, die_on_error=True)
get_kentekens()
print("Processing IONIQ5 kentekens")
with open(xkentekensfilename, encoding="utf8") as json_file:
json_data = json.load(json_file)
Expand Down
78 changes: 33 additions & 45 deletions rdw_utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
"""rdwfinder.py"""
"""rdw_utils.py"""
# pylint:disable=too-many-lines
import re
import subprocess
import socket
import sys
import time
import traceback
from urllib.error import HTTPError, URLError
from urllib.request import Request, urlopen


# ===============================================================================
Expand All @@ -21,49 +24,34 @@ def my_die(txt):
sys.exit(1)


# ===============================================================================
# execute_command
# parameter 1: command
# return errorlevel
# ===============================================================================
def execute_command(command, debug, retry, die_on_error):
"""execute_command"""
# Note that the Python code uses the subprocess module to execute the
# command instead of the system function used in Perl.
# The subprocess.call function is used with the shell=True argument to
# execute the command in a shell.
# The output is redirected to stderr with 2>&1 as in the original
# Perl code.
# The function returns the error code, which is obtained by masking the
# returned value with 0xffff.
if retry:
try_count = 5
else:
try_count = 1
while try_count > 0:
if retry and try_count < 5:
print(f"Retry: {try_count}")
try_count -= 1
if debug:
print(f"Before: {command} 2>&1")
return_code = subprocess.call(f"{command} 2>&1", shell=True)
if return_code == 0:
# everything Ok
return 0
elif return_code == 0xFF00:
print(f"ERROR: Command [{command}] failed: {return_code}")
elif return_code > 0x80:
return_code >>= 8
print(
f"ERROR: Command [{command}] exited with non-zero exit status: {return_code}" # noqa
)
else:
print(
f"ERROR: Command [{command}] exited with signal {return_code}"
) # noqa
if die_on_error:
my_die(f"execute_command failed with {return_code}: [$command]")
return return_code & 0xFFFF
# == get_kentekens ====================================================================
def get_kentekens():
"""get_kentekens and handle errors"""
while True:
url = "https://opendata.rdw.nl/api/id/m9d7-ebf2.json?$select=*&$order=`:id`+ASC&$limit=8000&$offset=0&$where=(%60handelsbenaming%60%20%3D%20%27IONIQ5%27)&$$read_from_nbe=true&$$version=2.1" # noqa
request = Request(url)
errorstring = ""
try:
with urlopen(request, timeout=30) as response:
body = response.read()
content = body.decode("utf-8")
with open("x.kentekens", "x", encoding="utf8") as xkentekensfile:
xkentekensfile.write(content)
return
except HTTPError as error:
errorstring = str(error.status) + ": " + error.reason
except URLError as error:
errorstring = str(error.reason)
except TimeoutError:
errorstring = "Request timed out"
except socket.timeout:
errorstring = "Socket timed out"
except Exception as ex: # pylint: disable=broad-except
errorstring = "urlopen exception: " + str(ex)
traceback.print_exc()

print(f"ERROR (retry after 1 minute): {url} -> {errorstring}")
time.sleep(60) # retry after 1 minute


# ===============================================================================
Expand Down

0 comments on commit 4b4b4a0

Please sign in to comment.