| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- from flights_mongodb import mongo_con_parse
- from settings import FLIGHTS_WEBSITES_ROUTE_CONF_TAB, FLIGHTS_CITY_AIRPORT_CODE_TAB
- db = mongo_con_parse()
- website = "gk"
- def get_from_to_data(input_str):
- input_str_list = input_str.split("\t")
- from_airport_code = input_str_list[0]
- to_airport_code = input_str_list[1]
- return {
- "from_airport_code": from_airport_code,
- "to_airport_code": to_airport_code
- }
- def get_city_data(airport_code):
- city_code = None
- find_data = db.get_collection(FLIGHTS_CITY_AIRPORT_CODE_TAB).find_one( # 这里拿出每个机场码所对应的城市码
- {
- "airport_code": airport_code,
- "website": website
- }
- )
- if find_data:
- city_code = find_data.get("city_code")
- return city_code
- def get_airport_data(city_code):
- airport_code_list = []
- find_data = db.get_collection(FLIGHTS_CITY_AIRPORT_CODE_TAB).find( # 这里要拿出每个城市所有对应的机场码
- {
- "city_code": city_code,
- "website": website
- }
- )
- for item in find_data:
- airport_code = item.get("airport_code")
- airport_code_list.append(airport_code)
- return airport_code_list
- # 根据城市码获取机场对
- def find_route_airport_pair(from_city_code, to_city_code):
- airport_code_pair_list = []
- find_data = db.get_collection(FLIGHTS_WEBSITES_ROUTE_CONF_TAB).find( # 这里拿出在航线表里定义的城市码对关联的机场码对
- {
- "from_city_code": from_city_code,
- "to_city_code": to_city_code,
- "source_website": website
- }
- )
- for item in find_data:
- from_airport_code = item.get("from_airport_code")
- to_airport_code = item.get("to_airport_code")
- airport_code_pair_list.append((from_airport_code, to_airport_code)) # 存元组
- return airport_code_pair_list
- def insert_routes():
- # 这里是城市代码, 网站没有的城市/机场码: OSA SPK TYO SHI, 删去它们的航段
- routes = """
- KCZ FUK
- KCZ KOJ
- KCZ KMJ
- KCZ KMI
- KCZ OIT
- KCZ OKA
- KCZ AKJ
- KCZ NGS
- FUK KCZ
- FUK MYJ
- FUK NGO
- FUK OKA
- FUK TAK
- FUK AKJ
- KOJ KCZ
- KOJ KMJ
- KOJ MYJ
- KOJ NGO
- KOJ OKA
- KOJ TAK
- KOJ AKJ
- KMJ KCZ
- KMJ KOJ
- KMJ MYJ
- KMJ NGO
- KMJ OKA
- KMJ TAK
- KMJ AKJ
- MYJ FUK
- MYJ KOJ
- MYJ KMJ
- MYJ KMI
- MYJ OIT
- MYJ OKA
- MYJ AKJ
- MYJ NGS
- KMI KCZ
- KMI MYJ
- KMI OKA
- KMI TAK
- KMI AKJ
- NGO FUK
- NGO KOJ
- NGO KMJ
- NGO OKA
- OIT KCZ
- OIT MYJ
- OIT OKA
- OIT TAK
- OIT AKJ
- OKA KCZ
- OKA FUK
- OKA KOJ
- OKA KMJ
- OKA MYJ
- OKA KMI
- OKA NGO
- OKA OIT
- OKA TAK
- OKA AKJ
- OKA NGS
- TAK FUK
- TAK KOJ
- TAK KMJ
- TAK KMI
- TAK OIT
- TAK OKA
- TAK AKJ
- TAK NGS
- AKJ KCZ
- AKJ FUK
- AKJ KOJ
- AKJ KMJ
- AKJ MYJ
- AKJ KMI
- AKJ OIT
- AKJ OKA
- AKJ TAK
- AKJ NGS
- NGS KCZ
- NGS MYJ
- NGS OKA
- NGS TAK
- NGS AKJ
- KCZ FUK
- KCZ KOJ
- KCZ KMJ
- KCZ KMI
- KCZ OIT
- KCZ OKA
- KCZ KIX
- KCZ CTS
- KCZ NRT
- KCZ HKO
- KCZ AKJ
- KCZ NGS
- FUK KCZ
- FUK MYJ
- FUK NGO
- FUK OKA
- FUK KIX
- FUK CTS
- FUK TAK
- FUK NRT
- FUK HKO
- FUK EKK
- FUK AKJ
- KOJ KCZ
- KOJ KMJ
- KOJ MYJ
- KOJ NGO
- KOJ OKA
- KOJ CTS
- KOJ TAK
- KOJ NRT
- KOJ HKO
- KOJ EKK
- KOJ AKJ
- KMJ KCZ
- KMJ KOJ
- KMJ MYJ
- KMJ NGO
- KMJ OKA
- KMJ KIX
- KMJ CTS
- KMJ TAK
- KMJ NRT
- KMJ HKO
- KMJ EKK
- KMJ AKJ
- MYJ FUK
- MYJ KOJ
- MYJ KMJ
- MYJ KMI
- MYJ OIT
- MYJ OKA
- MYJ CTS
- MYJ NRT
- MYJ HKO
- MYJ AKJ
- MYJ NGS
- KMI KCZ
- KMI MYJ
- KMI OKA
- KMI KIX
- KMI CTS
- KMI TAK
- KMI NRT
- KMI HKO
- KMI EKK
- KMI AKJ
- NGO FUK
- NGO KOJ
- NGO KMJ
- NGO OKA
- NGO CTS
- NGO HKO
- OIT KCZ
- OIT MYJ
- OIT OKA
- OIT KIX
- OIT CTS
- OIT TAK
- OIT NRT
- OIT HKO
- OIT EKK
- OIT AKJ
- OKA KCZ
- OKA FUK
- OKA KOJ
- OKA KMJ
- OKA MYJ
- OKA KMI
- OKA NGO
- OKA OIT
- OKA KIX
- OKA CTS
- OKA TAK
- OKA NRT
- OKA HKO
- OKA EKK
- OKA AKJ
- OKA NGS
- KIX KCZ
- KIX FUK
- KIX KMJ
- KIX KMI
- KIX OIT
- KIX OKA
- KIX CTS
- KIX NRT
- KIX HKO
- KIX EKK
- KIX AKJ
- KIX NGS
- CTS KCZ
- CTS FUK
- CTS KOJ
- CTS KMJ
- CTS MYJ
- CTS KMI
- CTS NGO
- CTS OIT
- CTS OKA
- CTS KIX
- CTS TAK
- CTS NRT
- CTS EKK
- CTS NGS
- TAK FUK
- TAK KOJ
- TAK KMJ
- TAK KMI
- TAK OIT
- TAK OKA
- TAK CTS
- TAK NRT
- TAK HKO
- TAK AKJ
- TAK NGS
- NRT KCZ
- NRT FUK
- NRT KOJ
- NRT KMJ
- NRT MYJ
- NRT KMI
- NRT OIT
- NRT OKA
- NRT KIX
- NRT CTS
- NRT TAK
- NRT HKO
- NRT EKK
- NRT AKJ
- NRT NGS
- HKO KCZ
- HKO FUK
- HKO KOJ
- HKO KMJ
- HKO MYJ
- HKO KMI
- HKO NGO
- HKO OIT
- HKO OKA
- HKO KIX
- HKO TAK
- HKO NRT
- HKO EKK
- HKO NGS
- EKK FUK
- EKK KOJ
- EKK KMJ
- EKK KMI
- EKK OIT
- EKK OKA
- EKK KIX
- EKK CTS
- EKK NRT
- EKK HKO
- EKK AKJ
- EKK NGS
- AKJ KCZ
- AKJ FUK
- AKJ KOJ
- AKJ KMJ
- AKJ MYJ
- AKJ KMI
- AKJ OIT
- AKJ OKA
- AKJ KIX
- AKJ TAK
- AKJ NRT
- AKJ EKK
- AKJ NGS
- NGS KCZ
- NGS MYJ
- NGS OKA
- NGS KIX
- NGS CTS
- NGS TAK
- NGS NRT
- NGS HKO
- NGS EKK
- NGS AKJ
- """
- db.get_collection(FLIGHTS_WEBSITES_ROUTE_CONF_TAB).delete_many({"source_website": website}) # 清空之前数据
- temp_list = [i.strip() for i in routes.split("\n") if i.strip()]
- routes = list(set(temp_list)) # 去重
- routes.sort(key=temp_list.index) # 保持原有顺序
- # print(len(routes))
- # total_route = {x for route in routes for x in route.split('\t')}
- # print(f'一共需要 {len(total_route)} 条航线的城市机场对应信息')
- insert_datas = [] # 列表里准备存字典
- count = 0
- for route in routes:
- name_code_datas = get_from_to_data(route)
- # print(route, name_code_datas)
- from_city_code = name_code_datas.get("from_airport_code")
- to_city_code = name_code_datas.get("to_airport_code")
- from_airport_code = get_airport_data(from_city_code)
- to_airport_code = get_airport_data(to_city_code)
- dict_mode = {
- "source_website": website, # 数据来源网站
- "website_status": 1, # 网站是否采集状态
- "flight_route_status": 1, # 航线是否采集状态
- "from_city_code": from_city_code,
- "from_city_name": "",
- "from_airport_code": from_airport_code,
- "to_city_code": to_city_code,
- "to_city_name": "",
- "to_airport_code": to_airport_code,
- }
- insert_datas.append(dict_mode)
- count += 1
- if count % 50 == 0:
- print("add {} ...".format(count))
- db.get_collection(FLIGHTS_WEBSITES_ROUTE_CONF_TAB).insert_many(insert_datas) # 一次录入多条数据
- print("finish...")
- if __name__ == "__main__":
- insert_routes()
|