Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
solos committed Jun 2, 2014
2 parents 717df90 + b7857e8 commit f95f4d6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
45 changes: 34 additions & 11 deletions linote.py
@@ -1,6 +1,18 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Linote.
Usage:
linote.py sync
linote.py --version
Options:
-h --help Show this screen.
--version Show version.
"""

import os
import sys
from path import path
Expand All @@ -10,14 +22,17 @@
sys.path.append(PROJECT_ROOT)
sys.path.append(PROJECT_CONFIG)


import re
import time
import local
import config
import encoding
import cPickle as pickle
import markdown
import lxml.html
import lxml.html.clean
from docopt import docopt
from logger import logger
from docutils.core import publish_parts
import evernote.edam.error.ttypes as Errors
Expand All @@ -40,7 +55,8 @@ def wrapper(self, *args, **kwargs):
if e.errorCode == Errors.EDAMErrorCode.RATE_LIMIT_REACHED:
logger.info("Rate limit reached, Retry your request in %d "
"seconds" % e.rateLimitDuration)
return None
logger.info('sleep %s' % e.rateLimitDuration)
time.sleep(int(e.rateLimitDuration) + 10)
except Exception, e:
logger.error(e)
return wrapper
Expand Down Expand Up @@ -159,17 +175,20 @@ def clean(self, content):
content = self.clean_note(content)
return content.encode('utf8')

@check_rate_limit
def process(self, note, subdir):
_id = note.guid
def need_to_sync(self, note):
_updated = note.updated / 1000
try:
local_updated = self.local_files[_id]['mtime']
local_updated = self.local_files[note.guid]['mtime']
except KeyError:
local_updated = 0
if _updated <= local_updated:
return
logger.info('sync %s %s' % (_id, note.title))
return False
else:
return True

@check_rate_limit
def process(self, note, subdir):
logger.info('sync %s %s' % (note.guid, note.title))
ntitle = note.title.replace('/', '-')
title = ntitle if len(ntitle) < 200 else ntitle[:200]

Expand All @@ -191,7 +210,7 @@ def process(self, note, subdir):
path(filename).open("w").write(content)

def sync(self):
self.local_files = local.gen_filelist()
self.local_files = local.gen_filelist(self.notedir)
notebooks = self.getNotebooks()
if not notebooks:
return
Expand All @@ -205,7 +224,8 @@ def sync(self):
if not notes:
return
for note in notes:
self.process(note, subdir)
if self.need_to_sync(note):
self.process(note, subdir)

def make_mdnote(self, md_source):
source_segment = '''<div style="display:none">%s</div>''' % md_source
Expand Down Expand Up @@ -256,7 +276,7 @@ def search_filename(self, keywords):
files = pickle.loads(
open(self.cachefile).read())
except Exception:
files = local.gen_filelist()
files = local.gen_filelist(self.notedir)

related = []
for _id in files:
Expand Down Expand Up @@ -300,7 +320,10 @@ def search_content(self, keywords):
def run():
ln = Linote(config.linote_config.get('linote.dev_token'),
config.linote_config.get('linote.notestoreurl'))
ln.sync()
arguments = docopt(__doc__, version=__version__)
if arguments['sync']:
ln.sync()


if __name__ == '__main__':
run()
15 changes: 7 additions & 8 deletions local.py
Expand Up @@ -2,26 +2,23 @@
# -*- coding: utf-8 -*-

import os
import stat
import cPickle as pickle
from path import path

local_files = {}

PROJECT_ROOT = path(__file__).dirname().abspath()


def gen_filelist():
notes_path = path(PROJECT_ROOT).joinpath('notes')
def gen_filelist(notedir):
notes_path = path(notedir)
home_path = path('%s' % os.environ['HOME'])
if notes_path.exists():
for pathname in notes_path.walkfiles():
statfile(pathname)
files = pickle.dumps(local_files)
lndir = home_path.joinpath('.linote')
cachefile = home_path.joinpath('.caches')
cachefile = home_path.joinpath('.caches').encode('utf8')
path(lndir).mkdir_p()
path(cachefile).write_text(unicode(files))
path(cachefile).write_text(files)
return local_files


Expand All @@ -44,6 +41,8 @@ def statfile(file):
}

if __name__ == '__main__':
filelist = gen_filelist()
import config
notedir = config.linote_config.get('linote.notedir')
filelist = gen_filelist(notedir)
for i in filelist:
print filelist[i]['file']
1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -4,3 +4,4 @@ markdown
docutils
path.py
kaptan
docopt

0 comments on commit f95f4d6

Please sign in to comment.