-
-
Save atomAltera/ed6a5d847e6ac0f93d96 to your computer and use it in GitHub Desktop.
Loads from drive2.ru modles data
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 sys | |
from urllib.request import urlopen | |
import json | |
ERR_SUCCESS = 0 | |
ERR_UNKNOWN = 1 | |
ERR_MAKES_FILE_NOT_FOUND = 2 | |
def load_models(make): | |
url = "http://www.drive2.ru/ajax/brand.models.cshtml?brand=%s" % make | |
response = urlopen(url) | |
if response.status != 200: | |
return None | |
raw_data = response.read() | |
str_data = raw_data.decode().strip() | |
json_data = json.loads(str_data) | |
return json_data | |
def main(): | |
try: | |
makes_file = open('_makes', 'r') | |
except IOError as e: | |
sys.stderr.write(e.__str__()) | |
return ERR_MAKES_FILE_NOT_FOUND | |
models = dict() | |
for make in makes_file: | |
make = make[:-1] | |
print('Loading {0}'.format(make)) | |
models[make] = load_models(make) | |
makes_file.close() | |
models_file = open('models.json', 'w') | |
json.dump(models, models_file) | |
models_file.close() | |
print('Done') | |
return ERR_SUCCESS | |
if __name__ == '__main__': | |
try: | |
exit(main()) | |
except Exception as e: | |
sys.stderr.write(e.__str__()) | |
exit(ERR_UNKNOWN) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment