Advertisement
yungyao

Simple Ubike Query System

Jun 7th, 2021
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.37 KB | None | 0 0
  1. import requests as rq
  2.  
  3. def printData(stat):
  4.     print (stat["sna"],' 剩餘車輛數:',stat["sbi"],', 剩餘空位數:',int(stat["tot"])-int(stat["sbi"]),sep="")
  5.     print ("車站位置 :",stat["ar"],',',stat["sarea"],sep='')
  6.  
  7. def isQueryinData(query,data):
  8.     if query in data["sna"] or query in data["ar"] or query in data["sarea"] or query in data["snaen"] or query in data["aren"] or query in data["sareaen"]:
  9.         return True
  10.     else:
  11.         return False;
  12.  
  13.  
  14. while True:
  15.     ubike = rq.get ("https://tcgbusfs.blob.core.windows.net/blobyoubike/YouBikeTP.json").json()["retVal"]
  16.     ubike2 = rq.get ("https://tcgbusfs.blob.core.windows.net/dotapp/youbike/v2/youbike_immediate.json").json()
  17.     ubikeNT = rq.get (" https://data.ntpc.gov.tw/api/datasets/71CD1490-A2DF-4198-BEF1-318479775E8A/csv/file").text
  18.  
  19.     ubikeNT = str(ubikeNT).split('\n')[:-1]
  20.     ubikeNT = {row.split('"')[1] : {ubikeNT[0].split('"')[1::2][i] : row.split('"')[1::2][i] for i in range(14)} for row in ubikeNT[1:]}
  21.  
  22.     query = input("請輸入你要查詢的站點名稱或關鍵字或輸入0以退出:")
  23.     if query == "0":
  24.         break
  25.  
  26.     queryList = {}
  27.     legal_queries = 0
  28.     for stat in ubike:
  29.         if isQueryinData(query, ubike[stat]):
  30.             legal_queries += 1
  31.             queryList[legal_queries] = ubike[stat]
  32.     for stat in ubike2:
  33.         if isQueryinData(query, stat):
  34.             legal_queries += 1
  35.             queryList[legal_queries] = stat
  36.     for stat in ubikeNT:
  37.         if isQueryinData(query, ubikeNT[stat]):
  38.             legal_queries += 1
  39.             queryList[legal_queries] = ubikeNT[stat]
  40.        
  41.    
  42.     print ("以下為搜尋結果")
  43.     if legal_queries > 1:
  44.         for key in queryList:
  45.             print(key,queryList[key]['sna'])
  46.     else:
  47.         if legal_queries == 1:
  48.             printData(queryList[1])
  49.         else:
  50.             print ("不存在符合查詢的站點")
  51.         continue
  52.  
  53.     n = input("請輸入查詢的站點編號或輸入0以取消:")
  54.     while True:
  55.         try:
  56.             n = int(n)
  57.             if 0 <= n <= len(queryList):
  58.                 break
  59.             else:
  60.                 print("請輸入範圍內的數")
  61.         except ValueError:
  62.             print ("請輸入整數")
  63.         n = input("請重新輸入查詢的站點編號或輸入0以取消:")
  64.    
  65.     if n:
  66.         printData (queryList[n])
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement