使用 Flask 服务器的 Covid 疫苗可用性
在本文中,我们将使用 Flask Server构建Covid 疫苗可用性检查器。
我们都知道整个世界都在遭受大流行病的折磨,唯一能帮助我们摆脱这种局面的就是大规模接种疫苗。但正如我们所知,由于该国人口众多,在我们附近的地区很难找到疫苗。因此,现在技术进入了画面,我们将构建我们自己的 covid 疫苗可用性检查器,以查找我们附近地区的疫苗可用性。
我们将使用一些Python库来查找疫苗的可用性并使用烧瓶服务器显示它们。将来,您还可以将其部署在实时服务器上。首先,我们必须使用以下命令安装Python Flask 包:
pip install flask
安装Python flask 包后,打开 Pycharm 或任何 IDE(最好是 Visual Studio Code)并创建一个新项目。在此之后制作一个主要的Python文件app.py和两个名为templates的文件夹,另一个是静态的(文件夹的名称必须与写入的名称相同)。
下面是表示项目目录结构的图像。
分步实施
第 1 步:在第一步中,我们必须导入我们将在项目中进一步使用的Python库。另外,制作一个 Flask 服务器的应用程序。
Python
from flask import Flask,request,session,render_template
import requests,time
from datetime import datetime,timedelta
app = Flask(__name__)
Python3
@app.route('/')
def home():
# rendering the homepage of project
return render_template("index.html")
@app.route('/CheckSlot')
def check():
# for checking the slot available or not
# fetching pin codes from flask
pin = request.args.get("pincode")
# fetching age from flask
age = request.args.get("age")
data = list()
# finding the slot
result = findSlot(age, pin, data)
if(result == 0):
return render_template("noavailable.html")
return render_template("slot.html", data=data)
Python3
def findSlot(age,pin,data):
flag = 'y'
num_days = 2
actual = datetime.today()
list_format = [actual + timedelta(days=i) for i in range(num_days)]
actual_dates = [i.strftime("%d-%m-%Y") for i in list_format]
while True:
counter = 0
for given_date in actual_dates:
# cowin website Api for fetching the data
URL = "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin?pincode={}&date={}".format(pin, given_date)
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36'}
# Get the results in json format.
result = requests.get(URL,headers = header)
if(result.ok):
response_json = result.json()
if(response_json["centers"]):
# Checking if centres available or not
if(flag.lower() == 'y'):
for center in response_json["centers"]:
# For Storing all the centre and all parameters
for session in center["sessions"]:
# Fetching the availability in particular session
datas = list()
# Adding the pincode of area in list
datas.append(pin)
# Adding the dates available in list
datas.append(given_date)
# Adding the centre name in list
datas.append(center["name"])
# Adding the block name in list
datas.append(center["block_name"])
# Adding the vaccine cost type whether it is
# free or chargable.
datas.append(center["fee_type"])
# Adding the available capacity or amount of
# doses in list
datas.append(session["available_capacity"])
if(session["vaccine"]!=''):
datas.append(session["vaccine"])
counter =counter + 1
# Add the data of particular centre in data list.
if(session["available_capacity"]>0):
data.append(datas)
else:
print("No response")
if counter == 0:
return 0
return 1
Python3
from flask import Flask,request,session,render_template
import requests,time
from datetime import datetime,timedelta
app = Flask(__name__)
@app.route('/')
def home():
return render_template("index.html")
@app.route('/CheckSlot')
def check():
# fetching pin codes from flask
pin = request.args.get("pincode")
# fetching age from flask
age = request.args.get("age")
data = list()
# finding the slot
result = findSlot(age,pin,data)
if(result == 0):
return render_template("noavailable.html")
return render_template("slot.html",data = data)
def findSlot(age,pin,data):
flag = 'y'
num_days = 2
actual = datetime.today()
list_format = [actual + timedelta(days=i) for i in range(num_days)]
actual_dates = [i.strftime("%d-%m-%Y") for i in list_format]
# print(actual_dates)
while True:
counter = 0
for given_date in actual_dates:
# cowin website Api for fetching the data
URL = "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin?pincode={}&date={}".format(pin, given_date)
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36'}
# Get the results in json format.
result = requests.get(URL,headers = header)
if(result.ok):
response_json = result.json()
if(response_json["centers"]):
# Checking if centres available or not
if(flag.lower() == 'y'):
for center in response_json["centers"]:
# For Storing all the centre and all parameters
for session in center["sessions"]:
# Fetching the availability in particular session
datas = list()
# Adding the pincode of area in list
datas.append(pin)
# Adding the dates available in list
datas.append(given_date)
# Adding the centre name in list
datas.append(center["name"])
# Adding the block name in list
datas.append(center["block_name"])
# Adding the vaccine cost type whether it is
# free or chargable.
datas.append(center["fee_type"])
# Adding the available capacity or amount of
# doses in list
datas.append(session["available_capacity"])
if(session["vaccine"]!=''):
datas.append(session["vaccine"])
counter =counter + 1
# Add the data of particular centre in data list.
if(session["available_capacity"]>0):
data.append(datas)
else:
print("No response")
if counter == 0:
return 0
return 1
if __name__ == "__main__":
app.run()
HTML
Covid Vaccination Slots
Find Your Vaccination Slots
Enter Your Credentials here
HTML
Covid Vaccination Slots
Vaccination Slots Availability
Pincode
Date
Vaccination Center Name
BlockName
Price
Available Capacity
Vaccine Type
{% for item in data%}
{{item[0]}}
{{item[1]}}
{{item[2]}}
{{item[3]}}
{{item[4]}}
{{item[5]}}
{{item[6]}}
{% endfor %}
Visit Government Website for Booking Slot
HTML
No Availablity
Sorry !
No Available Vaccine Slots
第 2 步:添加路由以处理客户端请求。
蟒蛇3
@app.route('/')
def home():
# rendering the homepage of project
return render_template("index.html")
@app.route('/CheckSlot')
def check():
# for checking the slot available or not
# fetching pin codes from flask
pin = request.args.get("pincode")
# fetching age from flask
age = request.args.get("age")
data = list()
# finding the slot
result = findSlot(age, pin, data)
if(result == 0):
return render_template("noavailable.html")
return render_template("slot.html", data=data)
第 3 步:这是我们项目的主要步骤,它查找疫苗的可用性(findslot()函数将执行以下操作):
- 首先从Python 的DateTime 库中获取年龄、pin 和查找日期等参数。
- 从 covin 官方网站上获取数据,例如疫苗剂量、疫苗接种中心、可用性、疫苗类型和价格等。
- 将所有数据存储在Python列表中。
蟒蛇3
def findSlot(age,pin,data):
flag = 'y'
num_days = 2
actual = datetime.today()
list_format = [actual + timedelta(days=i) for i in range(num_days)]
actual_dates = [i.strftime("%d-%m-%Y") for i in list_format]
while True:
counter = 0
for given_date in actual_dates:
# cowin website Api for fetching the data
URL = "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin?pincode={}&date={}".format(pin, given_date)
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36'}
# Get the results in json format.
result = requests.get(URL,headers = header)
if(result.ok):
response_json = result.json()
if(response_json["centers"]):
# Checking if centres available or not
if(flag.lower() == 'y'):
for center in response_json["centers"]:
# For Storing all the centre and all parameters
for session in center["sessions"]:
# Fetching the availability in particular session
datas = list()
# Adding the pincode of area in list
datas.append(pin)
# Adding the dates available in list
datas.append(given_date)
# Adding the centre name in list
datas.append(center["name"])
# Adding the block name in list
datas.append(center["block_name"])
# Adding the vaccine cost type whether it is
# free or chargable.
datas.append(center["fee_type"])
# Adding the available capacity or amount of
# doses in list
datas.append(session["available_capacity"])
if(session["vaccine"]!=''):
datas.append(session["vaccine"])
counter =counter + 1
# Add the data of particular centre in data list.
if(session["available_capacity"]>0):
data.append(datas)
else:
print("No response")
if counter == 0:
return 0
return 1
下面是 app.py 的完整实现
蟒蛇3
from flask import Flask,request,session,render_template
import requests,time
from datetime import datetime,timedelta
app = Flask(__name__)
@app.route('/')
def home():
return render_template("index.html")
@app.route('/CheckSlot')
def check():
# fetching pin codes from flask
pin = request.args.get("pincode")
# fetching age from flask
age = request.args.get("age")
data = list()
# finding the slot
result = findSlot(age,pin,data)
if(result == 0):
return render_template("noavailable.html")
return render_template("slot.html",data = data)
def findSlot(age,pin,data):
flag = 'y'
num_days = 2
actual = datetime.today()
list_format = [actual + timedelta(days=i) for i in range(num_days)]
actual_dates = [i.strftime("%d-%m-%Y") for i in list_format]
# print(actual_dates)
while True:
counter = 0
for given_date in actual_dates:
# cowin website Api for fetching the data
URL = "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin?pincode={}&date={}".format(pin, given_date)
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36'}
# Get the results in json format.
result = requests.get(URL,headers = header)
if(result.ok):
response_json = result.json()
if(response_json["centers"]):
# Checking if centres available or not
if(flag.lower() == 'y'):
for center in response_json["centers"]:
# For Storing all the centre and all parameters
for session in center["sessions"]:
# Fetching the availability in particular session
datas = list()
# Adding the pincode of area in list
datas.append(pin)
# Adding the dates available in list
datas.append(given_date)
# Adding the centre name in list
datas.append(center["name"])
# Adding the block name in list
datas.append(center["block_name"])
# Adding the vaccine cost type whether it is
# free or chargable.
datas.append(center["fee_type"])
# Adding the available capacity or amount of
# doses in list
datas.append(session["available_capacity"])
if(session["vaccine"]!=''):
datas.append(session["vaccine"])
counter =counter + 1
# Add the data of particular centre in data list.
if(session["available_capacity"]>0):
data.append(datas)
else:
print("No response")
if counter == 0:
return 0
return 1
if __name__ == "__main__":
app.run()
第 4 步:现在在模板文件夹中创建三个文件。
- index.html:显示项目的主页,用于输入年龄和密码。
- slot.html:在页面上显示数据。
- noavailable.html:如果在任何中心都没有找到疫苗,则 - 无可用性页面。
index.html 代码
HTML
Covid Vaccination Slots
Find Your Vaccination Slots
Enter Your Credentials here
插槽.html
HTML
Covid Vaccination Slots
Vaccination Slots Availability
Pincode
Date
Vaccination Center Name
BlockName
Price
Available Capacity
Vaccine Type
{% for item in data%}
{{item[0]}}
{{item[1]}}
{{item[2]}}
{{item[3]}}
{{item[4]}}
{{item[5]}}
{{item[6]}}
{% endfor %}
Visit Government Website for Booking Slot
不可用.html
HTML
No Availablity
Sorry !
No Available Vaccine Slots
将图像或其他文件(如果有)添加到静态文件夹。