Last active
June 4, 2020 20:51
-
-
Save ansakoy/f2493ca208b0493f8792 to your computer and use it in GitHub Desktop.
clearspending API - arbidol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import requests | |
import csv | |
import time | |
csv_keys = ['region_code', 'year', 'summ_price', 'total_items', 'ave_price', 'total_contracts'] | |
def extract_values(lst, year, region_code): | |
api = 'http://openapi.clearspending.ru/restapi/v3/contracts/search/?productsearch=арбидол&fz=44&daterange=01.01.{}-31.12.{}&customerregion={:0>2}'.format(year, year, region_code) | |
#api = '{}{:0>2}'.format(base, region_code) | |
raw = requests.get(api) | |
try: | |
data = raw.json() | |
num_of_contracts = data[u'contracts'][u'total'] | |
num_of_pages = num_of_contracts // 50 + (num_of_contracts % 50 != 0) | |
total_items = 0 | |
total_sum = 0 | |
for page in xrange(1, num_of_pages + 1): | |
page_url = '{}&page={}'.format(api, page) | |
data = requests.get(page_url).json() | |
length = len(data[u'contracts'][u'data']) | |
for index in range(length): | |
contract = data[u'contracts'][u'data'][index] | |
products = contract[u'products'] | |
for product in products: | |
if u'арбидол' in product[u'name'] or u'Арбидол' in product[u'name'] or u'АРБИДОЛ' in product[u'name']: | |
quantity = float(product.get(u'quantity', 0)) | |
item_price = float(product.get(u'price', 1)) | |
sum_price = float(product.get(u'sum', 0)) | |
if quantity == 0 and sum_price != 0 and item_price > 1: | |
quantity = sum_price // item_price | |
elif sum_price == 0 and quantity != 0 and item_price > 1: | |
sum_price = item_price * quantity | |
try: | |
okei = product[u'OKEI'][u'code'] | |
if okei == u'782': | |
quantity *= 1000 | |
except: | |
print 'No OKEI' | |
try: | |
total_items += quantity | |
except: | |
print 'error:', Exception | |
print 'regnum:', contract[u'regNum'] | |
try: | |
total_sum += sum_price | |
except: | |
print 'error:', Exception | |
print 'regnum:', contract[u'regNum'] | |
average = round(total_sum // total_items, 3) | |
contract_dict = dict() | |
contract_dict['region_code'] = region_code | |
contract_dict['year'] = year | |
contract_dict['summ_price'] = round(total_sum, 3) | |
contract_dict['total_items'] = int(total_items) | |
contract_dict['ave_price'] = average | |
contract_dict['total_contracts'] = num_of_contracts | |
lst.append(contract_dict) | |
return lst | |
except Exception as error: | |
print 'Something went wrong:', 'error:', error, 'code:', region_code, 'status code:', raw.status_code | |
start = time.time() | |
for chronos in xrange(2012, 2016): | |
arbidol = list() | |
for code in xrange(1, 100): | |
try: | |
extract_values(arbidol, chronos, code) | |
except Exception as er: | |
print 'another error:', er | |
with open('arbidol_total{}.csv'.format(chronos), 'wb') as f: | |
writer = csv.DictWriter(f, fieldnames = csv_keys) | |
writer.writeheader() | |
for item in arbidol: | |
writer.writerow(item) | |
stop = time.time() | |
print 'Working time:', stop - start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment