-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup-feedly.py
More file actions
executable file
·129 lines (92 loc) · 3.68 KB
/
backup-feedly.py
File metadata and controls
executable file
·129 lines (92 loc) · 3.68 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/python3
import requests
import os
import json
import time
import argparse
SILENT=True
def get_opml(access_token):
headers = {"Authorization" : "OAuth " + access_token}
if not SILENT:
print("Fetching OPML file...")
r = requests.get("https://cloud.feedly.com/v3/opml", headers = headers)
if r.status_code == 200:
filename = "feedly-backup-" + str(int(time.time())) + ".opml"
with open(filename, "w") as f:
f.write(r.text)
if not SILENT:
print("Saved Subscription feeds to file: ", filename)
else:
print("Error while fetching OPML file, status code: " + str(r.status_code))
print(r.json())
def get_saved_items(user_id, access_token):
headers = {"Authorization" : "OAuth " + access_token}
url = "https://cloud.feedly.com/v3/streams/contents?streamId=user/" + user_id + "/tag/global.saved&count=10000"
if not SILENT:
print("Fetching saved items...")
r = requests.get(url, headers = headers)
if r.status_code == 200:
response = r.json()
items = response["items"]
url_list = []
count = 1
for i in items:
content_title = i["title"]
try:
content_url = i["canonical"][0]["href"]
except KeyError:
content_url = i["alternate"][0]["href"]
url_list.append((content_title, content_url))
if not SILENT:
print()
print("[" + str(count) + "]" + " Title : ", content_title)
print("[" + str(count) + "]" + " URL: ", content_url)
count += 1
return url_list
else:
print("Error while fetching saved items, status code: " + str(r.status_code))
print(r.json())
exit(1)
def make_bookmark(url_list):
html = """<!DOCTYPE NETSCAPE-Bookmark-file-1>
<!--This is an automatically generated file.
It will be read and overwritten.
Do Not Edit! -->
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<Title>Bookmarks</Title>
<H1>Bookmarks</H1>
<DL><p>
"""
for i in url_list:
item = ' <DT><A HREF="' + i[1] + '">' + i[0] + '</a>\n'
html += item
html += '</DL><p>\n'
filename = "feedly-saved-" + str(int(time.time())) + ".html"
with open(filename, "w") as f:
f.write(html)
if not SILENT:
print()
print("Saved contents to Bookmark file: " + str(filename))
print()
def main(fetch_saved, fetch_opml):
try:
user_id = os.environ['FEEDLY_USER_ID']
access_token = os.environ['FEEDLY_ACCESS_TOKEN']
except KeyError:
print("Please Set the Environment Variables FEEDLY_USER_ID, FEEDLY_ACCESS_TOKEN")
exit(1)
if fetch_saved:
url_list = get_saved_items(user_id, access_token)
make_bookmark(url_list)
if fetch_opml:
get_opml(access_token)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="A Command Line Utility to backup saved items (Read Later's) and subscriptions from feedly")
parser.add_argument("-v", "--verbose", action='store_true', default=False, help="Print what is being done")
parser.add_argument("--no-saved", action='store_true', default=False, help="Don't fetch and make a bookmark file of saved items")
parser.add_argument("--no-opml", action='store_true', default=False, help="Don't download OPML file containing the subscribed feeds")
args = parser.parse_args()
SILENT= not args.verbose
fetch_saved = not args.no_saved
fetch_opml = not args.no_opml
main(fetch_saved, fetch_opml)