Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import requests
- import json
- import sys
- import os
- TOKEN = ''
- API = 'https://api.imgur.com/3/'
- def get(url):
- return requests.get(API + url, headers={'Authorization': TOKEN}).json()
- def post(url):
- try:
- return requests.post(API + url, headers={'Authorization': TOKEN}).json()
- except json.JSONDecodeError:
- return {'success': False}
- def dump(fp, data):
- open(fp, 'w').write('\n'.join(list(data)+['']))
- def get_ids(account, posts=True, v=True):
- name = 'posts' if posts else 'comments'
- fp='%s_%s.txt'%(account, name)
- ids = set()
- if os.path.isfile(fp):
- ids = set(open(fp).read().splitlines())
- page = 0
- while True:
- r = get('account/%s/%s/%d' % (account, 'submissions' if posts else 'comments/newest', page))
- if not r.get('success') or len(r.get('data'))==0:
- break
- page += 1
- id=[str(c['id']) for c in r.get('data')]
- stop = id[-1] in ids
- ids.update(id)
- if v:
- print('Getting %s... (%d found)' % (name, len(ids)), end='\r')
- if stop:
- break
- if v: print()
- dump(fp, ids)
- return ids
- def bar(n, t, l=40):
- x=(n*l)//t
- return '['+'='*x+' '*(l-x)+']'
- def do_vote(account, ids, vote, posts=True, v=True):
- name = 'posts' if posts else 'comments'
- s,f,t=0,0,len(ids)
- fp='%s_%s_done.txt'%(account, name)
- done=set()
- if os.path.isfile(fp):
- done=set(open(fp).read().splitlines())
- for id in ids:
- if id in done:
- s+=1
- else:
- r = post('%s/%s/vote/%s' % ('gallery' if posts else 'comment', id, vote))
- if r.get('success'):
- done.add(id)
- open(fp, 'a').write(id+'\n')
- s+=1
- else:
- f+=1
- print(name.capitalize()+':', bar(s+f, t), '(%d/%d, %d failed)' % (s, t, f), end='\r')
- print()
- if __name__ == '__main__':
- # Command-line input
- # TOKEN = 'Bearer ' + open('token').read().strip()
- # account = sys.argv[1]
- # comments = '-c' in sys.argv
- # gallery = '-s' in sys.argv or '-g' in sys.argv or '-p' in sys.argv
- # vote = 'up' if 'up' in sys.argv else 'down'
- # Human input
- account = input('Enter target user: ').strip()
- v = input('Vote "up" or "down"? ')
- vote = 'up' if v.lower() in '1trueupvote' else 'down'
- both = input('Vote on posts, comments, or both? ')
- gallery = both.lower() != 'comments'
- comments= both.lower() != 'posts'
- if not TOKEN:
- TOKEN = 'Bearer ' + input('Enter your token: ').strip()
- # Start
- acc = get('account/%s' % account).get('data')
- name, points = acc.get('url'), acc.get('reputation')
- print('%s currently has %d points' % (name, points))
- cids,pids=[],[]
- if comments:
- cids = get_ids(account, posts=False)
- if gallery:
- pids = get_ids(account)
- print('Estimated reputation change: ' + ('+' if vote == 'up' else '-') + str(len(cids)+len(pids)))
- if comments:
- do_vote(account, cids, vote, posts=False)
- if gallery:
- do_vote(account, pids, vote)
- acc = get('account/%s' % account).get('data')
- name, points2 = acc.get('url'), acc.get('reputation')
- print('%s currently has %d points' % (name, points2))
- print('Point difference since beginning: %d' % (points2 - points))
- input('Press Enter to close...')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement