Skip to content

Commit 31b35c5

Browse files
authored
Merge pull request #26 from Linusp/new-command-edit-subscription
New command `edit-subscription`
2 parents 1ff58ac + 5e66c55 commit 31b35c5

File tree

6 files changed

+86
-7
lines changed

6 files changed

+86
-7
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
# CHANGELOG
22

3+
## v0.5.0
4+
5+
Added
6+
7+
- New method: `Inoreader.edit_subscription`
8+
- New command `edit-subscription`, now you can do these with this command:
9+
10+
- Subscribe a new feed
11+
- Unsubscribe a subscription
12+
- Rename a subscription
13+
- Add a subscription to a folder or remove it from a folder
14+
15+
Thanks to [Robért](https://github.com/rsguhr) for his [advice](https://github.com/Linusp/python-inoreader/issues/25).
16+
317
## v0.4.6
418

519
Added

inoreader/client.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class InoreaderClient(object):
3232
SUBSCRIPTION_LIST_PATH = 'subscription/list'
3333
STREAM_CONTENTS_PATH = 'stream/contents/'
3434
EDIT_TAG_PATH = 'edit-tag'
35+
EDIT_SUBSCRIPTION_PATH = 'subscription/edit'
3536

3637
# tags
3738
GENERAL_TAG_TEMPLATE = 'user/-/label/{}'
@@ -151,7 +152,7 @@ def get_stream_contents(self, stream_id, c='', limit=None):
151152
for a in articles:
152153
try:
153154
yield Article.from_json(a)
154-
fetched_count+=1
155+
fetched_count += 1
155156
except Exception as e:
156157
print(e)
157158
continue
@@ -160,7 +161,6 @@ def get_stream_contents(self, stream_id, c='', limit=None):
160161
break
161162
if c is None:
162163
break
163-
164164

165165
def __get_stream_contents(self, stream_id, continuation=''):
166166
self.check_token()
@@ -227,7 +227,6 @@ def fetch_articles(self, folder=None, tags=None, unread=True, starred=False, lim
227227
break
228228

229229
continuation = response.get('continuation')
230-
231230

232231
def fetch_unread(self, folder=None, tags=None, limit=None):
233232
for article in self.fetch_articles(folder=folder, tags=tags, unread=True):
@@ -285,3 +284,27 @@ def remove_liked(self, articles):
285284

286285
def broadcast(self, articles):
287286
self.add_general_label(articles, self.BROADCAST_TAG)
287+
288+
def edit_subscription(self, stream_id, action, title=None, add_folder=None, remove_folder=None):
289+
self.check_token()
290+
url = urljoin(BASE_URL, self.EDIT_SUBSCRIPTION_PATH)
291+
# https://us.inoreader.com/developers/edit-subscription
292+
# The documentation looks a bit outdated, `follow`/`unfollow` don't work
293+
action = {'follow': 'subscribe', 'unfollow': 'unsubscribe'}.get(action) or action
294+
params = {'ac': action, 's': stream_id}
295+
if title:
296+
params['t'] = title
297+
298+
if add_folder:
299+
params['a'] = add_folder
300+
301+
if remove_folder:
302+
params['r'] = remove_folder
303+
304+
r = self.session.post(url, params=params, proxies=self.proxies)
305+
response = self.parse_response(
306+
r,
307+
# self.session.post(url, params=params, proxies=self.proxies),
308+
json_data=False
309+
)
310+
return response

inoreader/main.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,5 +586,47 @@ def fetch_starred(folder, tags, outfile, outdir, limit, save_image, out_format):
586586
LOGGER.info("fetched %d articles and saved them in %s", fetched_count, outdir)
587587

588588

589+
@main.command("edit-subscription")
590+
@click.option("-a", "--action",
591+
required=True,
592+
type=click.Choice(['follow', 'unfollow', 'rename', 'add-folder', 'remove-folder']),
593+
help="")
594+
@click.option("-i", "--stream-id", required=True, help='Stream ID which you want to fetch')
595+
@click.option("-n", "--name", help='The name of subscription, for action follow/rename(required)')
596+
@click.option("-f", "--folder", help='Folder which subscription belong to')
597+
@catch_error
598+
def edit_subscriptions(action, stream_id, name, folder):
599+
"""Get your subscriptions"""
600+
edit_action = action
601+
if action in ('rename', 'add-folder', 'remove-folder'):
602+
edit_action = 'edit'
603+
if action == 'rename' and not name:
604+
click.secho("`name` is required for action `rename`!", fg="red")
605+
return -1
606+
elif action in ('add-folder', 'remove_starred') and not folder:
607+
click.secho(f"`folder` is required for action `{action}`", fg="red")
608+
return -1
609+
610+
client = get_client()
611+
stream_id = 'feed/' + stream_id if not stream_id.startswith('feed/') else stream_id
612+
if folder and not folder.startswith('user/-/label/'):
613+
folder = client.GENERAL_TAG_TEMPLATE.format(folder)
614+
615+
add_folder = folder if action in ('follow', 'add-folder') else None
616+
remove_folder = folder if action == 'remove-folder' else None
617+
try:
618+
response = client.edit_subscription(
619+
stream_id,
620+
edit_action,
621+
title=name,
622+
add_folder=add_folder,
623+
remove_folder=remove_folder
624+
)
625+
click.secho(response, fg="green")
626+
except Exception as exception:
627+
print("Error:", str(exception))
628+
return -1
629+
630+
589631
if __name__ == '__main__':
590632
main()

inoreader/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def extract_text(html_content):
3838
link.text = '[%s](%s)' % (text, url)
3939
try:
4040
return content.text_content().replace("\xa0", "").strip()
41-
except:
41+
except Exception:
4242
return ""
4343

4444

setup.cfg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[flake8]
22
max-line-length = 100
3-
ignore = E201,E202
3+
ignore = E201,E202,E203
44

55
[pep8]
66
max-line-length = 100
7-
ignore = E201,E202
7+
ignore = E201,E202,E203

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from setuptools import setup, find_packages
55

66

7-
VERSION = '0.4.7'
7+
VERSION = '0.5.0'
88
REQS = [
99
'lxml',
1010
'requests',

0 commit comments

Comments
 (0)