-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimport_pocket.py
More file actions
executable file
·109 lines (89 loc) · 3.64 KB
/
import_pocket.py
File metadata and controls
executable file
·109 lines (89 loc) · 3.64 KB
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
from datetime import datetime
from os import path
from pocket import Pocket
# https://github.com/karlicoss/pockexport/issues/1
# https://github.com/jamietr1/obsidian-automation/blob/main/obsidian-make-daily.py#L9-L19
def get_config_value(key):
try:
with open(home_root + config_file, 'r') as f:
for line in f:
if key + "=" in line:
(_, value) = line.split("=", 1)
return value.replace('"', "").strip()
except:
print("An error occurred reading the config file. Does it exist?")
quit()
return None
def get_items(consumer_key, access_token):
p = Pocket(consumer_key, access_token)
export_full = p.get(favorite=1, state="archive", sort="oldest",
detailType="complete", annotations=1)
export = export_full[0]['list']
print(f"{len(export)} item(s) extracted from Pocket.")
return export
def export_items(items, consumer_key, access_token):
i = 0
all_info = []
for key in items:
item = items[key]
if item['resolved_title'] != '':
filename = item['resolved_title']
elif item['given_title'] != '':
filename = item['given_title']
else:
filename = f"Untitled[{i}"
i += 1
filename = filename.strip().replace('/', '--or--')
info = {
"item_id": key,
"title": filename,
"title-cs":
item['resolved_title'] if item['lang'] == "cs"
else "",
"lang": item['lang'],
"category":
' '.join(item['tags'].keys()) if item.get('tags')
else "",
"created": datetime.fromtimestamp(int(item['time_read'])).strftime('%Y-%m-%d'),
"updated": datetime.now().strftime('%Y-%m-%d'),
"sources": item['resolved_url'],
"excerpt": item['excerpt'],
"annotations":
[a['quote'] for a in item['annotations']] if item.get('annotations')
else None
}
file_path = pocket_path + "/" + filename + ".md"
if path.exists(file_path):
print(f"File {file_path} already exists, hence not overwriting.")
else:
print(f"Putting an item into file {file_path}.")
with open(file_path, 'w') as f:
f.write("---\n")
f.write(f"title: {info['title']}\n")
f.write(f"title-cs: {info['title-cs']}\n")
f.write(f"category: {info['category']}\n")
f.write("tags: [idea]\n")
f.write("season: autumn\n")
f.write(f"created: {info['created']}\n")
f.write(f"updated: {info['updated']}\n")
f.write(f"sources: {info['sources']}\n")
f.write("---\n")
if info['annotations']:
f.write("\n## Highlights\n")
for a in info['annotations']:
f.write(f"{a}\n\n")
p = Pocket(consumer_key, access_token)
p.unfavorite(info['item_id']).commit()
print("All items imported.")
return None
# Put it all together
if __name__ == "__main__":
home_root = path.dirname(path.dirname(path.abspath(__file__))) # https://stackoverflow.com/a/3430395
config_file = "/utilities/config"
pocket_root = "/_notes/_0-inbox/pocket"
pocket_path = home_root + pocket_root
consumer_key = get_config_value("consumer_key")
access_token = get_config_value("access_token")
items = get_items(consumer_key, access_token)
export_items(items, consumer_key, access_token)
print("Importing of Pocket is done.")