-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathved.py
More file actions
executable file
·91 lines (76 loc) · 2.11 KB
/
Copy pathved.py
File metadata and controls
executable file
·91 lines (76 loc) · 2.11 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/python
# ugly path patching
import sys
import os
sys.path.append(os.path.abspath(os.path.join(__file__, '..', 'lib')))
import base64
from proto.ved_pb2 import Ved
'''
The type of link encoded in the ved message. If you find out, what other values mean,
please either send me a pull request or comment in the article
(http://gqs-decoder.blogspot.com/2013/08/google-referrer-query-strings-debunked-part-1.html)
'''
LINK_TYPES = {
22 : 'web',
245 : 'image thumbnail',
429 : 'image',
311 : 'video',
312 : 'video thumbnail',
341 : 'related search',
1617 : 'advertisement',
2459 : 'knowledge sidebar link',
3836 : 'knowledge sidebar image',
3838 : 'knowledge sidebar image small',
3849 : 'knowledge sidebar "more images"'
}
def try_decode(s):
''' try to base64 decode s. return None, if decoding fails '''
try:
return base64.b64decode(str(s)+'=====', '_-')
except TypeError:
return None
def decode_ved_plain(s):
''' decode the plain text varian of the ved parameter. no error checking. '''
key_mapping = {'i':'index_boost', 't':'type', 'r':'result_position', 's':'start'}
kv_pairs = s.split(',')
kv_pairs = map(lambda x: x.split(':'), kv_pairs)
kv_pairs = map(lambda (k,v): (key_mapping[k], int(v)), kv_pairs)
return dict(kv_pairs)
def decode_ved_protobuf(s):
''' decode the protobuf variant of the ved parameter. '''
decoded = try_decode(s)
if not decoded:
return None
ved = Ved()
try:
ved.ParseFromString(decoded)
ret = {}
for k,v in ved.ListFields():
ret[k.name] = v
return ret
except DecodeError:
return None
def decode_ved(s):
''' decode a ved '''
if not s:
return None
if s[0] == '1': #TODO: decode plain text variant
return decode_ved_plain(s[1:])
elif s[0] == '0':
return decode_ved_protobuf(s[1:])
def format_type(type):
type_name = LINK_TYPES.get(type, 'unknown')
return '%s (%s)' % (type_name, type)
def format_ved(ved):
if 'type' in ved:
ved['type'] = format_type(ved['type'])
return ved
def main():
import sys
for line in sys.stdin:
line = line.strip()
if not line:
continue
print format_ved(decode_ved(line))
if __name__ == '__main__':
main()