Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New avid #14

Open
wants to merge 5 commits into
base: get
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed map_project/__pycache__/scrape.cpython-311.pyc
Binary file not shown.
Binary file removed map_project/api/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file removed map_project/api/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file removed map_project/api/__pycache__/admin.cpython-310.pyc
Binary file not shown.
Binary file removed map_project/api/__pycache__/admin.cpython-311.pyc
Binary file not shown.
Binary file removed map_project/api/__pycache__/apps.cpython-310.pyc
Binary file not shown.
Binary file removed map_project/api/__pycache__/apps.cpython-311.pyc
Binary file not shown.
Binary file removed map_project/api/__pycache__/models.cpython-310.pyc
Binary file not shown.
Binary file removed map_project/api/__pycache__/models.cpython-311.pyc
Binary file not shown.
Binary file removed map_project/api/__pycache__/serializers.cpython-310.pyc
Binary file not shown.
Binary file not shown.
Binary file removed map_project/api/__pycache__/urls.cpython-310.pyc
Binary file not shown.
Binary file removed map_project/api/__pycache__/urls.cpython-311.pyc
Binary file not shown.
Binary file removed map_project/api/__pycache__/views.cpython-310.pyc
Binary file not shown.
Binary file removed map_project/api/__pycache__/views.cpython-311.pyc
Binary file not shown.
5 changes: 4 additions & 1 deletion map_project/api/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from django.contrib import admin

from .models import BusAlert, SignUp
# Register your models here.

admin.site.register(BusAlert)
admin.site.register(SignUp)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 4.1.3 on 2022-11-06 09:45

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('api', '0005_alter_busalert_busname_alter_busalert_busnumber_and_more'),
]

operations = [
migrations.AlterField(
model_name='busalert',
name='busname',
field=models.CharField(default='', max_length=50),
),
migrations.AlterField(
model_name='busalert',
name='busnumber',
field=models.CharField(default='', max_length=3),
),
migrations.AlterField(
model_name='signup',
name='email',
field=models.EmailField(max_length=254),
),
]
18 changes: 18 additions & 0 deletions map_project/api/migrations/0007_alter_signup_time.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.1.3 on 2022-11-06 09:49

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('api', '0006_alter_busalert_busname_alter_busalert_busnumber_and_more'),
]

operations = [
migrations.AlterField(
model_name='signup',
name='time',
field=models.TimeField(auto_now=True),
),
]
18 changes: 18 additions & 0 deletions map_project/api/migrations/0008_alter_signup_favourites.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.1.3 on 2022-11-06 14:00

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('api', '0007_alter_signup_time'),
]

operations = [
migrations.AlterField(
model_name='signup',
name='favourites',
field=models.CharField(default='', max_length=500),
),
]
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
92 changes: 86 additions & 6 deletions map_project/api/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from django.db import models
import string, random
import json
from django.core.mail import send_mail
from threading import Thread
import time


# Create your models here.
def jsonReader():
Expand Down Expand Up @@ -29,6 +33,69 @@ def jsonReader():
print(PopulatedDict)
return PopulatedDict

def jsonReadPreferences(preferences):
preferenceList = preferences.split(",")
PopulatedDict = {}
with open("./Alerts.json") as rawOpen:
Data = json.load(rawOpen)
for i in Data:
if Data[i] in preferenceList:
# print(preferences)
for y in Data[i]:
#print(y)
linkIndex = (y).find("<")
#print(linkIndex)
if linkIndex != -1:
#print(i in PopulatedDict.keys())
if ((i in PopulatedDict.keys()) == False):
PopulatedDict[i] = [(str(y))[0:linkIndex]]
#print("THIS RAN")
else:
PopulatedDict[i].append((str(y))[0:linkIndex])
else:
if ((i in PopulatedDict.keys()) == False):
PopulatedDict[i] = [str(y)]
else:
PopulatedDict[i].append(str(y))
print(PopulatedDict)
return PopulatedDict

def emailPerson(subject,message,otherEmail):
send_mail(subject,message,"[email protected]",[str(otherEmail)],fail_silently=False)
print("Email Delivered")

