Skip to content

Commit

Permalink
Merge pull request #69 from sousa-dev/dev
Browse files Browse the repository at this point in the history
GMAP calls from API
  • Loading branch information
hc-sousa authored Feb 4, 2024
2 parents b6a9b80 + ba5dd18 commit 385e841
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ __pycache__/
*.py[cod]
*$py.class

.DS_Store

# C extensions
*.so

Expand Down
11 changes: 10 additions & 1 deletion src/SaoMiguelBus/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@

import os

import environ

env = environ.Env()
# Reading .env file
environ.Env.read_env()

GOOGLE_MAPS_API_KEY = env('GOOGLE_MAPS_API_KEY')
AUTH_KEY = env('AUTH_KEY')

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, "static")
Expand All @@ -23,7 +32,7 @@
SECRET_KEY = 'hjq2808rur(19m(zf$3ahcxi=!r74qdvkard7a4yc32j3^jwss'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
DEBUG = True

ALLOWED_HOSTS = ['saomiguelbus-api.herokuapp.com', '127.0.0.1', 'sousa-dev.github.io']

Expand Down
1 change: 1 addition & 0 deletions src/SaoMiguelBus/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@
path('api/v1/info/active', views.get_active_infos_v1),
path('api/v1/stats/group', views.get_group_stats_v1),
path('api/v1/infos', views.get_infos_v1),
path('api/v1/gmaps', views.get_gmaps_v1),
path('statistics', views.stats),
]
56 changes: 55 additions & 1 deletion src/app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from difflib import SequenceMatcher
from django.shortcuts import render
from django.utils import timezone
from SaoMiguelBus import settings
from numpy import full
from rest_framework.decorators import api_view
from rest_framework.response import Response
Expand All @@ -10,7 +11,9 @@
from django.views.decorators.http import require_GET, require_POST
from datetime import datetime, date, timedelta
from statistics import median
import json
import requests
from django.http import JsonResponse
import pytz

#Get All Stops
@api_view(['GET'])
Expand Down Expand Up @@ -73,6 +76,57 @@ def get_trip_v1(request):
except Exception as e:
print(e)
return Response(status=404)

@api_view(['GET'])
@require_GET
def get_gmaps_v1(request):
# If can use maps is true
# variable = Variables.objects.all().first().__dict__
# if not variable['maps']:
# return JsonResponse({'error': 'Google Maps API is disabled'}, status=400)
print("here")
origin = request.GET.get('origin')
destination = request.GET.get('destination')
language_code = request.GET.get('languageCode', 'en') # Default language set to English
arrival_departure = request.GET.get('arrival_departure', 'departure')
time = request.GET.get('time', "NA")
platform = request.GET.get('platform', 'NA')
version = request.GET.get('version', 'NA')
debug = request.GET.get('debug', False)
sessionToken = request.GET.get('sessionToken', 'NA')
key = request.GET.get('key', 'NA')
if key != settings.AUTH_KEY or int(version.split('.')[0]) < 5:
return JsonResponse({'error': 'Unauthorized'}, status=401)
if not debug:
debug = True

if time == "NA":
# Define the Azores timezone
azores_timezone = pytz.timezone('Atlantic/Azores')
# Get the current UTC time, aware of the timezone
current_utc_time = datetime.now(pytz.utc)
# Convert the current UTC time to Azores time
azores_time = current_utc_time.astimezone(azores_timezone)
# Convert Azores time to Unix timestamp in seconds
time = int(azores_time.timestamp())

if not (origin and destination):
return JsonResponse({'error': 'Missing required parameters'}, status=400)

# Build the Google Maps API URL
maps_url = f"https://maps.googleapis.com/maps/api/directions/json?origin={origin}&destination={destination}&mode=transit&key={settings.GOOGLE_MAPS_API_KEY}&language={language_code}&alternatives=true"
maps_url += f"&arrival_time={time}" if arrival_departure == 'arrival' else f"&departure_time={time}"

try:
response = requests.get(maps_url)
if response.status_code == 200:
data = response.json()
# Process the data and return as needed
return JsonResponse(data) # Simplified for demonstration
else:
return JsonResponse({'warning': 'NA'}, status=response.status_code)
except Exception as e:
return JsonResponse({'warning': 'NA'}, status=500)

@api_view(['GET'])
@require_GET
Expand Down
Binary file modified src/db.sqlite3
Binary file not shown.
4 changes: 3 additions & 1 deletion src/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ numpy==1.23.3
jsonfield==3.1.0
SQLAlchemy==1.4.41
gunicorn==20.1.0
psycopg2-binary==2.8.6
django-environ==0.11.2
requests==2.31.0
#psycopg2-binary==2.8.6

0 comments on commit 385e841

Please sign in to comment.