-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewsBlurSavedStories.py
215 lines (160 loc) · 6.13 KB
/
newsBlurSavedStories.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
'''
newsBlurSavedStories.py
WRITTEN BY: Sarah H. McGrath (www.shmcgrath.com)
LAST MODIFICATIONS: 2017-01-12
This script will get favorited stories from NewsBlur and put them into
a markdown document for the downloader's use.
To use this script, put your NewsBlur username and password into
nbUser and nbPass.
Helpful References that got me started (and finished):
- [D'Arcy Norman's post getting starred feed items from Newsblur via Python](https://darcynorman.net/2016/12/28/getting-starred-feed-items-from-newsblur-via-python/)
- [Requests: HTTP for Humans' Quickstart Documentation](http://docs.python-requests.org/en/master/user/quickstart/#more-complicated-post-requests)
- [John Morahan's newsblur-export in php](https://github.com/jmorahan/newsblur-export/blob/master/export.php)
- [NewsBlur API in YAML](https://github.com/samuelclay/NewsBlur/blob/master/templates/static/api.yml)
- [Raphael Jasjukaitis' instapaper2chrome.py](https://gist.github.com/raphaa/1327761)
- [Christoper Su's export-saved-reddit](https://github.com/csu/export-saved-reddit))
- I followed D'Arcy Norman's lead and chose readability over brevity.
This means long variable and function names.
- Finally, I tried to comment the code as extensively as possible
so others understand my thought processes.
- I am not sure what kind of license to put on this, so feel free to use
it as needed. Any modifications or forks are fine as long as you link
back to the original on github. Feel free to submit a pull request.
- I am hoping to either add features to this or to roll this into a
larger project for importing/exporting saved articles from various sites.
Thanks, Sarah
'''
import requests
import json
import logging
import os
import sys
from collections import Mapping
# setting NewsBlur name and password
# TODO: use a config file
nbUser = 'USER' #Your username here
nbPass = 'PASS' #Your password here
#creating the creds string to easily pass the credentials
nbCreds = {'username': nbUser, 'password': nbPass}
nbURL = 'http://www.newsblur.com'
'''
newsBlurLogin logs the user in via the API and returns the session ID
to nbSessionId
'''
def newsBlurLogin():
global nbSessionId
loginURL = nbURL + '/api/login'
login = requests.post(loginURL, data=nbCreds)
print('==LOGIN INFORMATION==')
print('Status Code: '+str(login.status_code))
nbSessionId = login.cookies['newsblur_sessionid']
print('nbSessionID Value:'+nbSessionId)
print('')
return nbSessionId
'''
newsBlurCookies gets the cookies that I need and returns it to a global
variable. I cannot get this to work still.
'''
def newsBlurCookies():
global nbCookies
nbCookies = dict(newsblur_sessionid=nbSessionId)
#print('==COOKIE INFORMATION==')
#print('nbCookies.text: '+nbCookies.text)
#print('')
return nbCookies
'''
getFeedInformation gets the starred count of the newsBlur feed that is
logged in. This is used to determine how long to run the
newsBlurStarredStories function below.
'''
def getFeedInformation():
#cannot get nbCookies global variable to work
nbCookies = dict(newsblur_sessionid=str(nbSessionId))
global countOfStarred
feedParams = {'flat': 'true'}
feedURL = nbURL + '/reader/feeds'
feedList = requests.get(feedURL, params=feedParams, cookies=nbCookies)
feedListRaw = feedList.json()
countOfStarred = feedListRaw['starred_count']
return countOfStarred
#tagMerge combines folders and tags into a single list of tags.
#This was done with the intent of eventually uploading to Pinboard.in
def tagMerge(tags, folders):
tagsIn = tags
foldersIn = folders
processList = tagsIn + foldersIn
outList = []
for tag in processList:
tagNoSpaces = ''.join(tag.split())
tagLowerCase = tagNoSpaces.lower()
tagOut = tagLowerCase
outList.append(tagOut)
return outList
'''
tagListToString creates a string list of all tags with each tag
separated by a comma.
'''
def tagListToString(tags):
tagsIn = tags
tags = ','.join(map(str, tagsIn))
return tags
'''
gatherStoryInfo grabs the story name, story permalink, story tags, and
the folder that the story was saved to. This will then return the
markdown line for printing. The encoded modifier came from an earlier
version that may have had me encoding the title and URL.
'''
def gatherStoryInfo(aStoryIn):
aStory = aStoryIn
title = aStory['story_title']
url = aStory['story_permalink']
tags = aStory['story_tags']
folder = aStory['user_tags']
encodedTitle = title
encodedUrl = url
encodedTagsList = tagMerge(tags, folder)
encodedTags = tagListToString(encodedTagsList)
mdLine = '[' + str(encodedTitle) + '](' + str(encodedUrl) + ') ' + encodedTags
return mdLine
'''
newsBlurStarredStories gets all of the information about the starred
stories that are in the user's account. it also creates the markdown
document and loops through the starred stories.
'''
def newsBlurStarredStories():
#cannot get nbCookies global variable to work
nbCookies = dict(newsblur_sessionid=str(nbSessionId))
#get new text document
fileName = "newsBlurFavorites.md"
nbFavoritesMD = open(fileName, 'w')
i = 1
page = 1
y = countOfStarred
x = countOfStarred // 10
remainder = countOfStarred % 10
if remainder != 0:
x+=2
if remainder == 0:
x+=1
starredURL = nbURL + '/reader/starred_stories'
starredParams = {'page': page,}
storyCount = 0
page = 1
while i < x:
starredList = requests.get(starredURL, {'page': page,}, cookies=nbCookies)
starredListRaw = starredList.json()
userStarredStories = starredListRaw ['stories']
print('Gathering Stories from Page '+str(i)+'...')
for aStory in userStarredStories:
mdLine = gatherStoryInfo(aStory)
storyCount +=1
nbFavoritesMD.write(mdLine)
nbFavoritesMD.write("\n")
i+=1
page+=1
print('Running Story Count: '+str(storyCount))
nbFavoritesMD.close()
#This code runs the script.
newsBlurLogin()
getFeedInformation()
userStarredStories = newsBlurStarredStories()