-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgovernmentapi.py
64 lines (47 loc) · 1.77 KB
/
governmentapi.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# -*- coding: utf-8 -*-
"""GovernmentAPI.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1CzqYLjLfddbSZhYBVDfrRIDY5wwTMYkM
"""
import requests
from pprint import pprint
import matplotlib.pyplot as plt
def SearchProject(keyword_search='เรือดำน้ำ',year=2562,limit=20):
url = 'https://opend.data.go.th/govspending/cgdcontract'
#url = 'https://opend.data.go.th/govspending/summary_cgdcontract'
token = 'rcdV5Vub9doETaWI798M0sNUCScd0q0O'
parameters = {'api-key':token,
'year':year,
'keyword':keyword_search,
'limit':limit
}
r = requests.get(url,params=parameters)
result = r.json()
print(result)
allproject = result['result']
#print(type(allproject))
print('Count: ',len(allproject)) # len() คือการนับค่าใน List / Dict
#pprint(allproject[8])
allbudget = []
projectname = []
print('----parameter-----')
print(allproject[0].keys())
print('------------------')
for pj in allproject:
print(pj['dept_name'])
print(pj['sum_price_agree'])
print(pj['project_name'])
print(pj['transaction_date'])
allbudget.append(int(pj['sum_price_agree'].replace(',','')))
projectname.append(pj['project_name'])
print('---------------')
print('***************************************************')
print('ค้นหาคำว่า: {} งบประมาณทั้งหมด: {:,d} บาท'.format(keyword_search,sum(allbudget)))
print('***************************************************')
# graph
# xbar = range(len(projectname))
# plt.bar(xbar,allbudget)
# plt.xticks(projectname)
# plt.show()
SearchProject('หน้ากากอนามัย')