Skip to content

Commit

Permalink
Fixed media_follow_changes script.
Browse files Browse the repository at this point in the history
  • Loading branch information
mihxil committed Nov 9, 2023
1 parent c5e7edd commit 8f70914
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
9 changes: 5 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ dependencies = [
#"xsdata[cli]==23.8",
"isoduration==20.11.0",
"lxml==4.9.3",
"json-stream==2.3.2",
]

requires-python = ">=3.11"
readme = "README.txt"
license = {text = "MIT"}
Expand Down Expand Up @@ -49,7 +50,7 @@ npo_pages_list = "npoapi.bin:pages_list"

npo_pages_search= "npoapi.bin:pages_search"
npo_pages_iterate= "npoapi.bin:pages_iterate"

npo_schedule_get="npoapi.bin:schedule_get"
npo_schedule_search="npoapi.bin:schedule_search"
npo_check_credentials="npoapi.bin:check_credentials"
Expand All @@ -60,5 +61,5 @@ npo_thesaurus="npoapi.bin:thesaurus"
npo_subtitles="npoapi.bin:subtitles"
#npo_integration_tests'

[tool.hatch.metadata]
allow-direct-references = true
[tool.hatch.metadata]
allow-direct-references = true
33 changes: 26 additions & 7 deletions src/npoapi/bin/npo_media_follow_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
"""
Simple client to get the changes feed from the NPO Frontend API
"""
import json
import time
from datetime import datetime

import json_stream

from npoapi import Media
from io import TextIOWrapper
from sys import stdout
Expand All @@ -11,28 +17,41 @@ def media_follow_changes():
client = Media().command_line_client("Get changes feed from the NPO Frontend API", exclude_arguments={"accept"})
client.add_argument('profile', type=str, nargs='?', help='Profile')
client.add_argument("-s", "--since", type=str, default=None)
client.add_argument("--sleep", type=int, default=5)
client.add_argument("--deletes", type=str, default="ID_ONLY")
client.add_argument('-p', "--properties", type=str, default=None,
help="properties filtering")

args = client.parse_args()

since=""
since = args.since
if since is None:
since = datetime.now().isoformat()
client.logger.info("No since given, using %s" % since)

while True:
client.logger.info("since: %s" % since)
response = TextIOWrapper(client.changes_raw(
profile=args.profile,
since=args.since,
since=since,
properties=args.properties,
deletes=args.deletes,
stream=True), encoding="UTF-8")

while True:
buffer = response.read(1024)
if len(buffer) == 0:
break
stdout.write(buffer)
data = json_stream.load(response)
changes = data['changes']
for change in changes.persistent():
c = json_stream.to_standard_types(change)
since = str(c['publishDate'])
stdout.write(json.dumps(c))
stdout.write("\n")
stdout.flush()

response.close()
time.sleep(args.sleep)

client.exit()


if __name__ == "__main__":
media_follow_changes()

0 comments on commit 8f70914

Please sign in to comment.