Jump to content

Multiple query parsing for Google Flights


Recommended Posts

Hello, all.

 

I'm trying to do a workflow that will allow me to search Google Flights by passing multiple queries to Alfred.  The URL for Google Flights looks like:

https://www.google.com/flights/#search;f=LAX;t=MAD;d=2015-06-17;r=2015-06-30

Summary:  f is the outgoing airport code, t is the destination airport code, d is the departure date, and r is the return date.  I've modified the Kitten image workflow I found elsewhere in the forums to try to use multiple queries, so my bash script looks like:

#!/bin/bash

# split the query into an argument array
IFS=',' read -a qarg <<< "{query}"

# set the x and y sizes from the array
w=${qarg[0]}
x=${qarg[1]}
y=${qarg[2]}
z=${qarg[3]}

# open the url with the two arguments
open "https://www.google.com/flights/#search;f=$w;t=$x;d=$y;r=$z"


 

 

When I run the script, though, it puts all the queries in for $w, and the other three don't get populated.  What am I doing wrong?  Any ideas?  Thanks.

Link to comment
  • 2 years later...

Try this Python script instead (be sure to set Language = /usr/bin/python):

import re
import sys
from webbrowser import open_new_tab

URL = 'https://www.google.com/flights/#search;f={};t={};d={};r={}'

query = sys.argv[1]

params = re.split(r'[^A-Za-z0-9-]+', query)

# Ensure params has 4 elements
params += (4 - len(params)) * ['']
if len(params) > 4:
    params = params[:4]

open_new_tab(URL.format(*params))

It works with spaces, commas, commas and spaces. Anything that isn't a letter, number or a dash, basically.

Edited by deanishe
Link to comment
  • 2 years later...
On 11/28/2017 at 7:01 PM, deanishe said:

Try this Python script instead (be sure to set Language = /usr/bin/python):


import re
import sys
from webbrowser import open_new_tab

URL = 'https://www.google.com/flights/#search;f={};t={};d={};r={}'

query = sys.argv[1]

params = re.split(r'[^A-Za-z0-9-]+', query)

# Ensure params has 4 elements
params += (4 - len(params)) * ['']
if len(params) > 4:
    params = params[:4]

open_new_tab(URL.format(*params))

It works with spaces, commas, commas and spaces. Anything that isn't a letter, number or a dash, basically.

 

Thanks a lot for this pointer to Python. I was able to get my custom search to work. However, I had to use Python2.

How do I make this work with Python3?

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...