-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
35 lines (28 loc) · 1.17 KB
/
run.py
File metadata and controls
35 lines (28 loc) · 1.17 KB
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
#! /usr/bin/env python3
import os
import requests
PATH = "supplier-data/descriptions/"
URL = "http://localhost/fruits/"
def fruits_upload(url: str, path: str) -> None:
"""
Posts data about fruits to a given url.
:param url: url for uploading the data
:param path: path to directory with text files holding fruit descriptions
"""
for infile in os.listdir(path):
if infile.endswith('.txt'):
fruit = {}
with open(path + infile, "r") as txt_file:
fruit['name'] = txt_file.readline().strip('\n\r')
fruit['weight'] = int(txt_file.readline().strip('\n\r').split(' ')[0])
fruit['description'] = txt_file.readline().strip('\n\r')
# to bind previously uploaded fruit image with fruit description
fruit['image_name'] = infile.replace('.txt', '.jpeg')
print(fruit)
try:
response = requests.post(url, data=fruit)
print(response.status_code)
response.raise_for_status()
except Exception as err:
print("ERROR: ", err)
fruits_upload(URL, PATH)