Skip to content

Instantly share code, notes, and snippets.

@johnmiedema
Last active June 9, 2021 03:59
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save johnmiedema/a8e93ad8e92db40335c0 to your computer and use it in GitHub Desktop.
Save johnmiedema/a8e93ad8e92db40335c0 to your computer and use it in GitHub Desktop.
Redirect to a random note link from Evernote
<?php
/*
--------------------------------------------------------------------------
EVERNOTE RANDOM
Use with IFTTT.com to get a daily random evernote note sent to your email
When link is opened, view it in your Evernote app
Edit a note daily to keep up on them all
--------------------------------------------------------------------------
Requirements:
1. Get an Evernote API key https://dev.evernote.com/. Receive email with consumer key and secret key.
2. Download the Developer SDK for PHP. Upload the Developer SDK to a web host supporting PHP & decompress.
3. Modify the /sample/oauth/index.php file. Sandbox=true. Fill in your consumer key and secret key. Modify the callback url based on your web path. Load this url to get a developer oauth key.
4. Upload this script to your web host.
5. Modify the REQUIRED variables below. You must first use the sandbox before you can use production.
6. Test the script by loading it in a browser. You will get redirected to your sandbox. Your browser will prompt you about the application to open the link. Stick to the web Evernote until you get into production. The sandbox notes will not be in your real app.
7. Set up an IFTTT recipe just like the Daily Random Wikipedia recipe. After adding it, change the URL to this script page. Wait for your first Random Evernote.
8. If all is well, you can request Evernote to promote you to production. https://dev.evernote.com/doc/articles/testing.php.
9. Repeat step 3 to get a production oauth key.
10. Modify the REQUIRED variables below to point to production and use your production oauth key.
--------------------------------------------------------------------------
Alpha version 2015-03-31
John Miedema|http://johnmiedema.com|john.miedema@gmail.com
Please take, improve, share it, sell it, whatever you like.
--------------------------------------------------------------------------
*/
//***REQUIRED set first line relative to your web file path
require '../src/autoload.php';
use EDAM\Error\EDAMUserException;
use EDAM\NoteStore\NoteFilter, EDAM\NoteStore\NotesMetadataResultSpec;
session_start();
//--------------------------------------------------------------------------
//*** REQUIRED
//first use your sandbox values (per above); once working use your production values
$token = '%INSERT_SANDBOX_OAUTH_TOKEN%'; //sandbox
$sandbox = true; //sandbox or production
$service = "sandbox.evernote.com";
//$token = '%INSERT_PROD_OAUTH_TOKEN%'; //production
//$sandbox = false; //sandbox or production
//$service = "www.evernote.com";
//--------------------------------------------------------------------------
//create a client
$client = new \Evernote\AdvancedClient($token, $sandbox);
//container for results
$result = array();
//--------------------------------------------------------------------------
//*** OPTIONS
//set filters if you want them, e.g., specific notebook; not set here
$filter = new NoteFilter();
//$notebookGuid = '%INSERT_NOTEBOOK_GUID%';
//$filter->notebookGuid = $notebookGuid;
//offset
$offset = 0;
//set max notes. I set to 10000. Only minimal metadata set coming back.
$maxNotes = 10000;
//can bring back extra metadata, not needed
$spec = new NotesMetadataResultSpec();
//$spec->includeTitle = true;
//--------------------------------------------------------------------------
//get list of note guids to choose from
do {
$noteStore = $client->getNoteStore();
$notesList = $noteStore->findNotesMetadata($filter, $offset, $maxNotes, $spec);
$offset = $notesList->startIndex + count($notesList->notes);
$remain = $notesList->totalNotes - $offset;
foreach ($notesList->notes as $note) {
$result[] = $note->guid;
}
} while($remain>0);
//select random note
$noteGuid = $result[array_rand($result, 1)];
//--------------------------------------------------------------------------
//build a Note Link
$userStore = $client->getUserStore();
$user = $userStore->getUser();
$userId = $user->id;
$shardId = $user->shardId;
$url = 'https://' . $service . '/shard/' . $shardId . '/nl/' . $userId . '/' . $noteGuid . '/';
//--------------------------------------------------------------------------
//redirect IFTTT.com recipe so that the random link is emailed to you
header('Location:' . $url);
die();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment