Updated hsl timetable

This commit is contained in:
Juuso Käenmäki
2016-09-15 20:11:25 +03:00
parent f41428c048
commit 6eea42fd58
4 changed files with 127 additions and 5 deletions
+4 -5
View File
@@ -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&center_coordinate=2545565,6675319").read().decode("utf-8")
userhash="1aca11408c6615e0a403a90e485a3b9ff0fb7cde7b7d"
src = urllib.request.urlopen("http://api.reittiopas.fi/hsl/prod/?userhash="+userhash+"&request=stops_area&center_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()
+36
View File
@@ -0,0 +1,36 @@
<div ng-app="myApp" ng-controller="timetableCtrl">
<table class="table">
<thead>
<tr>
<th>
Aika
</th>
<th>
Bussin numero
</th>
<th>
Pys&#228;kki
</th>
<th>
P&#228;&#228;tepys&#228;kki
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="x in arr | orderBy: ['date','time']">
<td>
{{x.time}}
</td>
<td>
{{x.bus}}
</td>
<td>
{{x.stop}}
</td>
<td>
{{x.laststop}}
</td>
</tr>
</tbody>
</table>
</div>
@@ -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);
}
);