| 5 | |
| 6 | |
| 7 | def make_data(): |
| 8 | source_file_list = [ |
| 9 | 'village', 'village_object', 'town', 'town_object', 'country', 'country_object', 'city', 'city_object', 'province', 'province_object'] |
| 10 | for k in reversed(source_file_list): |
| 11 | data = codecs.open('json/%s.json' % k, 'r', 'utf-8').read() |
| 12 | js_data = 'let %s = ' % k + data + '\nexport {%s} ' % k |
| 13 | out_js = codecs.open('js/%s.js' % k, 'w', 'utf-8') |
| 14 | out_js.write(js_data) |
| 15 | json_data = json.loads(data) |
| 16 | mysql_data_list = [] |
| 17 | |
| 18 | if k == 'province': |
| 19 | for index, i in enumerate(json_data): |
| 20 | mysql_data = "INSERT INTO province VALUES ('%s', '%s', '%s');\n" % (index + 1, i['name'], i['id']) # noqa |
| 21 | mysql_data_list.append(mysql_data) |
| 22 | |
| 23 | if k == 'city': |
| 24 | index = 0 |
| 25 | for province_id in sorted(json_data.keys()): |
| 26 | for city in json_data[province_id]: |
| 27 | index += 1 |
| 28 | mysql_data = "INSERT INTO city VALUES ('%s', '%s', '%s', '%s');\n" % (index, city['name'], city['id'], province_id) # noqa |
| 29 | mysql_data_list.append(mysql_data) |
| 30 | |
| 31 | if k == 'country': |
| 32 | index = 0 |
| 33 | for city_id in sorted(json_data.keys()): |
| 34 | for country in json_data[city_id]: |
| 35 | index += 1 |
| 36 | mysql_data = "INSERT INTO country VALUES ('%s', '%s', '%s', '%s');\n" % (index, country['name'], country['id'], city_id) # noqa |
| 37 | mysql_data_list.append(mysql_data) |
| 38 | |
| 39 | if k == 'town': |
| 40 | index = 0 |
| 41 | for country_id in sorted(json_data.keys()): |
| 42 | for town in json_data[country_id]: |
| 43 | index += 1 |
| 44 | mysql_data = "INSERT INTO town VALUES ('%s', '%s', '%s', '%s');\n" % (index, town['name'], town['id'], country_id) # noqa |
| 45 | mysql_data_list.append(mysql_data) |
| 46 | |
| 47 | if k == 'village': |
| 48 | index = 0 |
| 49 | for town_id in sorted(json_data.keys()): |
| 50 | for village in json_data[town_id]: |
| 51 | index += 1 |
| 52 | mysql_data = "INSERT INTO village VALUES ('%s', '%s', '%s', '%s');\n" % (index, village['name'], village['id'], town_id) # noqa |
| 53 | mysql_data_list.append(mysql_data) |
| 54 | |
| 55 | if k in ['province', 'city', 'country', 'town', 'village']: |
| 56 | out_mysql = codecs.open('mysql/%s.sql' % k, 'w', 'utf-8') |
| 57 | index = 0 |
| 58 | start = 0 |
| 59 | while start < len(mysql_data_list): |
| 60 | end = start + 1000 |
| 61 | out_mysql.write(''.join(mysql_data_list[start:end])) |
| 62 | start = end |
| 63 | |
| 64 | out_mysql.close() |