def EmailTimer():
while(True):
emailALL()
print("cycle complete")
time.sleep(43200) # 12 hours --> 43200

def StartTimer():
t2 = Thread(target=EmailTimer)
t2.daemon = True
t2.start()
print("TIMER STARTING")


def emailALL():
profileCount = (SignUp.objects.all()).count()
for i in range(profileCount):
favoriteList = getattr(SignUp.objects.all()[i],"favourites")
favoriteList = favoriteList.split(",") # --> ['line 1', 'bus 1', 'bus 50']
# personalized = jsonReader()#jsonReadPreferences(getattr(SignUp.objects.all()[i],"favourites"))
print(favoriteList)
alertinfo = AlertInfo()
string = f''
for b in favoriteList:
string += "\n" + alertinfo.getmessage(b)
print(string)

if not string:
emailPerson("STATUS UPDATE", "No Alert Messages!",getattr(SignUp.objects.all()[i],"email"))
else:
emailPerson("STATUS UPDATE", string ,getattr(SignUp.objects.all()[i],"email"))
#display info

class BusAlert(models.Model):
busnumber = models.CharField(max_length=3, default='')
busname = models.CharField(max_length=50, default='')
Expand All @@ -37,8 +104,9 @@ class BusAlert(models.Model):

class SignUp(models.Model):
email = models.EmailField()
favourites = models.CharField(max_length=8, null=True, blank=True)
time = models.TimeField(auto_now=False, auto_now_add=False)
favourites = models.CharField(max_length=500, default = "")
time = models.TimeField(auto_now=True, auto_now_add=False)



class AlertInfo:
Expand All @@ -55,8 +123,20 @@ def getNumber(self, bus: str) -> str:
elif bus[0] == 'L':
return bus[4:]

def getName(self, bus: str) -> str:
message = self.alertdict[bus]
index = message.find(':')
def getmessage(self, bus: str):
if bus in self.alertdict:
print(self.alertdict)
message = self.alertdict[bus]
a = f''
for i in message:
a += i
a += '\n'
message = a
else:
message = ''
return message



return message[:index]
# class Users:
# def __init__
5 changes: 4 additions & 1 deletion map_project/api/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from .views import BusAlertAPIView, SignUpAPIView
from django.urls import path
from .models import BusAlert, SignUp, emailPerson, jsonReadPreferences, emailALL, EmailTimer,StartTimer

urlpatterns = [
path('BusAlert', BusAlertAPIView.as_view()),
path('SignUp', SignUpAPIView.as_view()),
]
]

StartTimer()
28 changes: 25 additions & 3 deletions map_project/api/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from rest_framework import generics, status
from .models import BusAlert, SignUp
from .models import BusAlert, SignUp, emailPerson, jsonReadPreferences, emailALL, EmailTimer,StartTimer
from .serializers import BusAlertSerializer, SignUpCreateSerializer
from rest_framework.response import Response

Expand All @@ -19,5 +19,27 @@ def get(self, request, *args, **kwargs):


class SignUpAPIView(generics.CreateAPIView):
queryset = SignUpCreateSerializer
serializer_class = SignUpCreateSerializer
serializer_class = SignUpCreateSerializer
# if Started == False:
# emailALL()
# Started == True
def get_queryset(self):
signups = SignUp.objects.all()
return signups

def post(self, request, *args, **kwargs):

signups_data = request.data

new_signup = SignUp.objects.create(email=signups_data["email"], favourites=signups_data["favourites"],)

new_signup.save()


serializer = SignUpCreateSerializer(new_signup)

return Response(serializer.data)


# def display_model_fields(model):
# print(getattr(new_))
Binary file modified map_project/db.sqlite3
Binary file not shown.
Binary file not shown.
Binary file not shown.
4 changes: 4 additions & 0 deletions map_project/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from scrape import runScrape
from subprocess import call
from threading import Thread
#from api.models import emailALL,EmailTimers
import requests
import time

Expand Down Expand Up @@ -34,5 +35,8 @@ def startScraping():
t1 = Thread(target=startScraping)
t1.daemon = True
t1.start()
# t2 = Thread(target = emailALL)
# t2.daemon = True
# t2.start()
#startScraping()
main()
Loading