-
Notifications
You must be signed in to change notification settings - Fork 16
/
url.py
63 lines (49 loc) · 1.56 KB
/
url.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from collections import OrderedDict
from moz_sql_parser import parse as parse_sql
import pyparsing
import re
from six.moves.urllib import parse
from gsheetsdb.exceptions import ProgrammingError
FROM_REGEX = re.compile(' from ("http.*?")', re.IGNORECASE)
def get_url(url, headers=0, gid=0, sheet=None):
parts = parse.urlparse(url)
if parts.path.endswith('/edit'):
path = parts.path[:-len('/edit')]
else:
path = parts.path
path = '/'.join((path.rstrip('/'), 'gviz/tq'))
qs = parse.parse_qs(parts.query)
if 'headers' in qs:
headers = int(qs['headers'][-1])
if 'gid' in qs:
gid = qs['gid'][-1]
if 'sheet' in qs:
sheet = qs['sheet'][-1]
if parts.fragment.startswith('gid='):
gid = parts.fragment[len('gid='):]
args = OrderedDict()
if headers > 0:
args['headers'] = headers
if sheet is not None:
args['sheet'] = sheet
else:
args['gid'] = gid
params = parse.urlencode(args)
return parse.urlunparse(
(parts.scheme, parts.netloc, path, None, params, None))
def extract_url(sql):
try:
url = parse_sql(sql)['from']
except pyparsing.ParseException:
# fallback to regex to extract from
match = FROM_REGEX.search(sql)
if match:
return match.group(1).strip('"')
return
while isinstance(url, dict):
url = url['value']['from']
return url