-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsncat
executable file
·124 lines (106 loc) · 3.77 KB
/
sncat
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
#!/usr/bin/env python
# sncat: simplenote cat
# Todd Foster, begun 21 April 2012
#
# Gets email, password from ~/.sncatrc with one per line: email = [email protected]
#
#
# Starting point: http://www.leancrew.com/all-this/2010/01/exploring-the-simplenote-api/
# I completely relied on this to get what I wanted out of simplenote
# Similar idea to what I'm thinking, but in perl: http://ping13.net/hacks/snwatch/
# Another similar idea, also in perl: https://github.com/fletcher/SimplenoteSync
# Config parser lifted from: http://www.decalage.info/en/python/configparser
#
from urllib import urlopen
from base64 import b64encode
import json
import os
import sys
import pickle
COMMENT_CHAR = '#'
OPTION_CHAR = '='
def parse_config(filename):
options = {}
f = open(os.path.expanduser(filename))
for line in f:
# First, remove comments:
if COMMENT_CHAR in line:
# split on comment char, keep only the part before
line, comment = line.split(COMMENT_CHAR, 1)
# Second, find lines with an option=value:
if OPTION_CHAR in line:
# split on option char:
option, value = line.split(OPTION_CHAR, 1)
# strip spaces:
option = option.strip()
value = value.strip()
# store in dictionary:
options[option] = value
f.close()
return options
def getNoteByKey(notes, key):
for note in notes:
if note['key'] == key:
return note
return None
options = parse_config('~/.sncatrc')
if options.has_key('destdir') and options.has_key('password'):
print "Retrieving simplenote notes for " + options['email']
else:
print "email or password not specified in .sncatrc: Quitting."
sys.exit()
if options.has_key('destdir'):
print "Specified destdir = " + options['destdir']
else:
print "destdir not specified in .sncatrc: Quitting."
sys.exit()
if options.has_key('pickleFile'):
pickleFile = options['pickleFile']
destdir = os.path.expanduser(options['destdir'])
# Get my authorization token for later calls.
loginURL = 'https://simple-note.appspot.com/api/login'
creds = b64encode('email=%s&password=%s' % (options['email'], options['password']))
login = urlopen(loginURL, creds)
token = login.readline().rstrip()
login.close()
# Get the current note index.
indexURL = 'https://simple-note.appspot.com/api/index?auth=%s&email=%s' % (token, options['email'])
index = urlopen(indexURL)
newNoteList = json.load(index)
# Get the previous note index
oldNoteList = []
pickleFile = destdir + '/.notelist.pkl'
if os.path.isfile(pickleFile):
input = open(pickleFile,'rb')
oldNoteList = pickle.load(input)
input.close()
# Retrieve changed notes, delete newly deleted notes
baseURL = 'https://simple-note.appspot.com/api/note?key=%s&auth=%s&email=%s'
titles = []
for i in newNoteList:
oldNote = getNoteByKey(oldNoteList, i['key'])
# If file was already deleted, ignore
if i['deleted'] == True:
if oldNote is None: continue
if oldNote['deleted'] == True: continue
# Get info about note
noteURL = baseURL % (i['key'], token, options['email'])
note = urlopen(noteURL).read().decode('utf-8').rstrip()
encoded = '\n'.join(note.split('\n')[1:]).encode('utf-8')
title = note.split('\n')[0].rstrip()
path = destdir + '/' + title
#print '%s: %s' % (title, i)
# Only delete newly deleted notes
if i['deleted'] == True:
print 'Removing Title: %s' % (title)
if os.path.isfile(path): os.remove(path)
elif oldNote is None or i['modify'] > oldNote['modify'] or not os.path.isfile(path):
print 'Downloading Title: %s Modified: %s' % (title, i['modify'])
titles.append(title)
target = open (path, 'w')
print>>target, encoded
target.close()
# Save new notelist
output = open(pickleFile, 'wb')
pickle.dump(newNoteList, output)
output.close()