Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bsdz committed May 9, 2020
0 parents commit f931ef0
Show file tree
Hide file tree
Showing 7 changed files with 636 additions and 0 deletions.
59 changes: 59 additions & 0 deletions Examples.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
function example_run_sync() {
// one time code from https://my.remarkable.com/connect/mobile
let rOneTimeCode = "abcdwxyz";

// can select google folder by id or using search sdk string
//let gdFolderSearchParams = "0Xxx_0XxxxX1XXX1xXXxxXXxxx0X";
let gdFolderSearchParams = "title = 'Books' and mimeType = 'application/vnd.google-apps.folder'"

let sync = new Synchronizer(rOneTimeCode, gdFolderSearchParams, "Google Drive", ["SkipFolder1", "SkipFolder2"]);
sync.run();
}

function example_force_sync() {
// one time code from https://my.remarkable.com/connect/mobile
let rOneTimeCode = "abcdwxyz";

// can select google folder by id or using search sdk string
//let gdFolderSearchParams = "0Xxx_0XxxxX1XXX1xXXxxXXxxx0X";
let gdFolderSearchParams = "title = 'Books' and mimeType = 'application/vnd.google-apps.folder'"

const IDS_TO_FORCE = [
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx1",
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx2",
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx3",
];

const forceFunc = (r, s) => IDS_TO_FORCE.includes(r["ID"]);
// const forceFunc = (r, s) => s["Type"] == "CollectionType";
// const forceFunc = (r, s) => r["VissibleName"] == "needs_force_push.pdf";
// const forceFunc = (r, s) => s["Version"] == 1;

let sync = new Synchronizer(rOneTimeCode, gdFolderSearchParams, "Google Drive", [], forceFunc);
sync.run();
}

function example_get_document_info() {
// To re-use a cached device token initialize like this
// let sync = new Synchronizer(rOneTimeCode, gdFolderSearchParams, "Google Drive");
// let rapi = sync.rApiClient;
// otherwise use one time code from https://my.remarkable.com/connect/mobile
let rOneTimeCode = "abcdwxyz";
let rapi = new RemarkableAPI(null, rOneTimeCode);
// example doc with uuid
let docs = rapi.listDocs('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', true);
}

function example_delete_all_documents() {
// To re-use a cached device token initialize like this
// let sync = new Synchronizer(rOneTimeCode, gdFolderSearchParams, "Google Drive");
// let rapi = sync.rApiClient;
// otherwise use one time code from https://my.remarkable.com/connect/mobile
let rOneTimeCode = "abcdwxyz";
let rapi = new RemarkableAPI(null, rOneTimeCode);
let allDocs = rapi.listDocs();
// delete all except at top level
let deleteDocs = allDocs.filter((r) => r["Parent"] != "");
rapi.delete(deleteDocs);
}

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Blair Azzopardi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
12 changes: 12 additions & 0 deletions Library.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Synchronizes PDFs from folder in Google Drive with Remarkable cloud.
*
* @rOneTimeCode {string} one time code from https://my.remarkable.com/connect/mobile
* @gdFolderSearchParams {string} google folder id or google drive search sdk string
* @rRootFolder {string} root folder on Remarkable to sync files to - must already exist
* @gdSkipFolders {array} list of names of google drive folders to skip syncing
*/
function syncGoogleDriveWithRemarkableCloud(rOneTimeCode, gdFolderSearchParams, rRootFolder, gdSkipFolders=[]) {
let sync = new Synchronizer(rOneTimeCode, gdFolderSearchParams, rRootFolder, gdSkipFolders);
sync.run();
}
25 changes: 25 additions & 0 deletions License.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
MIT License
Copyright (c) [year] [fullname]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# google-drive-remarkable-sync
Apps Script library for synchronising Google Drive folder with Remarkable reader.

# Installation

1. Go to https://script.google.com and click on "New Project".
Click in menu "File/Rename" and provide suitable name, eg "Sync Google Drive Books to Remarkable".

1. Option 1 - Include library from Apps Script
Click in menu "Resources/Libraries" and in "Add a library" paste "1_ftsHelqnCqBXAwFAOv3U-WUUm_n3_nENg7n6BrDDzze7EekBD9vmf-0" without the double quotes. In version drop down choose "Stable" Then press "Save" button.

2. Option 2 - Copy the code files from this repository into your Apps Script project being careful to rename the *.js files to *.gs files.

2. Create a folder at top level in your Remarkable device called "Google Drive".

3. In your Code.gs file paste the following code:

function run_sync() {
// one time code from https://my.remarkable.com/connect/mobile
let rOneTimeCode = "abcdwxyz";
let gdFolderSearchParams = "title = 'Books' and mimeType = 'application/vnd.google-apps.folder'"
RemarkableGoogleDriveSyncLib.syncGoogleDriveWithRemarkableCloud(rOneTimeCode, gdFolderSearchParams, "Google Drive");
}

Change the rOneTimeCode to include a one time code obtained from https://my.remarkable.com/connect/mobile. Also change the name "Books" in gdFolderSearchParams to the name of your Google Drive folder that contains your relevant PDFs. You can also replace the gdFolderSearchParams with a Google Drive folder ID.

4. Click the menu "Run/Run function/run_sync"; you will be prompted for Authorization. Click Review Permissions and select your Google account. You will be prompted that the app isn't verified. Click the Advanced hyperlink and choose "Go to <Your project name> (unsafe)". Choose Allow to the permissions shown.

5. View the execution log of your project to check everything appears to be working.

6. Set up a regular trigger by click in menu "Edit/Current project's triggers". Click "+ Add Trigger" button. Choose run_sync function and select "Time-driver", "Hour timer", "Every hour" and "Notify me daily" then press Save.

