вторник, 24 января 2017 г.

Russian Post Tracking (python edition)

Продолжаем благородную и бп такую полезную тему как отслеживание посылок на Почте России. В прошлый раз это был bash + jq на этот раз python.


 import urllib3  
 import json  
 import os  
 tracks="RR271518558JP", "RL913799426NC", "LMJ71813512CN", "L0Q71954815CN", "LQ017898534CN", "RP351676848SG" , "29844602075216"  
 index = "index.html"  
 http = urllib3.PoolManager()  
 def deleteContent(fName):  
   with open(fName, "w"):  
     pass  
 pwd=os.getcwd()  
 if pwd != "/var/www/html/dash/track":  
     os.chdir("/var/www/html/dash/track")  
 #get tracks from russia post  
 for p in tracks:   
      print p  
      url="https://www.pochta.ru/tracking?p_p_id=trackingPortlet_WAR_portalportlet&p_p_lifecycle=2&p_p_state=normal&p_p_mode=view&p_p_resource_id=getList&p_p_cacheability=cacheLevelPage&p_p_col_id=column-1&p_p_col_count=1&barcodeList=" + str(p) + "&postmanAllowed=true&_=1461757915398"  
      r = http.request('GET', url)  
      f = open(p+".tmp",'w')  
      f.write(r.data)  
      f.close()  
 deleteContent(index)  
 #and parse them all  
 for jf in tracks:  
      with open(jf+".tmp") as data_file:    
             data = json.load(data_file)  
           data_file.close()  
      barcode = data["list"][0]["trackingItem"]["barcode"]  
      title = data["list"][0]["trackingItem"]["title"]  
      origCN = data["list"][0]["trackingItem"]["originCountryName"]  
      ihistlen = len(data["list"][0]["trackingItem"]["trackingHistoryItemList"])  
      with open(index,'a') as index_file:  
                index_file.write(barcode+" "+title+" <br/> \n")  
                for k in range(0,ihistlen-1):  
                     index_file.write(data["list"][0]["trackingItem"]["trackingHistoryItemList"][k]["date"]+" ")  
                     index_file.write(data["list"][0]["trackingItem"]["trackingHistoryItemList"][k]["humanStatus"]+" ")  
                     #and unicode!   
                     description = data["list"][0]["trackingItem"]["trackingHistoryItemList"][k]["description"]  
                     index_file.write(description.encode("utf-8", "ignore")+" <br/> \n")  
                index_file.write("<br/> \n <br/> \n <br/> \n")  
                index_file.close()       

на выходе получим index.html приблизительно такого вида:

 RR271518558JP Small packet from Japan   
 2016-05-06T19:06:00.000+03:00 Delivered to the addressee Moskva 122   
 2016-05-04T04:40:00.000+03:00 Available for pickup at the Post Office Moskva 122   
 2016-05-03T02:47:00.000+03:00 Departed from local distribution center Moskva MSP-34 MPKO-Tsentr Uompo   
 2016-05-01T22:22:00.000+03:00 Forwarded for delivery within Russia Mr Lts Vnukovo Tsekh-1   
 2016-04-30T11:20:00.000+03:00 Released from RF customs Mr Lts Vnukovo Tsekh-1   
 2016-04-30T11:20:00.000+03:00 Handed over to the customs Mr Lts Vnukovo Tsekh-1   
 2016-04-30T10:54:00.000+03:00 Registered Mr Lts Vnukovo Tsekh-1   
 2016-04-29T04:42:00.000+03:00 Arrived to Russian Federation Sheremetʹyevo MMPO PCI-7   
 2016-04-26T16:00:00.000+09:00 Awaiting for departure from Japan Japan