Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. #!/bin/env python3
  2.  
  3. # Copyright (c) 2016, Daniel Miranda <danielkza2@gmail.com>
  4. # All rights reserved.
  5.  
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are met:
  8.  
  9. # 1. Redistributions of source code must retain the above copyright notice, this
  10. # list of conditions and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright notice,
  12. # this list of conditions and the following disclaimer in the documentation
  13. # and/or other materials provided with the distribution.
  14.  
  15. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25.  
  26. import os.path
  27. import sys
  28. import json
  29. import argparse
  30. import praw
  31.  
  32. def read_filters(fname):
  33. fp = None
  34. try:
  35. if fname == '-':
  36. fp = sys.stdin
  37. else:
  38. fp = open(fname, 'r')
  39.  
  40. return set(line.rstrip('\n').lower() for line in fp.readlines())
  41. finally:
  42. if fp and fp != sys.stdin:
  43. fp.close()
  44.  
  45. def sync_filters(reddit, username, filters):
  46. response = reddit.request('GET',
  47. '/api/filter/user/{}/f/all'.format(username))
  48. cur_filters = set(map(lambda sr: sr['name'].lower(),
  49. response['data']['subreddits']))
  50.  
  51. to_add = filters.difference(cur_filters)
  52. to_remove = cur_filters.difference(filters)
  53.  
  54. for sub in sorted(to_add):
  55. print('+ ' + sub)
  56. reddit.request('PUT',
  57. '/api/filter/user/{}/f/all/r/{}'.format(username, sub),
  58. data={'model': json.dumps({'name': sub})})
  59.  
  60. for sub in sorted(to_remove):
  61. print('- ' + sub)
  62. reddit.request('DELETE',
  63. '/api/filter/user/{}/f/all/r/{}'.format(username, sub))
  64.  
  65. if __name__ == '__main__':
  66. args = argparse.ArgumentParser(description="Manage Reddit's /r/all filter")
  67. args.add_argument('--username', metavar='NAME', help='Reddit username')
  68. args.add_argument('--password', metavar='PASS', help='Reddit password')
  69. args.add_argument('--client-id', metavar='ID',
  70. help='Reddit application client ID')
  71. args.add_argument('--client-secret', metavar='SECRET',
  72. help='Reddit application client secret')
  73. args.add_argument('filter_file', metavar='FILE', nargs='?',
  74. default='./reddit-filters.txt',
  75. help=('File containing subreddits names, one per line, '
  76. 'to replace the list of filters with. Text case is '
  77. 'irrelevant'))
  78. args = args.parse_args()
  79.  
  80. reddit = praw.Reddit(client_id=args.client_id,
  81. client_secret=args.client_secret,
  82. user_agent='filter manager by /u/danielkza',
  83. username=args.username,
  84. password=args.password)
  85. filters = read_filters(args.filter_file)
  86.  
  87. sync_filters(reddit, args.username, filters)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement