Created
July 4, 2016 12:43
-
-
Save nipotan/e9e13669180301f555efad8452028686 to your computer and use it in GitHub Desktop.
evernote 2 simplenote without app_key
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use utf8; | |
use DBI; | |
use Encode; | |
use File::Spec; | |
use Config::Pit; | |
use Log::Any::Adapter ('Stdout'); | |
use WebService::Simplenote; | |
use WebService::Simplenote::Note; | |
use Data::Dumper; | |
our $EvernoteUserId; | |
# Evernote のデータが保存されてるディレクトリ | |
our $Basedir = File::Spec->catfile( | |
$ENV{HOME}, | |
qw(Library Containers com.evernote.Evernote Data Library), | |
'Application Support', | |
qw(com.evernote.Evernote accounts www.evernote.com), | |
); | |
if (! $EvernoteUserId) { | |
opendir my $dh, $Basedir or die "$Basedir: $!"; | |
my @dirs = grep { ! /^(?:\..*|props)$/ } readdir($dh); | |
closedir $dh; | |
if (@dirs == 1) { | |
$EvernoteUserId = $dirs[0]; | |
} | |
else { | |
die "Cannot determine your Evernote user id. Set \$EvernoteUserId.\n"; | |
} | |
} | |
$Basedir = File::Spec->catfile($Basedir, $EvernoteUserId); | |
# Simplenote の設定 | |
my $sn_config = pit_get('simplenote.com', require => +{ | |
email => '[email protected]', | |
password => 'kogaidan', | |
}); | |
my $sn = WebService::Simplenote->new(%$sn_config); | |
# Mac に保存されてる Evernote の全ノートを取得 | |
my $notes = get_notes(); | |
for my $note (@$notes) { | |
# Simplenote 用のオブジェクト | |
my $note_obj = WebService::Simplenote::Note->new( | |
content => $note->{content}, | |
); | |
$note_obj->tags($note->{tags}); | |
$note_obj->deleted(0); # XXX | |
# XXX これがどうやっても Simplenote の Trash に保存されるので | |
# put された後に自分で Trash から移してください… | |
$sn->put_note($note_obj); | |
} | |
sub get_notes { | |
my $dbh = dbh(); | |
my @notes = (); | |
# 保存されてるノートの情報を取得 | |
my $note_sth = $dbh->prepare( | |
'SELECT Z_PK, ZTITLE, ZLOCALUUID FROM ZENNOTE ORDER BY ZDATEUPDATED ASC' | |
); | |
$note_sth->execute; | |
while (my($note_id, $title, $uuid) = $note_sth->fetchrow_array) { | |
warn qq|reading data from [$uuid]\n|; | |
# タグを検索 | |
my @tags = (); | |
my $tags_sth = | |
$dbh->prepare('SELECT Z_14TAGS FROM Z_5TAGS WHERE Z_5NOTES = ?'); | |
$tags_sth->execute($note_id); | |
while (my($tag_id) = $tags_sth->fetchrow_array) { | |
my($tag) = $dbh->selectrow_array( | |
'SELECT ZNORMALIZEDNAME FROM ZENTAG WHERE Z_PK = ?', | |
+{}, | |
$tag_id, | |
); | |
push @tags, $tag; | |
} | |
my $note_content = get_note_from_uuid($uuid); | |
push @notes, +{ | |
content => | |
join("\n", map { decode('utf-8', $_) } ($title, $note_content)), | |
tags => [ map { decode('utf-8', $_) } @tags ], | |
}; | |
} | |
return \@notes; | |
} | |
sub get_note_from_uuid { | |
my $uuid = shift; | |
# uuid のディレクトリ以下の content.enml から textutil でテキスト抽出 | |
my $file = "$Basedir/content/$uuid/content.enml"; | |
my @command = ( | |
'/usr/bin/textutil', | |
'-convert', 'txt', | |
'-inputencoding', 'UTF-8', | |
'-stdout', | |
'-format', 'html', | |
'-noload', | |
quotemeta($file), | |
); | |
my $textnote = `@command`; | |
return $textnote; | |
} | |
{ | |
my $dbh; | |
sub dbh { | |
return $dbh if $dbh; | |
return $dbh = | |
DBI->connect("dbi:SQLite:dbname=$Basedir/LocalNoteStore.sqlite"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment