-
Notifications
You must be signed in to change notification settings - Fork 0
/
gsvu.sh
executable file
·201 lines (136 loc) · 5.26 KB
/
gsvu.sh
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/bin/bash
# $1 - image folder '/data/mapillary/test/'
api_key='AIzaSyBV1HvfE3nQuJqBiW1a_dZhZyhPEkWpIJw'
api_key='AIzaSyBCuk_HvkTYs4LroURcKUnoY5NOBmh_ScQ' #panotrolleway
scriptdir=$(dirname "$0")
secret_file=$(find $scriptdir -name "client_secret*.json" | head -n 1)
client_id=$(cat $secret_file | jq '.installed.client_id')
client_secret=$(cat $secret_file | jq '.installed.client_secret')
scope='https://www.googleapis.com/auth/streetviewpublish'
#strip quotes
client_id="${client_id%\"}"
client_id="${client_id#\"}"
client_secret="${client_secret%\"}"
client_secret="${client_secret#\"}"
# Client id from Google Developer console
# Client Secret from Google Developer console
# Scope this is a space seprated list of the scopes of access you are requesting.
# Authorization link. Place this in a browser and copy the code that is returned after you accept the scopes.
echo 'Upload panoramic photos to Google Street View'
echo
echo 'Open browser, and copy code'
echo
echo 'https://accounts.google.com/o/oauth2/auth?client_id='$client_id'&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope='$scope'&response_type=code'
echo
read -p "Enter authorization code: " auth_code
echo
echo 'Exchange Authorization code for an access token and a refresh token.'
data="code=$auth_code&client_id=$client_id&client_secret=$client_secret&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code"
exchange_result="$(curl -s \
--request POST \
--data $data \
https://accounts.google.com/o/oauth2/token)"
access_token=$(echo $exchange_result | jq '.access_token')
refresh_token=$(echo $exchange_result | jq '.refresh_token')
#strip quotes
access_token="${access_token%\"}"
access_token="${access_token#\"}"
refresh_token="${refresh_token%\"}"
refresh_token="${refresh_token#\"}"
function get_data_json {
# $1 - path to jpg
# 2 - upload_url
url=$2
json_data=$(exiftool -q -q -json -n -gpslatitude -gpslongitude -gpsimgdirection -datetimeoriginal $1)
lat=$(jq -n "$json_data" | jq '.[].GPSLatitude' )
lon=$(jq -n "$json_data" | jq '.[].GPSLongitude' )
dir=$(jq -n "$json_data" | jq '.[].GPSImgDirection' )
tme=$(jq -n "$json_data" | jq '.[].DateTimeOriginal' | tr -d \" )
tme=${tme:0:4}${tme:5:2}${tme:8:2}${tme:10:9} #simple convert datetime for convert to unixtime
tme=$(date --date "$tme" +%s)
TEMPLATE='{"uploadReference":{"uploadUrl": "%s"
},"pose":{"heading": %s,
"latLngPair":
{ "latitude": %s,
"longitude": %s
}}, "captureTime":{"seconds": %s},}'
exp_json=$(printf "$TEMPLATE" "$url" "$dir" "$lat" "$lon" "$tme" )
exp_json=$(jq -n "$exp_json")
echo $exp_json
}
dir=$1
dir=$(realpath $dir)
uploaded_dir=$dir/'uploaded2gsv'
mkdir $uploaded_dir
#progress count
total=$(ls $dir/*.JPG | wc -l)
counter=0
#start timer
time1=$(date +%s)
for filename in $dir/*.JPG
do
echo $filename
#filename='/data/mapillary/test/IMG_20200627_190002_3566343.JPG'
#get upload URL
upload_url_json="$(curl -s --request POST \
--url "https://streetviewpublish.googleapis.com/v1/photo:startUpload?key=${api_key}" \
--header "Authorization: Bearer ${access_token}" \
--header 'Content-Length: 0')"
upload_url=$(jq -n "$upload_url_json" | jq '.uploadUrl')
#strip quotes
upload_url="${upload_url%\"}"
upload_url="${upload_url#\"}"
#post image
echo 'post image'
post_image_response="$(curl --request POST \
--url "${upload_url}" \
--upload-file "${filename}" \
--header "Authorization: Bearer ${access_token}" \
)"
echo "make metadata"
image_metadata_json=$(get_data_json $filename $upload_url)
#post image metadata
exec 3>&1
# Run curl in a separate command, capturing output of -w "%{http_code}" into HTTP_STATUS
# and sending the content to this command's STDOUT with -o >(cat >&3)
HTTP_STATUS="$(curl -w "%{http_code}" -o >(cat >&3) --request POST \
--url "https://streetviewpublish.googleapis.com/v1/photo?key=${api_key}" \
--header "Authorization: Bearer ${access_token}" \
--header 'Content-Type: application/json' \
--data "${image_metadata_json}"
)"
if [ "$HTTP_STATUS" != 200 ]; then
echo $filename 'STATUS='$HTTP_STATUS
exit 0
fi
#echo $post_metadata_response
mv $filename $uploaded_dir/$(basename $filename)
counter=$((counter+1))
echo $counter/$total
#timer tick
time2=$(date +%s)
timedelta=$((time2-time1))
#check for timer event
#compare numbers in arichmetic context
if (($timedelta > 60*40 )) ; then
echo 'timer signal';
time1=$(date +%s);
# Exchange a refresh token for a new access token.
#curl \
#--request POST \
#--data 'client_id=[Application Client Id]&client_secret=[Application Client Secret]&refresh_token=[Refresh token granted by second step]&grant_type=refresh_token' \
#https://accounts.google.com/o/oauth2/token
echo 'refresh token'
data="client_id=$client_id&client_secret=$client_secret&refresh_token=$refresh_token&grant_type=refresh_token"
echo $data
exchange_result="$(curl -s \
--request POST \
--data $data \
https://accounts.google.com/o/oauth2/token)"
echo $exchange_result | jq '.'
access_token=$(echo $exchange_result | jq '.access_token')
#strip quotes
access_token="${access_token%\"}"
access_token="${access_token#\"}"
fi
done