Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/env python3
- # Copyright (c) 2016, Daniel Miranda <danielkza2@gmail.com>
- # All rights reserved.
- # Redistribution and use in source and binary forms, with or without
- # modification, are permitted provided that the following conditions are met:
- # 1. Redistributions of source code must retain the above copyright notice, this
- # list of conditions and the following disclaimer.
- # 2. Redistributions in binary form must reproduce the above copyright notice,
- # this list of conditions and the following disclaimer in the documentation
- # and/or other materials provided with the distribution.
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- import os.path
- import sys
- import json
- import argparse
- import praw
- def read_filters(fname):
- fp = None
- try:
- if fname == '-':
- fp = sys.stdin
- else:
- fp = open(fname, 'r')
- return set(line.rstrip('\n').lower() for line in fp.readlines())
- finally:
- if fp and fp != sys.stdin:
- fp.close()
- def sync_filters(reddit, username, filters):
- response = reddit.request('GET',
- '/api/filter/user/{}/f/all'.format(username))
- cur_filters = set(map(lambda sr: sr['name'].lower(),
- response['data']['subreddits']))
- to_add = filters.difference(cur_filters)
- to_remove = cur_filters.difference(filters)
- for sub in sorted(to_add):
- print('+ ' + sub)
- reddit.request('PUT',
- '/api/filter/user/{}/f/all/r/{}'.format(username, sub),
- data={'model': json.dumps({'name': sub})})
- for sub in sorted(to_remove):
- print('- ' + sub)
- reddit.request('DELETE',
- '/api/filter/user/{}/f/all/r/{}'.format(username, sub))
- if __name__ == '__main__':
- args = argparse.ArgumentParser(description="Manage Reddit's /r/all filter")
- args.add_argument('--username', metavar='NAME', help='Reddit username')
- args.add_argument('--password', metavar='PASS', help='Reddit password')
- args.add_argument('--client-id', metavar='ID',
- help='Reddit application client ID')
- args.add_argument('--client-secret', metavar='SECRET',
- help='Reddit application client secret')
- args.add_argument('filter_file', metavar='FILE', nargs='?',
- default='./reddit-filters.txt',
- help=('File containing subreddits names, one per line, '
- 'to replace the list of filters with. Text case is '
- 'irrelevant'))
- args = args.parse_args()
- reddit = praw.Reddit(client_id=args.client_id,
- client_secret=args.client_secret,
- user_agent='filter manager by /u/danielkza',
- username=args.username,
- password=args.password)
- filters = read_filters(args.filter_file)
- sync_filters(reddit, args.username, filters)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement