コード例 #1
0
ファイル: gnsync.py プロジェクト: dprabah/geeknote
def all_linked_notebooks():
    geeknote = GeekNote()
    return geeknote.findLinkedNotebooks()
コード例 #2
0
ファイル: gnsyncm.py プロジェクト: freyp567/geeknote
def all_notebooks(sleep_on_ratelimit=False):
    geeknote = GeekNote(sleepOnRateLimit=sleep_on_ratelimit)
    return [notebook for notebook in geeknote.findNotebooks()]
コード例 #3
0
ファイル: gnsync.py プロジェクト: dprabah/geeknote
def main():
    try:
        parser = argparse.ArgumentParser()
        parser.add_argument("--path",
                            "-p",
                            action="store",
                            help="Path to synchronize directory")
        parser.add_argument(
            "--mask",
            "-m",
            action="store",
            help='Mask of files to synchronize. Default is "*.*"',
        )
        parser.add_argument(
            "--format",
            "-f",
            action="store",
            default="plain",
            choices=["plain", "markdown", "html"],
            help=
            'The format of the file contents. Default is "plain". Valid values are "plain" "html" and "markdown"',
        )
        parser.add_argument(
            "--notebook",
            "-n",
            action="store",
            help=
            "Notebook name for synchronize. Default is default notebook unless all is selected",
        )
        parser.add_argument(
            "--all",
            "-a",
            action="store_true",
            help="Synchronize all notebooks",
            default=False,
        )
        parser.add_argument("--all-linked",
                            action="store_true",
                            help="Get all linked notebooks")
        parser.add_argument(
            "--logpath",
            "-l",
            action="store",
            help="Path to log file. Default is GeekNoteSync in home dir",
        )
        parser.add_argument(
            "--two-way",
            "-t",
            action="store_true",
            help="Two-way sync (also download from evernote)",
            default=False,
        )
        parser.add_argument(
            "--download-only",
            action="store_true",
            help="Only download from evernote; no upload",
            default=False,
        )
        parser.add_argument(
            "--nodownsync",
            "-d",
            action="store",
            help="Sync from Evernote only if the file is already local.",
        )
        parser.add_argument("--save-images",
                            action="store_true",
                            help="save images along with text")
        parser.add_argument(
            "--sleep-on-ratelimit",
            action="store_true",
            help="sleep on being ratelimited",
        )
        parser.add_argument(
            "--images-in-subdir",
            action="store_true",
            help=
            "save images in a subdirectory (instead of same directory as file)",
        )

        args = parser.parse_args()

        path = args.path if args.path else "."
        mask = args.mask if args.mask else None
        format = args.format if args.format else None
        notebook = args.notebook if args.notebook else None
        logpath = args.logpath if args.logpath else None
        twoway = args.two_way
        download_only = args.download_only
        nodownsync = True if args.nodownsync else False

        # image options
        imageOptions = {}
        imageOptions["saveImages"] = args.save_images
        imageOptions["imagesInSubdir"] = args.images_in_subdir

        reset_logpath(logpath)

        geeknote = GeekNote()

        if args.all_linked:
            my_map = {}
            for notebook in all_linked_notebooks():
                print "Syncing notebook: " + notebook.shareName
                notebook_url = urlparse.urlparse(notebook.noteStoreUrl)
                sharedNoteStoreClient = THttpClient.THttpClient(
                    notebook.noteStoreUrl)
                sharedNoteStoreProtocol = TBinaryProtocol.TBinaryProtocol(
                    sharedNoteStoreClient)
                sharedNoteStore = NoteStore.Client(sharedNoteStoreProtocol)

                sharedAuthResult = sharedNoteStore.authenticateToSharedNotebook(
                    notebook.shareKey, geeknote.authToken)
                sharedAuthToken = sharedAuthResult.authenticationToken
                sharedNotebook = sharedNoteStore.getSharedNotebookByAuth(
                    sharedAuthToken)

                my_filter = NoteStore.NoteFilter(
                    notebookGuid=sharedNotebook.notebookGuid)

                noteList = sharedNoteStore.findNotes(sharedAuthToken,
                                                     my_filter, 0, 10)

                print "Found " + str(noteList.totalNotes) + " shared notes."

                print noteList.notes

                filename = notebook.shareName + "-" + noteList.notes[
                    0].title + ".html"

                filename = filename.replace(" ", "-").replace("/", "-")

                content = sharedNoteStore.getNoteContent(
                    sharedAuthToken, noteList.notes[0].guid)

                with open(filename, "w") as f:
                    f.write(content)
            return

        if args.all:
            for notebook in all_notebooks(
                    sleep_on_ratelimit=args.sleep_on_ratelimit):
                logger.info("Syncing notebook %s", notebook)
                escaped_notebook_path = re.sub(os.sep, "-", notebook)
                notebook_path = os.path.join(path, escaped_notebook_path)
                if not os.path.exists(notebook_path):
                    os.mkdir(notebook_path)
                GNS = GNSync(
                    notebook,
                    notebook_path,
                    mask,
                    format,
                    twoway,
                    download_only,
                    nodownsync,
                    sleep_on_ratelimit=args.sleep_on_ratelimit,
                    imageOptions=imageOptions,
                )
                GNS.sync()
        else:
            GNS = GNSync(
                notebook,
                path,
                mask,
                format,
                twoway,
                download_only,
                nodownsync,
                sleep_on_ratelimit=args.sleep_on_ratelimit,
                imageOptions=imageOptions,
            )
            GNS.sync()

    except (KeyboardInterrupt, SystemExit, tools.ExitException):
        pass

    except Exception, e:
        logger.error(str(e))
コード例 #4
0
ファイル: gnsync.py プロジェクト: dprabah/geeknote
    def sync(self):
        """
        Synchronize files to notes
        TODO: add two way sync with meta support
        TODO: add specific notebook support
        """
        if not self.all_set:
            return

        files = self._get_files()
        notes = self._get_notes()

        if not self.download_only:
            for f in files:
                has_note = False
                meta = self._parse_meta(self._get_file_content(f["path"]))
                title = f["name"] if "title" not in meta else meta[
                    "title"].strip()
                tags = (None if "tags" not in meta else meta["tags"].replace(
                    "[", "").replace("]", "").split(","))
                tags = None if not tags else map(lambda x: x.strip(), tags)
                meta["tags"] = tags
                meta["title"] = title
                note = None

                if self.format == "html":
                    meta["mtime"] = f["mtime"]
                    note = self._html2note(meta)

                for n in notes:
                    if title == n.title:
                        has_note = True
                        if f["mtime"] > n.updated:
                            if self.format == "html":
                                gn = GeekNote(
                                    sleepOnRateLimit=self.sleep_on_ratelimit)
                                note.guid = n.guid
                                gn.getNoteStore().updateNote(
                                    gn.authToken, note)
                                logger.info('Note "{0}" was updated'.format(
                                    note.title))
                            else:
                                self._update_note(f, n, title, meta["content"],
                                                  tags)
                            break

                if not has_note:
                    if self.format == "html":
                        gn = GeekNote(sleepOnRateLimit=self.sleep_on_ratelimit)
                        gn.getNoteStore().createNote(gn.authToken, note)
                        logger.info('Note "{0}" was created'.format(
                            note.title))
                    else:
                        self._create_note(f, title, meta["content"], tags)

        if self.twoway or self.download_only:
            for n in notes:
                has_file = False
                for f in files:
                    if f["name"] == n.title:
                        has_file = True
                        if f["mtime"] < n.updated:
                            self._update_file(f, n)
                            break

                if not self.nodownsync:
                    if not has_file:
                        self._create_file(n)

        logger.info("Sync Complete")
コード例 #5
0
 def _get_notes(self):
     """
     Get notes from evernote.
     """
     keywords = 'notebook:"{0}"'.format(tools.strip(self.notebook_name))
     return GeekNote().findNotes(keywords, 10000).notes
コード例 #6
0
def all_notebooks():
    geeknote = GeekNote()
    return [notebook.name for notebook in geeknote.findNotebooks()]
コード例 #7
0
    def sync(self):
        """
        Synchronize files to notes
        TODO: add two way sync with meta support
        TODO: add specific notebook support
        """
        if not self.all_set:
            return

        files = self._get_files()
        notes = self._get_notes()

        if not self.download_only:
            for f in files:
                has_note = False
                meta = self._parse_meta(self._get_file_content(f['path']))
                title = f['name'] if 'title' not in meta else meta[
                    'title'].strip()
                tags = None if 'tags' not in meta else meta['tags'] \
                    .replace('[', '').replace(']', '').split(',')
                tags = None if not tags else map(lambda x: x.strip(), tags)
                meta['tags'] = tags
                meta['title'] = title
                note = None

                if self.format == 'html':
                    meta['mtime'] = f['mtime']
                    note = self._html2note(meta)

                for n in notes:
                    if title == n.title:
                        has_note = True
                        if f['mtime'] > n.updated:
                            if self.format == 'html':
                                gn = GeekNote()
                                note.guid = n.guid
                                gn.getNoteStore().updateNote(
                                    gn.authToken, note)
                                logger.info('Note "{0}" was updated'.format(
                                    note.title))
                            else:
                                self._update_note(f, n, title, meta['content'],
                                                  tags)
                            break

                if not has_note:
                    if self.format == 'html':
                        gn = GeekNote()
                        gn.getNoteStore().createNote(gn.authToken, note)
                        logger.info('Note "{0}" was created'.format(
                            note.title))
                    else:
                        self._create_note(f, title, meta['content'], tags)

        if self.twoway or self.download_only:
            for n in notes:
                has_file = False
                for f in files:
                    if f['name'] == n.title:
                        has_file = True
                        if f['mtime'] < n.updated:
                            self._update_file(f, n)
                            break
                        os.utime(f, (n.updated, n.updated))

                if not self.nodownsync:
                    if not has_file:
                        self._create_file(n)

        logger.info('Sync Complete')