-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands.py
92 lines (64 loc) · 2.38 KB
/
commands.py
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
import sys
import requests
from datetime import datetime
from abc import ABC, abstractmethod
from persistence import BookmarkDatabase
persistence = BookmarkDatabase()
class Command(ABC):
@abstractmethod
def execute(self, data):
pass
class AddBookmarkCommand(Command):
def execute(self, data, timestamp=None):
data['date_added'] = timestamp or datetime.utcnow().isoformat()
persistence.create(data)
return True, None
class ListBookmarksCommand(Command):
def __init__(self, order_by='date_added'):
self.order_by = order_by
def execute(self, data=None):
return True, persistence.list(order_by=self.order_by)
class DeleteBookmarkCommand(Command):
def execute(self, data):
persistence.delete(data)
return True, None
class EditBookmarkCommand(Command):
def execute(self, data):
persistence.edit(data)
return True, None
class ImportGithubStarsCommand(Command):
def _extract_bookmark_info(self, repo):
return {
'title': repo.get('name'),
'url': repo.get('html_url'),
'notes': repo.get('description'),
}
def execute(self, data):
bookmarks_imported = 0
github_username = data.get('github_username')
next_page_of_results = \
f'https://api.github.com/users/{github_username}/starred'
while next_page_of_results:
print('Loading')
stars_response = requests.get(
next_page_of_results,
headers={'Accept': 'application/vnd.github.v3.star+json'},
)
next_page_of_results = \
stars_response.links.get('next', {}).get('url')
for repo_info in stars_response.json():
repo = repo_info.get('repo')
if data.get('preserve_timestamp'):
timestamp = datetime.strptime(
repo_info.get('starred_at'), '%Y-%m-%dT%H:%M:%SZ')
else:
timestamp = None
bookmarks_imported += 1
AddBookmarkCommand().execute(
self._extract_bookmark_info(repo),
timestamp=timestamp,
)
return True, f'Imported {bookmarks_imported} bookmarks from starred repos!'
class QuitCommand(Command):
def execute(self, data=None):
sys.exit()