Skip to content

Instantly share code, notes, and snippets.

@Jaakkonen
Created June 27, 2024 21:01
Show Gist options
  • Select an option

  • Save Jaakkonen/f0833db6511094cebe928e7695f06a0f to your computer and use it in GitHub Desktop.

Select an option

Save Jaakkonen/f0833db6511094cebe928e7695f06a0f to your computer and use it in GitHub Desktop.
Extract NoCFO.io attachements
import requests
import os
from pathlib import Path
token = os.environ["NOCFO_TOKEN"]
y_tunnus = os.environ["NOCFO_VAT_CODE"]
s = requests.Session()
s.headers.update({"Authorization": token})
results = []
next_page = 1
while True:
res = s.get(f"https://api-prd.nocfo.io/v1/business/{y_tunnus}/files/?search=&root=true&page={next_page}")
data = res.json()
results.extend(data["results"])
if not data["next"]:
break
next_page = data["next"]
# Get file details
for file in results:
file_id = file['id']
res = s.get(f"https://api-prd.nocfo.io/v1/business/{y_tunnus}/document/?page=1&page_size=10&file={file_id}")
data = res.json()
file['event'] = {
# Name is the name of first credet transaction associated with the file
'name': next((result['description'] for result in data['results'])),
'date': min(result['date'] for result in data['results']),
}
files = {
file['name']: file['file'] # name -> url
for file in results
}
# Download and place to ./nocfo_files
Path("nocfo_files").mkdir(exist_ok=True)
for result in results:
url = result['file']
res = requests.get(url)
transaction_name = result['event']['name']
filename = result['name']
date = result['event']['date']
fullname = f"{date} - {transaction_name} - {filename}".replace("/", "_")
with open(f"nocfo_files/{fullname}", "wb") as f:
f.write(res.content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment