kiwiz / gkeepapi

An unofficial client for the Google Keep API.
MIT License
1.54k stars 114 forks source link

Sorting all notes by date created #84

Closed ChocoTonic closed 4 years ago

ChocoTonic commented 4 years ago

I recently did a lot of overhaul in my notes collection by adding labels in bulk, but it looks like it's sorting by last modified and the order is now jumbled.

is there some code floating around that would allow me to do that?

kiwiz commented 4 years ago

You can try using Python's sort() method with the node.timestamps.created field.

ChocoTonic commented 4 years ago

I did the same test as before. The code below works completely with my near empty account and it looks like it works once then fails every time after in the loop with my main account with 3k+ notes.

What it does is change the datetime object to seconds elapsed from epoch date then sort it based on this suggestion: https://github.com/kiwiz/gkeepapi/issues/84#issuecomment-627098731

Here is the code I tried:

# https://gkeepapi.readthedocs.io/en/latest/#id1

import sys
from plyer import notification
import keyring
import gkeepapi
from time import sleep
import json
import datetime
import itertools

def sync():
    try:
        print("syncing...")
        keep.sync()
    except gkeepapi.exception.ParseException as e:
        print(e.raw)

# Credentials
# username = "xxxx@gmail.com"
# password = "xxx"
username = "xxx@gmail.com"
password = "xxx"

keep = gkeepapi.Keep()

print("logging in...")
keep.login(username, password)
print("logged in...")

# gnotes = keep.all()
gnotes = keep.find(pinned=False, archived=False, trashed=False)

# sort by timestamp in seconds

for note in itertools.islice(gnotes, 10):
    asdf = note.timestamps.created
    asdf = asdf.timestamp()
    asdf = int(asdf)
    print(asdf)
    note.sort = asdf
    sync()

# script complete notification
sync()
notification.notify("VSC", "Script Complete! :)", timeout=2)

This is the output I get using the try except code containing the sync() you suggested here: https://github.com/kiwiz/gkeepapi/issues/85#issuecomment-627768017

logging in...
logged in...
1589277028
syncing...
{'kind': 'notes#timestamps', 'updated': '2020-05-13T10:03:55.647Z', 'trashed': '1970-01-01T00:00:00.000Z', 'userEdited': '2020-05-12T02:50:28.336Z'}
1589378845
syncing...
{'kind': 'notes#timestamps', 'updated': '2020-05-13T10:03:56.204Z', 'trashed': '1970-01-01T00:00:00.000Z', 'userEdited': '2020-05-12T02:50:28.336Z'}
1588431399
syncing...
{'kind': 'notes#timestamps', 'updated': '2020-05-13T10:03:56.204Z', 'trashed': '1970-01-01T00:00:00.000Z', 'userEdited': '2020-05-12T02:50:28.336Z'}
1588325839
syncing...
{'kind': 'notes#timestamps', 'updated': '2020-05-13T10:03:56.204Z', 'trashed': '1970-01-01T00:00:00.000Z', 'userEdited': '2020-05-12T02:50:28.336Z'}
1588278499
syncing...
{'kind': 'notes#timestamps', 'updated': '2020-05-13T10:03:56.204Z', 'trashed': '1970-01-01T00:00:00.000Z', 'userEdited': '2020-05-12T02:50:28.336Z'}
1588262156
syncing...
{'kind': 'notes#timestamps', 'updated': '2020-05-13T10:03:56.204Z', 'trashed': '1970-01-01T00:00:00.000Z', 'userEdited': '2020-05-12T02:50:28.336Z'}
1587696813
syncing...
{'kind': 'notes#timestamps', 'updated': '2020-05-13T10:03:56.204Z', 'trashed': '1970-01-01T00:00:00.000Z', 'userEdited': '2020-05-12T02:50:28.336Z'}
1587671102
syncing...
{'kind': 'notes#timestamps', 'updated': '2020-05-13T10:03:56.204Z', 'trashed': '1970-01-01T00:00:00.000Z', 'userEdited': '2020-05-12T02:50:28.336Z'}
1587580030
syncing...
{'kind': 'notes#timestamps', 'updated': '2020-05-13T10:03:56.204Z', 'trashed': '1970-01-01T00:00:00.000Z', 'userEdited': '2020-05-12T02:50:28.336Z'}
1587578198
syncing...
{'kind': 'notes#timestamps', 'updated': '2020-05-13T10:03:56.204Z', 'trashed': '1970-01-01T00:00:00.000Z', 'userEdited': '2020-05-12T02:50:28.336Z'}
syncing...
{'kind': 'notes#timestamps', 'updated': '2020-05-13T10:03:56.204Z', 'trashed': '1970-01-01T00:00:00.000Z', 'userEdited': '2020-05-12T02:50:28.336Z'}
kiwiz commented 4 years ago

This should hopefully be resolved now that #85 is fixed. Small note: you don't need to call sync() on every iteration of the loop. Doing it once at the end should work just fine.

ChocoTonic commented 4 years ago

noted. Thanks!