Created
February 10, 2019 14:11
-
-
Save dmitrodem/ae300dec4b48b64e11f3f9ab38ff57e8 to your computer and use it in GitHub Desktop.
Получить информацию о последнем статусе посылки с russianpost.ru
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import hashlib, base64, uuid | |
import requests, urllib | |
import urllib | |
import time, datetime | |
import json | |
import os | |
class RussianPostClient: | |
UA = "Dalvik/2.1.0 (Linux; U; Android 5.1.1; Moto G Build/LMY48Y)" | |
bootstrapVER = "4.0" | |
VER = "5.0" | |
osVersion = "22" | |
BASEURL = "https://mobileapp.russianpost.ru/mobile-api/method" | |
def init_api(self, filename = "russianpost.json"): | |
deviceId = str(uuid.uuid4()) | |
save_changes = "Подписаться" | |
x = (deviceId + save_changes + self.osVersion).encode("utf-8") | |
deviceToken = base64.b64encode(hashlib.sha256(x).digest()).decode("ascii") | |
params = { | |
"v": self.bootstrapVER, | |
"deviceOs": "Android", | |
"deviceId" : deviceId, | |
"deviceToken" : deviceToken, | |
"deviceOsVersion" : self.osVersion, | |
"appVersion": self.bootstrapVER, | |
"deviceModel" : "Moto G", | |
"deviceManufacturer" : "motorola" | |
} | |
while True: | |
try: | |
r = requests.post("{}/{}/auth.signin".format(self.BASEURL, self.bootstrapVER), | |
data = params, | |
headers = { | |
"User-Agent" : self.UA, | |
}) | |
params.update(r.json()) | |
break | |
except requests.exceptions.ConnectionError: | |
print("Failure") | |
time.sleep(1) | |
with open(filename, "w") as fd: | |
json.dump(params, fd) | |
def __init__(self, filename = "russianpost.json"): | |
try: | |
with open(filename, "r") as fd: | |
self.params = json.load(fd) | |
except FileNotFoundError: | |
self.init_api(filename) | |
with open(filename, "r") as fd: | |
self.params = json.load(fd) | |
self.headers = {"User-Agent": self.UA, | |
"MobileApiAccessToken": self.params["accessToken"]} | |
@staticmethod | |
def now(): | |
return datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S") | |
def track_item_create(self, rpoId): | |
url = "{}/{}/track.item.create".format(self.BASEURL, self.VER) | |
data = {"currentDateTime": self.now(), | |
"rpoId": rpoId} | |
r = requests.post(url, headers = self.headers, data = data) | |
return r.json()["lastHistoryItem"] | |
def pretty_print_status(self, rpoId): | |
r = self.track_item_create(rpoId) | |
n1 = (56 - len(rpoId))//2 | |
n2 = 56 - len(rpoId) - n1 | |
print("="*n1 + " " + rpoId + " " + "="*n2) | |
for k, v in r.items(): | |
print(f"|{k: <25}|{v: <30}|") | |
print(58*"-") | |
def main(): | |
import sys | |
rp = RussianPostClient() | |
for rpoId in sys.argv[1:]: | |
rp.pretty_print_status(rpoId) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
почта давным-давно сайт обновила. Код, конечно, сдох.