Zeroji

Obliviate

Sep 30th, 2017
79
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. import requests
  3. import json
  4. import sys
  5. import os
  6.  
  7. TOKEN = ''
  8. API = 'https://api.imgur.com/3/'
  9.  
  10. def get(url):
  11.   return requests.get(API + url, headers={'Authorization': TOKEN}).json()
  12. def post(url):
  13.   try:
  14.     return requests.post(API + url, headers={'Authorization': TOKEN}).json()
  15.   except json.JSONDecodeError:
  16.     return {'success': False}
  17.  
  18. def dump(fp, data):
  19.   open(fp, 'w').write('\n'.join(list(data)+['']))
  20.  
  21. def get_ids(account, posts=True, v=True):
  22.   name = 'posts' if posts else 'comments'
  23.   fp='%s_%s.txt'%(account, name)
  24.   ids = set()
  25.   if os.path.isfile(fp):
  26.     ids = set(open(fp).read().splitlines())
  27.   page = 0
  28.   while True:
  29.     r = get('account/%s/%s/%d' % (account, 'submissions' if posts else 'comments/newest', page))
  30.     if not r.get('success') or len(r.get('data'))==0:
  31.       break
  32.     page += 1
  33.     id=[str(c['id']) for c in r.get('data')]
  34.     stop = id[-1] in ids
  35.     ids.update(id)
  36.     if v:
  37.       print('Getting %s... (%d found)' % (name, len(ids)), end='\r')
  38.     if stop:
  39.       break
  40.   if v: print()
  41.   dump(fp, ids)
  42.   return ids
  43.  
  44. def bar(n, t, l=40):
  45.   x=(n*l)//t
  46.   return '['+'='*x+' '*(l-x)+']'
  47.  
  48. def do_vote(account, ids, vote, posts=True, v=True):
  49.   name = 'posts' if posts else 'comments'
  50.   s,f,t=0,0,len(ids)
  51.   fp='%s_%s_done.txt'%(account, name)
  52.   done=set()
  53.   if os.path.isfile(fp):
  54.     done=set(open(fp).read().splitlines())
  55.   for id in ids:
  56.     if id in done:
  57.       s+=1
  58.     else:
  59.       r = post('%s/%s/vote/%s' % ('gallery' if posts else 'comment', id, vote))
  60.       if r.get('success'):
  61.         done.add(id)
  62.         open(fp, 'a').write(id+'\n')
  63.         s+=1
  64.       else:
  65.         f+=1
  66.     print(name.capitalize()+':', bar(s+f, t), '(%d/%d, %d failed)' % (s, t, f), end='\r')
  67.   print()
  68.  
  69. if __name__ == '__main__':
  70. # Command-line input
  71. #  TOKEN = 'Bearer ' + open('token').read().strip()
  72. #  account = sys.argv[1]
  73. #  comments = '-c' in sys.argv
  74. #  gallery = '-s' in sys.argv or '-g' in sys.argv or '-p' in sys.argv
  75. #  vote = 'up' if 'up' in sys.argv else 'down'
  76. # Human input
  77.   account = input('Enter target user: ').strip()
  78.   v = input('Vote "up" or "down"? ')
  79.   vote = 'up' if v.lower() in '1trueupvote' else 'down'
  80.   both = input('Vote on posts, comments, or both? ')
  81.   gallery = both.lower() != 'comments'
  82.   comments= both.lower() != 'posts'
  83.   if not TOKEN:
  84.     TOKEN = 'Bearer ' + input('Enter your token: ').strip()
  85. # Start
  86.   acc = get('account/%s' % account).get('data')
  87.   name, points = acc.get('url'), acc.get('reputation')
  88.   print('%s currently has %d points' % (name, points))
  89.   cids,pids=[],[]
  90.   if comments:
  91.     cids = get_ids(account, posts=False)
  92.   if gallery:
  93.     pids = get_ids(account)
  94.   print('Estimated reputation change: ' + ('+' if vote == 'up' else '-') + str(len(cids)+len(pids)))
  95.   if comments:
  96.     do_vote(account, cids, vote, posts=False)
  97.   if gallery:
  98.     do_vote(account, pids, vote)
  99.   acc = get('account/%s' % account).get('data')
  100.   name, points2 = acc.get('url'), acc.get('reputation')
  101.   print('%s currently has %d points' % (name, points2))
  102.   print('Point difference since beginning: %d' % (points2 - points))
  103.   input('Press Enter to close...')
RAW Paste Data