|
| 1 | +############################################################################### |
| 2 | +# Copyright (C) 2021 Eric Craw, KF7EEL <[email protected]> |
| 3 | +# |
| 4 | +# This program is free software; you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU General Public License as published by |
| 6 | +# the Free Software Foundation; either version 3 of the License, or |
| 7 | +# (at your option) any later version. |
| 8 | +# |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with this program; if not, write to the Free Software Foundation, |
| 16 | +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | +############################################################################### |
| 18 | + |
| 19 | +''' |
| 20 | +This is an example application that utilizes the JSON API. |
| 21 | +It is an information directory application. |
| 22 | +''' |
| 23 | + |
| 24 | +from flask import Flask, request, Markup, jsonify, make_response |
| 25 | +import json, requests, time |
| 26 | +from config import * |
| 27 | +import os, ast |
| 28 | + |
| 29 | +app = Flask(__name__) |
| 30 | + |
| 31 | +def respond_request(dest_id, message, token, url): |
| 32 | + url = url + '/app' |
| 33 | + print(url) |
| 34 | + app_response = { |
| 35 | + 'mode':'app', |
| 36 | + 'app_name':app_name, |
| 37 | + 'app_shortcut':app_shortcut, |
| 38 | + 'auth_token':str(token), |
| 39 | + 'data':{ |
| 40 | + 1:{ 'destination_id':int(dest_id), |
| 41 | + 'slot':0, |
| 42 | + 'msg_type':'unit', |
| 43 | + 'msg_format':'motorola', |
| 44 | + 'message':message |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + json_object = json.dumps(app_response, indent = 4) |
| 49 | + print(json_object) |
| 50 | + requests.post(url, data=json_object, headers={'Content-Type': 'application/json'}) |
| 51 | + |
| 52 | +@app.route('/', methods=['GET']) |
| 53 | +def view(): |
| 54 | + q_data = ast.literal_eval(os.popen('cat ./queries.txt').read()) |
| 55 | + print(q_data) |
| 56 | + q_list = '' |
| 57 | + for i in q_data: |
| 58 | + q_list = q_list + ''' <tr> |
| 59 | + <td style="text-align: center;">''' + i + '''</td> |
| 60 | + </tr>''' + '\n' |
| 61 | + print(i) |
| 62 | + header = ''' |
| 63 | + <!DOCTYPE html> |
| 64 | + <html lang="en"> |
| 65 | + <head> |
| 66 | + <meta charset="UTF-8"> |
| 67 | + <title>''' + app_name + '''</title> |
| 68 | + </head> |
| 69 | + <p><img style="display: block; margin-left: auto; margin-right: auto;" src="''' + app_logo + '''" alt="Logo" width="300" height="144" /></p> |
| 70 | + <h1 style="text-align: center;">''' + app_name + '''</h1> |
| 71 | + <table style="width: 200px; margin-left: auto; margin-right: auto;" border="1"> |
| 72 | + <tbody> |
| 73 | + <tr> |
| 74 | + <td style="text-align: center;"> |
| 75 | + <h2>Available Queries</h2> |
| 76 | + </td> |
| 77 | + </tr> |
| 78 | + |
| 79 | +
|
| 80 | + ''' |
| 81 | + footer = ''' |
| 82 | + </tbody> |
| 83 | + </table> |
| 84 | + <p> </p> |
| 85 | + <div style="text-align: center;">Information Directory created by KF7EEL - <a href="https://kf7eel.github.io/hblink3/">https://github.com/kf7eel/hblink3</a></div> |
| 86 | + </html> |
| 87 | + ''' |
| 88 | + |
| 89 | + return header + q_list + footer |
| 90 | + |
| 91 | + |
| 92 | +@app.route('/query', methods=['POST']) |
| 93 | +def api(api_mode=None): |
| 94 | + q_data = ast.literal_eval(os.popen('cat ./queries.txt').read()) |
| 95 | + api_data = request.json |
| 96 | + #radio_id_get = response = requests.get("https://radioid.net/api/dmr/user/?id=" + str(api_data['data']['source_id'])) |
| 97 | + print(api_data['data']['message']) |
| 98 | + #print(radio_id_get.json()) |
| 99 | + #radio_id_result = radio_id_get.json() |
| 100 | + #print(radio_id_result['results'][0]['callsign']) |
| 101 | + #from_callsign = radio_id_result['results'][0]['callsign'] |
| 102 | + #name = radio_id_result['results'][0]['fname'] |
| 103 | + if api_data['mode'] == 'app': |
| 104 | + try: |
| 105 | + respond_request(api_data['data']['source_id'], q_data[api_data['data']['message']], api_data['auth_token'],api_data['response_url']) |
| 106 | + return jsonify( |
| 107 | + mode=api_data['mode'], |
| 108 | + status='Response Sent', |
| 109 | + ) |
| 110 | + except: |
| 111 | + respond_request(api_data['data']['source_id'], 'Error with query', api_data['auth_token'],api_data['response_url']) |
| 112 | + return jsonify( |
| 113 | + mode=api_data['mode'], |
| 114 | + status='Response Sent', |
| 115 | + ) |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == '__main__': |
| 120 | + global display_list, app_name |
| 121 | + display_list = [] |
| 122 | + app.run(debug = True, port=app_port, host=app_host) |
0 commit comments