-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtestGoogleHandwritingAPI.py
More file actions
67 lines (49 loc) · 9.71 KB
/
testGoogleHandwritingAPI.py
File metadata and controls
67 lines (49 loc) · 9.71 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
#curl 'https://inputtools.google.com/request?ime=handwriting&app=quickdraw&dbg=1&cs=1&oe=UTF-8' -H 'Accept: */*' -H 'Referer: https://quickdraw.withgoogle.com/' -H 'Origin: https://quickdraw.withgoogle.com' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36' -H 'Content-Type: application/json' --data-binary '{"input_type":0,"requests":[{"language":"quickdraw","writing_guide":{"width":886,"height":348},"ink":[[[384,384,384,381,381,381,381,388,398,413,426,437,443,448,452,459,466,475,483,496,507,518,526,531,540,549,555,567,577,584,593,596,598,598,596,587,573,553,529,504,478,453,437,421,409,403,403],[72,79,90,102,108,118,125,131,132,131,123,117,112,107,102,102,105,108,111,116,120,125,131,134,140,144,144,144,141,137,131,125,114,98,79,60,46,39,37,36,36,39,44,51,57,59,59],[0,159,195,210,228,245,263,297,314,331,348,366,383,417,522,571,589,605,622,638,655,673,687,705,740,772,790,807,823,839,856,873,891,908,925,942,959,976,992,1010,1027,1044,1061,1078,1095,1112,1127]]]}]}' --compressed
import requests
import json
import re
import numpy as np
import pprint
###
ink = '''
[[217,218,220,222,225,227,230,233,235,238,240,241,243,246,248,250,252,254,256,258,260,263,266,268,273,275,278,281,284,288,291,295,298,303,307,311,317,320,324,329,334,340,344,348,351,353,358,360,361,362,363,363,363,363,363,363,363,363,363,363,363,363,363,364,364,364,364,365,365,365,365,365,365,365,366,366,367,367,367,368,368,369,370,371,371,373,374,376,378,382,384,388,392,395,398,401,403,406,409,413,416,418,421,422,424,424,425,425,425,425,425,425,424,423,423,422,421,419,418,415,413,410,406,403,400,398,397,397,397,397,397,398,401,406,411,417,423,432,439,447,456,464,473,482,491,498,508,517,526,534,542,550,555,559,563,566,570,572,574,576,577,579,579,579,579,579,579,579,579,579,579,579,576,573,568,564,560,556,552,550,546,543,540,539,538,537,536,534,533,530,529,525,523,521,519,516,514,512,510,508,506,504,501,498,494,490,487,483,479,474,467,461,456,449,441,433,426,422,415,411,408,404,401,398,396,392,389,385,381,376,372,370,365,362,359,358,355,353,352,350,350,349,348,348,347,347,346,346,345,345,345,345,345,345,345,345,345,345,345,345,345,345,345,345,345,345,344,344,343,343,342,341,340,340,339,338,337,335,334,333,331,330,328,326,324,322,319,317,314,311,308,306,305,302,299,297,294,292,289,286,282,279,274,271,267,263,259,255,253,250,248,246,244,242,241,239,237,234,233,231,230,229,227,226,224,223,222,221,220,219,218,217,216,216,215,215,214,213],[81,81,81,81,81,82,82,83,84,84,84,84,84,84,84,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,84,84,84,83,83,83,83,83,83,83,84,86,88,90,94,98,105,110,116,121,125,130,134,139,144,148,155,160,165,169,173,177,180,182,185,188,191,194,197,200,202,205,207,209,211,213,215,217,219,221,223,225,228,230,233,237,241,246,249,252,254,256,257,258,259,259,259,259,259,259,258,257,256,255,254,253,253,252,251,251,250,250,250,249,249,248,248,247,246,244,243,241,239,238,237,236,234,232,229,226,222,220,217,214,213,212,212,212,212,212,212,212,212,212,212,212,212,212,212,212,212,212,213,213,216,219,221,223,225,228,231,234,240,244,249,254,256,257,258,258,259,259,260,260,260,260,260,260,260,260,260,260,260,260,261,265,269,276,279,285,289,295,299,303,309,314,318,321,323,325,326,328,330,331,333,334,335,336,336,336,336,336,336,336,336,336,336,335,334,334,333,332,331,330,329,326,321,315,309,302,294,287,281,278,274,270,268,264,262,260,259,255,252,248,244,240,237,233,230,226,221,218,215,211,207,202,198,195,192,189,185,181,179,176,173,170,167,163,159,155,151,148,145,141,138,136,131,129,126,124,121,118,116,115,113,112,111,111,110,109,109,108,108,107,107,106,106,105,104,104,102,100,99,98,97,96,96,95,95,94,93,93,92,92,92,92,91,91,90,89,89,88,87,87,86,86,85,85,84,84,83,83,83,82,82,81],[0,298.4149999999995,316.1100000000001,333.21000000000004,350.43499999999995,367.8449999999998,385.3899999999999,402.145,419.91499999999996,436.98,454.2049999999999,471.57000000000016,488.4449999999997,505.8699999999999,522.6299999999997,539.5799999999999,556.5349999999999,573.6750000000002,590.8999999999996,608.3499999999999,626,642.645,659.5099999999998,676.4749999999999,693.5299999999997,710.7550000000001,728.21,745.835,763.1949999999997,780.2000000000003,797.335,814.585,831.5049999999997,848.9949999999999,866.1700000000001,883.98,901.0500000000002,918.2599999999998,935.645,953.1199999999999,969.9449999999997,987.6500000000001,1004.6950000000002,1021.8100000000004,1039.1750000000002,1056.6399999999999,1074.1,1090.8000000000002,1107.745,1124.81,1141.975,1158.98,1176.35,1193.9650000000001,1211.645,1228.3899999999999,1245.0750000000003,1262.0099999999998,1279.04,1296.2200000000003,1313.5899999999997,1331.13,1347.9099999999999,1365.705,1382.7450000000003,1399.9050000000002,1417.2099999999996,1434.685,1451.9700000000003,1468.7200000000003,1485.6800000000003,1502.6949999999997,1519.88,1537.2299999999996,1554.6849999999995,1572.2649999999994,1590.0550000000003,1607.1449999999995,1624.3550000000005,1641.7400000000007,1659.085,1675.7300000000005,1692.7200000000003,1709.4849999999997,1726.415,1743.4700000000003,1760.7050000000008,1778.1150000000007,1795.8050000000003,1812.7150000000001,1829.4099999999999,1846.2150000000001,1863.3599999999997,1880.63,1898.04,1915.5950000000003,1932.5600000000004,1949.4250000000002,1966.46,1983.3100000000004,2000.5649999999996,2017.9650000000001,2035.5349999999999,2052.3500000000004,2069.12,2086.925,2104.115,2140.33,2204.4000000000005,2238.38,2271.375,2295.2850000000008,2317.4749999999995,2374.3899999999994,2470.335,2503.4350000000004,2520.4349999999995,2537.5700000000006,2555.38,2572.8949999999995,2589.965,2607.045,2624.0599999999995,2641.1400000000003,2658.08,2675.4399999999996,2692.5600000000004,2709.215,2726.0949999999993,2742.9800000000005,2759.9050000000007,2776.8050000000003,2793.49,2810.7000000000007,2828.165,2845.7750000000005,2862.425,2880.215,2897.335,2914.6100000000006,2931.3150000000005,2948.8200000000006,2966,2983.125,3000.125,3017.2300000000005,3034.455,3051.8499999999995,3069.385,3086.38,3104.13,3121.2,3138.4399999999996,3155.8450000000003,3173.385,3190.085000000001,3207.9000000000005,3224.9849999999997,3242.1950000000006,3259.3050000000003,3276.2299999999996,3292.9799999999996,3310.26,3327.6799999999994,3344.6450000000004,3361.8450000000003,3379.1049999999996,3396.3199999999997,3412.96,3430.2,3447.3950000000004,3465.3949999999995,3482.3900000000003,3499.5299999999997,3516.795,3533.8500000000004,3551.4350000000004,3568.34,3585.1399999999994,3602.1550000000007,3619.2400000000007,3636.46,3653.885,3671.455000000001,3688.2749999999996,3728.3250000000007,3745.0600000000004,3762.045,3779.0550000000003,3796.1449999999995,3812.915,3830.3650000000007,3847.9300000000003,3865.0200000000004,3881.995000000001,3899.0550000000003,3916.250000000001,3933.6049999999996,3951.175,3968.0750000000007,3985.215,4002.2300000000005,4019.3450000000003,4036.62,4053.96,4071.5700000000006,4088.920000000001,4105.93,4122.960000000001,4140.155,4157.515,4175.035,4192.185,4209.455,4226.450000000001,4243.620000000001,4260.615000000001,4277.96,4295.540000000001,4312.39,4330.165,4347.24,4364.55,4381.849999999999,4398.625,4415.535,4432.24,4450.045,4467.14,4484.3550000000005,4501.3550000000005,4518.595,4536,4553.535,4570.295,4588.095,4605.205000000001,4622.465,4639.825,4657.380000000001,4674.05,4691.29,4708.28,4725.39,4742.655,4760.075000000001,4777.225,4793.87,4811.67,4828.635,4845.455,4862.080000000001,4879.4400000000005,4896.210000000001,4913.13,4929.945000000001,4946.7300000000005,4963.58,4980.635,4997.805,5015.135,5032.570000000001,5049.975,5067.035000000001,5084.045,5101.125,5117.85,5135.205000000001,5152.68,5170.145,5187,5203.970000000001,5221.01,5238.255,5255.610000000001,5273.18,5289.945000000001,5307.715000000001,5324.79,5341.925,5359.26,5376.025000000001,5393.210000000001,5409.975,5426.885000000001,5443.8,5460.79,5478.000000000001,5495.195,5512.660000000001,5529.970000000001,5546.66,5563.340000000001,5580.34,5597.65,5614.6900000000005,5631.855000000001,5649.195,5666.6849999999995,5683.975,5701.01,5718,5735.08,5752.295,5769.610000000001,5787.1,5803.995000000001,5821.719999999999,5838.785,5855.965,5873.289999999999,5891.07,5907.945,5924.705,5941.66,5959.380000000001,5976.110000000001,5993.540000000001,6010.15,6027.345000000001,6044.755000000001,6061.665000000001,6078.65,6095.825000000001,6113.08,6129.880000000001,6146.844999999999,6165.424999999999,6182.425000000001,6199.530000000001,6221.545,6238.550000000001,6269.58,6293.455,6310.1849999999995,6327.92,6345.725,6363.1849999999995]]
'''
###
def getGoogleHandwritingAPIResults(app_language):
url = 'https://inputtools.google.com/request?ime=handwriting&app=' + app_language + '&dbg=1&cs=1&oe=UTF-8'
headers = {
'Accept': '*/*',
'Referer': 'https://quickdraw.withgoogle.com/',
'Origin': 'https://quickdraw.withgoogle.com',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36',
'Content-Type': 'application/json'
}
data = '{"input_type":0,"requests":[{"language":"' + app_language + '","writing_guide":{"width":886,"height":348},"ink":[' + ink + ']}]}'
r = requests.post(url, headers=headers, data=data)
d = json.loads(r.content)
return d
def convertData(d):
di = d[1][0][3]['debug_info']
result = re.search('SCORESINKS: (.*)]]', di)
scores = json.loads(result.group(1)+ ']]')
a = np.array(scores)
x = a[:,0]
y = a[:,1].astype(np.float)
return x,y
d_qd = getGoogleHandwritingAPIResults('quickdraw')
d_ad = getGoogleHandwritingAPIResults('autodraw')
x_qd,y_qd = convertData(d_qd)
x_ad,y_ad = convertData(d_ad)
scores = {}
for ii in range(0,len(x_qd)):
xi_qd = x_qd[ii]
yi_qd = y_qd[ii]
scores[xi_qd] = [yi_qd,0]
for ii in range(0,len(x_ad)):
xi_ad = x_ad[ii]
yi_ad = y_ad[ii]
if xi_ad in scores:
scores[xi_ad][1] = yi_ad
else:
scores[xi_ad] = [0,yi_ad]
pprint.pprint(scores)