윤영준 윤영준 2023-09-01
building apis
updating weather info on the fly and updates regularly, as well as the predictions, and also api that gives warning accordingly
@da011f0d1caf009cb6d7a8207e9868f787c61e2f
 
action.py (added)
+++ action.py
@@ -0,0 +1,34 @@
+import sched
+
+from flask_restx import Resource, Api, Namespace, fields
+from flask import request
+from flask import Flask, render_template, request
+import pandas as pd
+
+Action = Namespace(
+    name="Action",
+    description="노드 분석을 위해 사용하는 api.",
+)
+# @sched.scheduler
+# def weather_update
+
[email protected]('/forecast')
+class forecast(Resource):
+    @Action.doc(responses={200: 'Success'})
+    @Action.doc(responses={500: 'Register Failed'})
+    def post(self):
+        if request.method == 'GET':
+            df = pd.read_csv("data/weather/weather_data_forecast.csv")
+            humidity = df['forecast'].value[6:] / 6
+            if humidity > 90:
+                return {
+                    'report': "warn"
+                }, 200
+            elif humidity <= 90 and humidity > 80:
+                return {
+                    'report': "caution"
+                }, 200
+            else:
+                return {
+                    'report': "safe"
+                }, 200(파일 끝에 줄바꿈 문자 없음)
app.py
--- app.py
+++ app.py
@@ -2,6 +2,10 @@
 from flask_restx import Api
 from auth import Auth
 from action import Action
+from apscheduler.schedulers.background import BackgroundScheduler
+from apscheduler.triggers.interval import IntervalTrigger
+from tools.weather_agency_api.weather_api import update_weather_info_to_today
+from tools.algo.SARIMA import sarima
 
 app = Flask(__name__)
 
@@ -14,11 +18,41 @@
     contact="[email protected]",
     license="MIT")
 
+
+scheduler = BackgroundScheduler()
+scheduler.start()
+
+# Schedule task_function to be called every 6 hours
+scheduler.add_job(
+    func=update_weather_info_to_today,
+    trigger=IntervalTrigger(hours=6),
+    args=("data/weather/weather_data.csv",),
+    # comma to make it a tuple, so that python won't confuse this as a list of char
+    id='weather_data_update',
+    name='update weather time every 6 hours',
+    replace_existing=True
+)
+scheduler.add_job(
+    func=sarima,
+    trigger=IntervalTrigger(hours=6),
+    args=("data/weather/weather_data.csv",),
+    # comma to make it a tuple, so that python won't confuse this as a list of char
+    id='weather_data_update',
+    name='update weather time every 6 hours',
+    replace_existing=True
+)
+
+api.add_namespace(Action, '/action')
+update_weather_info_to_today("data/weather/weather_data.csv")
+sarima("data/weather/weather_data.csv")
+
 api.add_namespace(Auth, '/auth')
 print("Api Add Auth")
 
-api.add_namespace(Action, '/action')
 
 if __name__ == "__main__":
-    app.run(debug=False, host='0.0.0.0', port=8080)
+    try:
+        app.run(debug=False, host='0.0.0.0', port=8080)
+    except:
+        scheduler.shutdown()
     print("Flask Start")
(파일 끝에 줄바꿈 문자 없음)
 
