-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
103 lines (95 loc) · 4.14 KB
/
index.html
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
<html>
<body>
<script src="https://apis.google.com/js/api.js"></script>
<script src="apikey.js"></script>
<pre id="pre"></pre>
<script>
function start() {
// 2. Initialize the JavaScript client library.
gapi.client.init({
'apiKey': app.apiKey,
// clientId and scope are optional if auth is not required.
'clientId': app.clientId,
'scope': 'https://www.googleapis.com/auth/photoslibrary',
}).then(function () {
// Listen for sign-in state changes.
gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
// Handle the initial sign-in state.
updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
});
};
// 1. Load the JavaScript client library.
gapi.load('client:auth2', start);
function updateSigninStatus(isSignedIn) {
// When signin status changes, this function is called.
// If the signin status is changed to signedIn, we make an API call.
if (isSignedIn) {
fetchPhotos();
} else {
gapi.auth2.getAuthInstance().signIn();
}
}
function sendRequest(pageToken) {
return gapi.client.request({
'path': 'https://photoslibrary.googleapis.com/v1/mediaItems:search',
'method': 'POST',
'body': {
pageSize: 100,
pageToken: pageToken,
"filters": {
"mediaTypeFilter": {
"mediaTypes": ["PHOTO"]
},
"dateFilter": {
"ranges": {
"startDate": {
"year": "2016",
"month": "7",
"day": "1"
},
"endDate": {
"year": "2017",
"month": "1",
"day": "1"
}
}
}
}
}
});
}
let totalCount = 0;
let duplicateCount = 0;
function processResponse(response) {
console.log(response);
let log = document.getElementById("pre").innerText;
let previousItem = null;
response.result.mediaItems.forEach(item => {
if (previousItem) {
pn = previousItem.filename.replace("_small.jpg", ".jpg");
cn = item.filename.replace("_small.jpg", ".jpg");
if (pn == cn &&
previousItem.mediaMetadata.creationTime == item.mediaMetadata.creationTime) {
// duplication found. find the smaller resolution version
let small = parseInt(item.mediaMetadata.width) < parseInt(previousItem.mediaMetadata.width) ? item : previousItem;
let other = parseInt(item.mediaMetadata.width) < parseInt(previousItem.mediaMetadata.width) ? previousItem : item;
if (parseInt(small.mediaMetadata.width) < parseInt(other.mediaMetadata.width)) { // we could decide by file size here, but that doesn't seem to be available here
log += JSON.stringify(small) + ";\n";
duplicateCount ++;
}
}
}
previousItem = item;
});
document.getElementById("pre").innerText = log;
totalCount += response.result.mediaItems.length;
if (duplicateCount < 100 && response.result.nextPageToken) {
sendRequest(response.result.nextPageToken).then(processResponse);
}
}
function fetchPhotos() {
sendRequest(null).then(processResponse);
}
</script>
</body>
</html>