insert_flights_gk_route.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. from flights_mongodb import mongo_con_parse
  2. from settings import FLIGHTS_WEBSITES_ROUTE_CONF_TAB, FLIGHTS_CITY_AIRPORT_CODE_TAB
  3. db = mongo_con_parse()
  4. website = "gk"
  5. def get_from_to_data(input_str):
  6. input_str_list = input_str.split("\t")
  7. from_airport_code = input_str_list[0]
  8. to_airport_code = input_str_list[1]
  9. return {
  10. "from_airport_code": from_airport_code,
  11. "to_airport_code": to_airport_code
  12. }
  13. def get_city_data(airport_code):
  14. city_code = None
  15. find_data = db.get_collection(FLIGHTS_CITY_AIRPORT_CODE_TAB).find_one( # 这里拿出每个机场码所对应的城市码
  16. {
  17. "airport_code": airport_code,
  18. "website": website
  19. }
  20. )
  21. if find_data:
  22. city_code = find_data.get("city_code")
  23. return city_code
  24. def get_airport_data(city_code):
  25. airport_code_list = []
  26. find_data = db.get_collection(FLIGHTS_CITY_AIRPORT_CODE_TAB).find( # 这里要拿出每个城市所有对应的机场码
  27. {
  28. "city_code": city_code,
  29. "website": website
  30. }
  31. )
  32. for item in find_data:
  33. airport_code = item.get("airport_code")
  34. airport_code_list.append(airport_code)
  35. return airport_code_list
  36. # 根据城市码获取机场对
  37. def find_route_airport_pair(from_city_code, to_city_code):
  38. airport_code_pair_list = []
  39. find_data = db.get_collection(FLIGHTS_WEBSITES_ROUTE_CONF_TAB).find( # 这里拿出在航线表里定义的城市码对关联的机场码对
  40. {
  41. "from_city_code": from_city_code,
  42. "to_city_code": to_city_code,
  43. "source_website": website
  44. }
  45. )
  46. for item in find_data:
  47. from_airport_code = item.get("from_airport_code")
  48. to_airport_code = item.get("to_airport_code")
  49. airport_code_pair_list.append((from_airport_code, to_airport_code)) # 存元组
  50. return airport_code_pair_list
  51. def insert_routes():
  52. # 这里是城市代码, 网站没有的城市/机场码: OSA SPK TYO SHI, 删去它们的航段
  53. routes = """
  54. KCZ FUK
  55. KCZ KOJ
  56. KCZ KMJ
  57. KCZ KMI
  58. KCZ OIT
  59. KCZ OKA
  60. KCZ AKJ
  61. KCZ NGS
  62. FUK KCZ
  63. FUK MYJ
  64. FUK NGO
  65. FUK OKA
  66. FUK TAK
  67. FUK AKJ
  68. KOJ KCZ
  69. KOJ KMJ
  70. KOJ MYJ
  71. KOJ NGO
  72. KOJ OKA
  73. KOJ TAK
  74. KOJ AKJ
  75. KMJ KCZ
  76. KMJ KOJ
  77. KMJ MYJ
  78. KMJ NGO
  79. KMJ OKA
  80. KMJ TAK
  81. KMJ AKJ
  82. MYJ FUK
  83. MYJ KOJ
  84. MYJ KMJ
  85. MYJ KMI
  86. MYJ OIT
  87. MYJ OKA
  88. MYJ AKJ
  89. MYJ NGS
  90. KMI KCZ
  91. KMI MYJ
  92. KMI OKA
  93. KMI TAK
  94. KMI AKJ
  95. NGO FUK
  96. NGO KOJ
  97. NGO KMJ
  98. NGO OKA
  99. OIT KCZ
  100. OIT MYJ
  101. OIT OKA
  102. OIT TAK
  103. OIT AKJ
  104. OKA KCZ
  105. OKA FUK
  106. OKA KOJ
  107. OKA KMJ
  108. OKA MYJ
  109. OKA KMI
  110. OKA NGO
  111. OKA OIT
  112. OKA TAK
  113. OKA AKJ
  114. OKA NGS
  115. TAK FUK
  116. TAK KOJ
  117. TAK KMJ
  118. TAK KMI
  119. TAK OIT
  120. TAK OKA
  121. TAK AKJ
  122. TAK NGS
  123. AKJ KCZ
  124. AKJ FUK
  125. AKJ KOJ
  126. AKJ KMJ
  127. AKJ MYJ
  128. AKJ KMI
  129. AKJ OIT
  130. AKJ OKA
  131. AKJ TAK
  132. AKJ NGS
  133. NGS KCZ
  134. NGS MYJ
  135. NGS OKA
  136. NGS TAK
  137. NGS AKJ
  138. KCZ FUK
  139. KCZ KOJ
  140. KCZ KMJ
  141. KCZ KMI
  142. KCZ OIT
  143. KCZ OKA
  144. KCZ KIX
  145. KCZ CTS
  146. KCZ NRT
  147. KCZ HKO
  148. KCZ AKJ
  149. KCZ NGS
  150. FUK KCZ
  151. FUK MYJ
  152. FUK NGO
  153. FUK OKA
  154. FUK KIX
  155. FUK CTS
  156. FUK TAK
  157. FUK NRT
  158. FUK HKO
  159. FUK EKK
  160. FUK AKJ
  161. KOJ KCZ
  162. KOJ KMJ
  163. KOJ MYJ
  164. KOJ NGO
  165. KOJ OKA
  166. KOJ CTS
  167. KOJ TAK
  168. KOJ NRT
  169. KOJ HKO
  170. KOJ EKK
  171. KOJ AKJ
  172. KMJ KCZ
  173. KMJ KOJ
  174. KMJ MYJ
  175. KMJ NGO
  176. KMJ OKA
  177. KMJ KIX
  178. KMJ CTS
  179. KMJ TAK
  180. KMJ NRT
  181. KMJ HKO
  182. KMJ EKK
  183. KMJ AKJ
  184. MYJ FUK
  185. MYJ KOJ
  186. MYJ KMJ
  187. MYJ KMI
  188. MYJ OIT
  189. MYJ OKA
  190. MYJ CTS
  191. MYJ NRT
  192. MYJ HKO
  193. MYJ AKJ
  194. MYJ NGS
  195. KMI KCZ
  196. KMI MYJ
  197. KMI OKA
  198. KMI KIX
  199. KMI CTS
  200. KMI TAK
  201. KMI NRT
  202. KMI HKO
  203. KMI EKK
  204. KMI AKJ
  205. NGO FUK
  206. NGO KOJ
  207. NGO KMJ
  208. NGO OKA
  209. NGO CTS
  210. NGO HKO
  211. OIT KCZ
  212. OIT MYJ
  213. OIT OKA
  214. OIT KIX
  215. OIT CTS
  216. OIT TAK
  217. OIT NRT
  218. OIT HKO
  219. OIT EKK
  220. OIT AKJ
  221. OKA KCZ
  222. OKA FUK
  223. OKA KOJ
  224. OKA KMJ
  225. OKA MYJ
  226. OKA KMI
  227. OKA NGO
  228. OKA OIT
  229. OKA KIX
  230. OKA CTS
  231. OKA TAK
  232. OKA NRT
  233. OKA HKO
  234. OKA EKK
  235. OKA AKJ
  236. OKA NGS
  237. KIX KCZ
  238. KIX FUK
  239. KIX KMJ
  240. KIX KMI
  241. KIX OIT
  242. KIX OKA
  243. KIX CTS
  244. KIX NRT
  245. KIX HKO
  246. KIX EKK
  247. KIX AKJ
  248. KIX NGS
  249. CTS KCZ
  250. CTS FUK
  251. CTS KOJ
  252. CTS KMJ
  253. CTS MYJ
  254. CTS KMI
  255. CTS NGO
  256. CTS OIT
  257. CTS OKA
  258. CTS KIX
  259. CTS TAK
  260. CTS NRT
  261. CTS EKK
  262. CTS NGS
  263. TAK FUK
  264. TAK KOJ
  265. TAK KMJ
  266. TAK KMI
  267. TAK OIT
  268. TAK OKA
  269. TAK CTS
  270. TAK NRT
  271. TAK HKO
  272. TAK AKJ
  273. TAK NGS
  274. NRT KCZ
  275. NRT FUK
  276. NRT KOJ
  277. NRT KMJ
  278. NRT MYJ
  279. NRT KMI
  280. NRT OIT
  281. NRT OKA
  282. NRT KIX
  283. NRT CTS
  284. NRT TAK
  285. NRT HKO
  286. NRT EKK
  287. NRT AKJ
  288. NRT NGS
  289. HKO KCZ
  290. HKO FUK
  291. HKO KOJ
  292. HKO KMJ
  293. HKO MYJ
  294. HKO KMI
  295. HKO NGO
  296. HKO OIT
  297. HKO OKA
  298. HKO KIX
  299. HKO TAK
  300. HKO NRT
  301. HKO EKK
  302. HKO NGS
  303. EKK FUK
  304. EKK KOJ
  305. EKK KMJ
  306. EKK KMI
  307. EKK OIT
  308. EKK OKA
  309. EKK KIX
  310. EKK CTS
  311. EKK NRT
  312. EKK HKO
  313. EKK AKJ
  314. EKK NGS
  315. AKJ KCZ
  316. AKJ FUK
  317. AKJ KOJ
  318. AKJ KMJ
  319. AKJ MYJ
  320. AKJ KMI
  321. AKJ OIT
  322. AKJ OKA
  323. AKJ KIX
  324. AKJ TAK
  325. AKJ NRT
  326. AKJ EKK
  327. AKJ NGS
  328. NGS KCZ
  329. NGS MYJ
  330. NGS OKA
  331. NGS KIX
  332. NGS CTS
  333. NGS TAK
  334. NGS NRT
  335. NGS HKO
  336. NGS EKK
  337. NGS AKJ
  338. """
  339. db.get_collection(FLIGHTS_WEBSITES_ROUTE_CONF_TAB).delete_many({"source_website": website}) # 清空之前数据
  340. temp_list = [i.strip() for i in routes.split("\n") if i.strip()]
  341. routes = list(set(temp_list)) # 去重
  342. routes.sort(key=temp_list.index) # 保持原有顺序
  343. # print(len(routes))
  344. # total_route = {x for route in routes for x in route.split('\t')}
  345. # print(f'一共需要 {len(total_route)} 条航线的城市机场对应信息')
  346. insert_datas = [] # 列表里准备存字典
  347. count = 0
  348. for route in routes:
  349. name_code_datas = get_from_to_data(route)
  350. # print(route, name_code_datas)
  351. from_city_code = name_code_datas.get("from_airport_code")
  352. to_city_code = name_code_datas.get("to_airport_code")
  353. from_airport_code = get_airport_data(from_city_code)
  354. to_airport_code = get_airport_data(to_city_code)
  355. dict_mode = {
  356. "source_website": website, # 数据来源网站
  357. "website_status": 1, # 网站是否采集状态
  358. "flight_route_status": 1, # 航线是否采集状态
  359. "from_city_code": from_city_code,
  360. "from_city_name": "",
  361. "from_airport_code": from_airport_code,
  362. "to_city_code": to_city_code,
  363. "to_city_name": "",
  364. "to_airport_code": to_airport_code,
  365. }
  366. insert_datas.append(dict_mode)
  367. count += 1
  368. if count % 50 == 0:
  369. print("add {} ...".format(count))
  370. db.get_collection(FLIGHTS_WEBSITES_ROUTE_CONF_TAB).insert_many(insert_datas) # 一次录入多条数据
  371. print("finish...")
  372. if __name__ == "__main__":
  373. insert_routes()