That should be it!
192 changes: 192 additions & 0 deletions Remarkable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
class RemarkableAPI {

constructor(deviceToken = null, oneTimeCode = null) {
// oneTimeCode from https://my.remarkable.com/connect/mobile
if (deviceToken === null && oneTimeCode === null) {
throw "Need at least either device-token or one-time-code";
}

if (deviceToken === null) {
Logger.log("Requesting new Remarkable device token from one time code..");
this.deviceToken = this.constructor._getDeviceToken(oneTimeCode);
}
else {
Logger.log("Using existing Remarkable device token..");
this.deviceToken = deviceToken;
}

this.userToken = this.constructor._getUserToken(this.deviceToken);
this.storageHost = this.constructor._getStorageHost(this.userToken);
}

// https://github.com/splitbrain/ReMarkableAPI/wiki/Authentication

static _getDeviceToken(oneTimeCode) {
let data = {
"code": oneTimeCode, // one-time code from website
"deviceDesc": "desktop-windows",
"deviceID": "29a64bd1-9fa3-4ff4-8849-3d8d527b93f2"
};
let options = {
'method': 'post',
'contentType': 'application/json',
'payload': JSON.stringify(data)
};
// https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app
let response = UrlFetchApp.fetch('https://my.remarkable.com/token/json/2/device/new', options);
let deviceToken = response.getContentText()
//Logger.log(`Received device token: ${deviceToken}`);
return deviceToken;
}

static _getUserToken(deviceToken) {
let options = {
'method': 'post',
'contentType': 'application/json',
'payload': JSON.stringify({}),
'headers': {
'Authorization': `Bearer ${deviceToken}`
}
};
let response = UrlFetchApp.fetch('https://my.remarkable.com/token/json/2/user/new', options);
let userToken = response.getContentText()
//Logger.log(`Received user Token: ${userToken}`);
return userToken;
}

// https://github.com/splitbrain/ReMarkableAPI/wiki/Service-Discovery

static _getStorageHost(userToken) {
let options = {
'method': 'get',
'contentType': 'application/json',
'headers': {
'Authorization': `Bearer ${userToken}`
}
};
let response = UrlFetchApp.fetch('https://service-manager-production-dot-remarkable-production.appspot.com/service/json/1/document-storage?environment=production&group=auth0%7C5a68dc51cb30df3877a1d7c4&apiVer=2', options);
let text = response.getContentText()
let data = JSON.parse(text);
if (data["Status"] == "OK") {
Logger.log(`Remarkable cloud storage host: ${data["Host"]}`);
return data["Host"];
}
else {
return null;
}
}

// https://github.com/splitbrain/ReMarkableAPI/wiki/Storage

listDocs(docUuid4 = null, withBlob = null) {
Logger.log("Fetching doc list from Remarkable cloud");
let options = {
'method': 'get',
'contentType': 'application/json',
'headers': {
'Authorization': `Bearer ${this.userToken}`
}
};
let params = [];
if (docUuid4 !== null) {
params.push(`doc=${docUuid4}`);
}
if (withBlob !== null) {
params.push(`withBlob=1`);
}
let urlParams = "";
if (params.length > 0) {
urlParams = "?" + params.join("&");
}
let response = UrlFetchApp.fetch(`https://${this.storageHost}/document-storage/json/2/docs${urlParams}`, options);
let text = response.getContentText();
let data = JSON.parse(text);
return data;
}

findDocUUID(name) {
// TODO: should accept a path
let allDocs = this.listDocs();
let filteredDocs = allDocs.filter((r) => r["VissibleName"] == name);
if (filteredDocs.length > 0) {
return filteredDocs[0]["ID"];
}
else {
return null;
}
}

uploadRequest(data) {
let payloadData = data.map((r) => (
({ ID, Type, Version }) => ({ ID, Type, Version }))(r));

let options = {
'method': 'put',
'contentType': 'application/json',
'headers': {
'Authorization': `Bearer ${this.userToken}`
},
'payload': JSON.stringify(payloadData)
};
let response = UrlFetchApp.fetch(`https://${this.storageHost}/document-storage/json/2/upload/request`, options);
let text = response.getContentText()
let res = JSON.parse(text);
return res;
}

blobUpload(url, zipBlob) {
let bytes = zipBlob.getBytes();
let options = {
'method': 'put',
'contentType': "", // needs to blank!
'contentLength': bytes.length,
//'payload': bytes,
'payload': zipBlob,
//'muteHttpExceptions': true // for debugging
};
let response = UrlFetchApp.fetch(url, options);
//let response = UrlFetchApp.getRequest(url, options);

if (response.getResponseCode() != 200) {
throw "Blob upload failed.";
}
}

uploadUpdateStatus(data) {
let payloadData = data.map((r) => (
({ ID, Type, Version, Parent, VissibleName }) => ({ ID, Type, Version, Parent, VissibleName }))(r));

let options = {
'method': 'put',
'contentType': 'application/json',
'headers': {
'Authorization': `Bearer ${this.userToken}`
},
'payload': JSON.stringify(payloadData)
};
let response = UrlFetchApp.fetch(`https://${this.storageHost}/document-storage/json/2/upload/update-status`, options);
let text = response.getContentText()
let res = JSON.parse(text);
return res;
}

delete(data) {
let payloadData = data.map((r) => (
({ ID, Version }) => ({ ID, Version }))(r));

let options = {
'method': 'put',
'contentType': 'application/json',
'headers': {
'Authorization': `Bearer ${this.userToken}`
},
'payload': JSON.stringify(payloadData)
};
let response = UrlFetchApp.fetch(`https://${this.storageHost}/document-storage/json/2/delete`, options);
let text = response.getContentText()
let res = JSON.parse(text);
Logger.log(res);
return res;
}

}
Loading

0 comments on commit f931ef0

Please sign in to comment.