auth.py (added)
+++ auth.py
@@ -0,0 +1,114 @@
+import hashlib
+from flask import request,jsonify,render_template,redirect,url_for
+from flask_restx import Resource, Api, Namespace, fields
+from database.database import DB
+import datetime
+import jwt
+
+
+
+
+
+users = {}
+
+Auth = Namespace(
+    name="Auth",
+    description="사용자 인증을 위한 API",
+)
+
+
+user_fields = Auth.model('User', {  # Model 객체 생성
+    'id': fields.String(description='a User Name', required=True, example="id")
+})
+
+
+user_fields_auth = Auth.inherit('User Auth', user_fields, {
+    'password': fields.String(description='Password', required=True)
+
+})
+
+user_fields_register = Auth.inherit('User reigster', user_fields, {
+    'password': fields.String(description='Password', required=True),'email': fields.String(description='email', required=True),'user_sex': fields.String(description='sex', required=True),'phone': fields.String(description='phone', required=True)
+
+})
+
+
+
[email protected]('/id')
+class AuthCheck(Resource):
+    @Auth.doc(responses={200: 'Success'})
+    @Auth.doc(responses={500: 'Register Failed'})
+    def post(self):
+        db=DB()
+        id = request.json['id']
+        value=db.db_check_id(id)
+        if value != None:
+            return {
+                "message": "중복 아이디가 있습니다"
+            }, 500
+        else:
+            return {
+                'message': '사용가능한 아이디입니다'  # str으로 반환하여 return
+            }, 200
+
+
+
+
[email protected]('/register')
+class AuthRegister(Resource):
+    @Auth.expect(user_fields_register)
+    @Auth.doc(responses={200: 'Success'})
+    @Auth.doc(responses={500: 'Register Failed'})
+    def post(self):
+        db=DB()
+        id = request.json['id']
+        password = request.json['password']
+        user_email = request.json['email']
+        sex = request.json['user_sex']
+        phone = request.json['phone']
+        pw_has = hashlib.sha256(password.encode('utf-8')).hexdigest()
+        value=db.db_login(id,password)
+        if value != None:
+            return {
+                "message": "Register Failed"
+            }, 500
+        else:
+            db.db_add_id(id,pw_has,user_email,sex,phone)
+            return {
+                'Authorization': id  # str으로 반환하여 return
+            }, 200
+
[email protected]('/login')
+class AuthLogin(Resource):
+    @Auth.expect(user_fields_auth)
+    @Auth.doc(responses={200: 'Success'})
+    @Auth.doc(responses={404: 'User Not Found'})
+    @Auth.doc(responses={500: 'Auth Failed'})
+    def post(self):
+        db=DB()
+        id = request.json['id']
+        password = request.json['password']
+        pw_hash = hashlib.sha256(password.encode('utf-8')).hexdigest()
+        result = db.db_login(id,pw_hash)
+        if result is not None:
+            payload = {
+                'id' : id,
+                'exp' : datetime.datetime.utcnow() + datetime.timedelta(seconds=70)
+            }
+            token = jwt.encode(payload, "secret", algorithm='HS256')
+            return jsonify({'result': 'success', 'token': token})
+        else:
+            return jsonify({'result': 'fail', 'msg': '아이디/비밀번호가 일치하지 않습니다.'})
+
+
[email protected]('/secession')
+class AuthSecession(Resource):
+    def post(self):
+         db=DB()
+         id = request.json['token']
+         payload = jwt.decode(id, "secret", algorithms=['HS256'])
+         db.db_delete_id(payload['id'])
+         return {'secession':'success'}
+
+
+
 
data/weather/weather_data.csv (added)
+++ data/weather/weather_data.csv
This file is too big to display.
 
database/database.py (added)
+++ database/database.py
This diff is skipped because there are too many other diffs.
tools/algo/SARIMA.py (Renamed from tools/algo/ARIMA.py)
--- tools/algo/ARIMA.py
+++ tools/algo/SARIMA.py
This diff is skipped because there are too many other diffs.
 
tools/weather_agency_api/check_missing.py (added)
+++ tools/weather_agency_api/check_missing.py
This diff is skipped because there are too many other diffs.
 
tools/weather_agency_api/duplicate_check.py (added)
+++ tools/weather_agency_api/duplicate_check.py
This diff is skipped because there are too many other diffs.
tools/weather_agency_api/weather_api.py
--- tools/weather_agency_api/weather_api.py
+++ tools/weather_agency_api/weather_api.py
This diff is skipped because there are too many other diffs.
Add a comment
List