diff --git a/.gitignore b/.gitignore index f02497f..792872f 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ sikweb/settings.py *.pyc uwsgi.ini uwsgi.log +infoscreen/static/js/hsl.json diff --git a/infoscreen/hsl.py b/infoscreen/hsl.py index 3fa5df9..1fe79c5 100644 --- a/infoscreen/hsl.py +++ b/infoscreen/hsl.py @@ -1,16 +1,15 @@ import json import urllib.request -usernm="hsl-api käyttäjä tähän" -passwd="salasana tähän" -src = urllib.request.urlopen("http://api.reittiopas.fi/hsl/prod/?user="+usernm+"&pass="+passwd+"&request=stops_area¢er_coordinate=2545565,6675319").read().decode("utf-8") +userhash="1aca11408c6615e0a403a90e485a3b9ff0fb7cde7b7d" +src = urllib.request.urlopen("http://api.reittiopas.fi/hsl/prod/?userhash="+userhash+"&request=stops_area¢er_coordinate=2545565,6675319").read().decode("utf-8") data = json.loads(src); arr=[] for element in data: - src = urllib.request.urlopen("http://api.reittiopas.fi/hsl/prod/?user="+usernm+"&pass="+passwd+"&request=stop&code="+element['code']).read().decode("utf-8") + src = urllib.request.urlopen("http://api.reittiopas.fi/hsl/prod/?userhash="+userhash+"&request=stop&code="+element['code']).read().decode("utf-8") parsed = json.loads(src)[0] arr.append({"name":parsed['name_fi'],"lines":parsed['lines'],"dist":element['dist'],"departures":parsed['departures']}) -file=open('json','w') +file=open('static/js/hsl.json','w') file.write(json.dumps(arr)) file.close() diff --git a/infoscreen/static/html/hsl.html b/infoscreen/static/html/hsl.html new file mode 100644 index 0000000..e7a0b78 --- /dev/null +++ b/infoscreen/static/html/hsl.html @@ -0,0 +1,36 @@ +
+ + + + + + + + + + + + + + + + + +
+ Aika + + Bussin numero + + Pysäkki + + Päätepysäkki +
+ {{x.time}} + + {{x.bus}} + + {{x.stop}} + + {{x.laststop}} +
+
diff --git a/infoscreen/static/js/infoscreen_controllers.js b/infoscreen/static/js/infoscreen_controllers.js index 0d67766..c2b6826 100644 --- a/infoscreen/static/js/infoscreen_controllers.js +++ b/infoscreen/static/js/infoscreen_controllers.js @@ -37,3 +37,89 @@ app.controller('ABBController', function($scope, $http){ $scope.jobs = response.data; }) }); +app.controller('timetableCtrl', + function($scope, $http, $interval) { + function load(){ + $http.get('../js/hsl.json') + .then(function(data, status, headers, config) { + $scope.arr=[]; + parse(data); + }); + }; + var obj; + $scope.arr=[]; + var dict=[]; + function parse(data){ + obj=data['data']; + for(var a in obj){ + for(var b in obj[a]['lines']){ + var elem=obj[a]['lines'][b].split(":"); + dict[elem[0]]=elem[1]; + } + for(var b in obj[a]['departures']){ + var e=true; + var time=obj[a]['departures'][b]['time']; + date=obj[a]['departures'][b]['date']; + if(time<1000) + var unit = 1; + else + var unit = 2; + var hh= Math.floor(obj[a]['departures'][b]['time']/100); + if(hh>=24){ + hh-=24; + date++; + } + var mm= obj[a]['departures'][b]['time']%100; + var c= obj[a]['departures'][b]['code'].substring(1,5); + if(c.charAt(0)=='0') + c=c.substring(1,4); + z={"stop":obj[a]['name'].split(",")[0],"dist":obj[a]['dist'],"bus":c,"date":date,"time":pad(hh,2)+":"+pad(mm,2),"laststop":dict[obj[a]['departures'][b]['code']].split(",")[0].split(" ")[0]}; + for(var i= $scope.arr.length-1;i>=0;i--) + if( $scope.arr[i]['bus']==z['bus'] && $scope.arr[i]['laststop']==z['laststop']){ + if( $scope.arr[i]['dist']==z['dist']){ + break; + } + else if( $scope.arr[i]['dist'] > z['dist']){ + $scope.arr.splice(i,1); + } + else + e=false; + } + if(e){ + $scope.arr.push(z); + } + + } + } + function pad(num, size) { + var s = num+""; + while (s.length < size) s = "0" + s; + return s; + } + delOld(); + } + function delOld(){ + f= new Date(); + for(var a=$scope.arr.length-1; a>=0; a--){ + if( $scope.arr[a]['time']<1000) + var unit = 1; + else + var unit = 2; + var time=$scope.arr[a]['time'].split(":"); + date=$scope.arr[a]['date'].toString(); + d= new Date(f); + d.setFullYear(date.substring(0,4),date.substring(4,6)-1,date.substring(6,8)); + d.setHours(time[0]); + d.setMinutes(time[1]); + console.log(d); + if(d < f){ + $scope.arr.splice(a,1); + } + } + } + load(); + var t=$interval(delOld,2000); + var z=$interval(load,60000); + } +); +