Advertisement
Guest User

Untitled

a guest
Sep 8th, 2018
1,582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.76 KB | None | 0 0
  1. import time, keyboard, webbrowser, pyautogui, pyperclip
  2. from selenium import webdriver
  3. from pynput.keyboard import Key, Controller
  4. keyboard = Controller()
  5.  
  6. # input the airport list in list_depart and list_destination, and also the complete names in dico_corresp.
  7. # input the dates and hours in the loop below
  8. # if no flight is found, it will return the URL.
  9.  
  10. dico_corresp = {"DUB":"dublin",
  11.                 "BCN":"barcelone",
  12.                 "CPH":"copenhague",
  13.                 "PRG":"prague",
  14.                 "NAP":"naples",
  15.                 "PMO":"palerme",
  16.                 "CAG":"cagliari",
  17.                 "CLY":"calvi"}
  18.  
  19. list_depart = ["CDG","ORY"] #from where we want to fly
  20. list_destination = ["DUB","BCN","PMO"] #where we may want to go
  21.  
  22. #"AGA","CMN","FEZ","RAK","NDR","OUD","RBA","TNG","TTU","VIL","EUN","DJE","TUN"
  23. #"DUB","BCN","CPH","PRG","NAP","PMO","CAG","CLY","BIA","AJA","FSC"]
  24.  
  25. final_list = []
  26. final_list_before_question =[]
  27. final_list_question = [] #the list that contains all data, joined by "?"
  28.  
  29.  
  30. for aeroport_depart in list_depart: #we start with the first departure airport, and try each destination. And then we go to the 2nd
  31.    
  32.     for destination in list_destination: #for each destination, we copy the infos in a variable "all infos", and make a list ou of it
  33.         aero_depart = aeroport_depart # takes the info from the first loop
  34.         aero_arrivee = destination #takes the info from this current loop
  35.         date_depart = "2019-02-01"
  36.         heure_depart = "1800-2400"
  37.         date_retour = "2019-02-10"
  38.         heure_retour = "1800-2400"
  39.         all_infos = "empty"
  40.  
  41.         # open google flights in firefox, go to the url with all the parameters, and scroll down a bit so that we can access the flights and copy them
  42.         url = 'https://www.google.fr/flights/#flt='+ aero_depart +"."+ aero_arrivee + "."+ date_depart+"*"+aero_arrivee+"."+aero_depart +"." + date_retour +";c:EUR;e:1;s:0*0;dt:"+heure_depart+";at:"+heure_retour+";sd:1;t:f"
  43.         browser = webdriver.Firefox()
  44.         browser.get(url)
  45.         time.sleep(3)
  46.         pyautogui.press('down')
  47.         pyautogui.press('down')
  48.         time.sleep(1)
  49.        
  50.         #move the mouse to copy the flights info, ctrl c them into the "all_infos" variable, and the close firefox
  51.         pyautogui.moveTo(263, 663)
  52.         time.sleep(1)
  53.         pyautogui.dragTo(1135, 710, 1, button='left')
  54.         time.sleep(1)
  55.         pyperclip.copy("")
  56.         keyboard.press(Key.ctrl)
  57.         keyboard.press('c')
  58.         keyboard.release('c')
  59.         keyboard.release(Key.ctrl)
  60.         time.sleep(1)
  61.         all_infos = pyperclip.paste()
  62.         keyboard.press(Key.alt)
  63.         keyboard.press(Key.f4)
  64.         keyboard.release(Key.alt)
  65.         keyboard.release(Key.f4)
  66.        
  67.         #we create a list that will contain all the infos from the flight.
  68.         list_allinfo= []
  69.         list_allinfo= all_infos.split("\n") #each time there is a new line, we split that into an item of the list
  70.        
  71.         #we clean the list, add the url to it, and add all this to a new list "final_list"
  72.         cleaned_all_info_list = []
  73.         for element in list_allinfo:
  74.             if element.strip() != '':
  75.                 cleaned_all_info_list.append(element.strip())
  76.  
  77.         cleaned_all_info_list.append(url)
  78.         final_list.append(cleaned_all_info_list)
  79.        
  80.         # for each list in the final_list, we merge the flight carrier into 1 item in the case there's more than 1
  81.         for sublist in final_list:
  82.             if len(sublist) <2 : #if it's only an url we skip
  83.                 continue
  84.             else:
  85.                 while "min" not in sublist[2]: #if "min" (minutes) is not the 3rd item, then that means we need to merge the previous items into one
  86.                     print("\n")
  87.                     print("----------")
  88.                     print("found one!")
  89.                     print("----------")
  90.                     print("\n")
  91.                     print("here it is before:")
  92.                     print(sublist)
  93.                     print("\n")
  94.                     sublist = [sublist[0]]+['-'.join(sublist[1:3])]+sublist[3:] #we take the 1st element of the list, merge the 2nd to 4th, and then add the rest
  95.                     sublist[1] = sublist[1].replace(",","") #remove the comma
  96.                     print("here it is after:")
  97.                     print(sublist)
  98.                     print(type(sublist))
  99.                     print("\n")
  100.         final_list_before_question.append(sublist) #we add all these lists into another list
  101.  
  102.  
  103.  
  104. for sublist in final_list_before_question:
  105.         final_list_question.append("?".join(sublist)) #all the elements are merged into one with a ? as a separator, so that we can split the columns easily later in google sheets
  106.  
  107.  
  108. #open google sheets
  109. browser = webdriver.Firefox()
  110. browser.get('https://docs.google.com/spreadsheets/d/14vG9O1Q3MCNytqJZJwPN8xdvP9mCJQTGws3sDJV9I-4/edit?usp=sharing')
  111. time.sleep(7) #wait for it to load
  112.  
  113. # we copy line by line each element of the final_list_question in google sheets
  114. for sublist in final_list_question:
  115.     pyperclip.copy(str(sublist))
  116.     time.sleep(1)
  117.     keyboard.press(Key.ctrl)
  118.     keyboard.press('v')
  119.     keyboard.release('v')
  120.     keyboard.release(Key.ctrl)
  121.     time.sleep(1)
  122.     keyboard.press(Key.down)
  123.     keyboard.release(Key.down)
  124.     time.sleep(1)
  125.  
  126. #we add a new colums to the left, that will contain the name of the destination
  127. pyautogui.moveTo(272, 144)
  128. time.sleep(0.5)
  129. pyautogui.click()
  130. time.sleep(0.5)
  131. keyboard.press(Key.down)
  132. keyboard.release(Key.down)
  133. keyboard.press(Key.down)
  134. keyboard.release(Key.down)
  135. keyboard.press(Key.down)
  136. keyboard.release(Key.down)
  137. keyboard.press(Key.enter)
  138. keyboard.release(Key.enter)
  139.  
  140. #go back to A1
  141. time.sleep(0.5)
  142. keyboard.press(Key.ctrl)
  143. keyboard.press(Key.up)
  144. keyboard.release(Key.up)
  145. keyboard.release(Key.ctrl)
  146.  
  147. #write all the destination names line by line
  148. time.sleep(0.5)
  149. for i in list_depart:
  150.     for destination in list_destination:
  151.         destination_long_name =  dico_corresp[destination]
  152.         keyboard.type(destination_long_name)
  153.         keyboard.press(Key.down)
  154.         keyboard.release(Key.down)
  155.         time.sleep(0.5)
  156.  
  157. #select column B and then go to the google sheets options to separate the content into multiple columns with "?" as a separator
  158. pyautogui.moveTo(197, 233)
  159. pyautogui.click()
  160. pyautogui.moveTo(393, 144)
  161. pyautogui.click()
  162. keyboard.press(Key.up)
  163. keyboard.release(Key.up)
  164. keyboard.press(Key.up)
  165. keyboard.release(Key.up)
  166. keyboard.press(Key.enter)
  167. keyboard.release(Key.enter)
  168. keyboard.press(Key.enter)
  169. keyboard.release(Key.enter)
  170. keyboard.press(Key.up)
  171. keyboard.release(Key.up)
  172. keyboard.press(Key.enter)
  173. keyboard.release(Key.enter)
  174. time.sleep(0.5)
  175. keyboard.press('?')
  176. keyboard.release('?')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement