Browse Source

port services code from co-data.

stable
Craig Oates 4 years ago
parent
commit
38189510f7
  1. 29
      app/main.py
  2. 91
      app/services/art_services.py
  3. 7
      app/services/data_services.py
  4. 6
      requirements.txt

29
app/main.py

@ -0,0 +1,29 @@
#!/usr/bin/python3
import requests
from datetime import datetime
from services import art_services, data_services
def main():
raw_art_data = data_services.get_json("https://api.craigoates.net/api/1.0/Artwork")
cd_totals = art_services.get_creation_date_totals(raw_art_data)
print(cd_totals)
cat_totals = art_services.get_db_column_totals(raw_art_data, "category")
print(cat_totals)
med_totals = art_services.get_db_column_totals(raw_art_data, "medium")
print(med_totals)
width_totals = art_services.get_dimension_totals(raw_art_data, "dimensions", "width")
print(width_totals)
height_totals = art_services.get_dimension_totals(raw_art_data, "dimensions", "height")
print(height_totals)
depth_totals = art_services.get_dimension_totals(raw_art_data, "dimensions", "depth")
print(depth_totals)
# Use for console priting for the moment.
date = datetime.now().strftime('%d/%m/%Y')
time = datetime.now().strftime("%H:%M")
return 0
if __name__ == "__main__":
main()

91
app/services/art_services.py

@ -0,0 +1,91 @@
from datetime import datetime
'''
Note: Hard-Coding "Months" and "Days" Sets
======================================================================
I have hardcoded the "months" and "days" sets because they are
fixed values -- unless something monumental happens scientifically
or politically. On top of that, this makes the graphs easier to
read because they are in chronological order. This is not
guaranteed if the "keys" for "months" and "sets" are formed from
the data-object this function receives.
Unfortunately, I cannot do the same for years. That will continue
to grow as the years roll on through here -- unless something
monumental happens scientifically or politically.
'''
def get_creation_date_totals(data):
years = {}
months = {"1": 0, "2": 0, "3": 0, "4": 0, "5": 0, "6": 0,
"7": 0, "8": 0, "9": 0,"10": 0, "11": 0, "12": 0}
days = {"1": 0, "2": 0, "3": 0, "4": 0, "5": 0, "6": 0,
"7": 0, "8": 0, "9": 0,"10": 0, "11": 0, "12": 0,
"13": 0, "14": 0, "15": 0, "16": 0, "17": 0, "18": 0,
"19": 0, "20": 0, "21": 0,"22": 0, "23": 0, "24": 0,
"25": 0, "26": 0, "27": 0, "28": 0, "29": 0, "30": 0,
"31": 0}
for item in data:
ft = datetime.fromisoformat(item["dateCreated"])
if str(ft.year) in years:
years[str(ft.year)] += 1
else:
years[str(ft.year)] = 1
if str(ft.month) in months:
months[str(ft.month)] += 1
else:
months[str(ft.month)] = 1
if str(ft.day) in days:
days[str(ft.day)] += 1
else:
days[str(ft.day)] = 1
return [years, months, days]
def get_category_totals(data):
categories = {}
for item in data:
cat = item["category"]
'''
The join and split is because the data returned from the A.P.I.
call contains a lot of white spaces. This just cleans it up.
The white space was, also, making the chart render incorrectly.
'''
cat = ''.join(cat.split())
if cat in categories:
total = categories.get(cat)
categories[cat] = total + 1
else:
categories[cat] = 1
return categories
def get_db_column_totals(data, column_name):
column_data = {}
for item in data:
col = item[column_name]
col = " ".join(col.split())
# print(col)
if col in column_data:
total = column_data.get(col)
column_data[col] = total + 1
else:
column_data[col] = 1
return column_data
def get_dimension_totals(data, column_name, dimension):
dimensions = {}
for item in data:
dim = item[column_name]
distance = dim[dimension]["value"]["distance"]
if distance is not None:
w = str(distance)
if w not in dimensions:
dimensions[str(distance)] = 1
else:
total = dimensions.get(str(distance))
dimensions[str(distance)] = total + 1
return dimensions

7
app/services/data_services.py

@ -0,0 +1,7 @@
import requests
def get_data(url):
return requests.get(url)
def get_json(url):
return requests.get(url).json()

6
requirements.txt

@ -0,0 +1,6 @@
certifi==2020.4.5.1
chardet==3.0.4
idna==2.9
pkg-resources==0.0.0
requests==2.23.0
urllib3==1.25.9