def main(): parser = argparse.ArgumentParser(description=DESCRIPTION, epilog=EPILOG, fromfile_prefix_chars='@') parser.set_defaults(**OPT_DEFAULTS) parser.add_argument('-n', '--per-image', type=int, help='Comments per image.') parser.add_argument('-C', '--client-id', required=True, help='Imgur API Client-ID to use. Required, if not provided by an @ file ' 'like @default.args.') parser.add_argument('-u', '--user', help='Imgur username. For compatibility only; not required.') new_argv = imgurlib.include_args_from_file(sys.argv, CONFIG_FILE) args = parser.parse_args(new_argv) # get random set of images page = random.randrange(0,51) path = RANDOM_PATH.format(page) (response, images) = imgurlib.make_request(path, args.client_id) imgurlib.handle_status(response.status) sys.excepthook = catch_quota_except for image in images: # get comments per image path = COMMENTS_PATH.format(image['id']) (response, comments) = imgurlib.make_request(path, args.client_id) imgurlib.handle_status(response.status) if args.per_image > len(comments): comment_sample_size = len(comments) else: comment_sample_size = args.per_image for comment in random.sample(comments, comment_sample_size): username = comment['author'] if username == '[deleted]': sys.stderr.write('Deleted username. Skipping.\n') continue # get number of comments per user path = COMMENT_COUNT_PATH.format(username) (response, count) = imgurlib.make_request(path, args.client_id) imgurlib.handle_status(response.status) if not isinstance(count, int): sys.stderr.write('Non-integer count: '+str(count)[:70]+'\n') continue print "{}\t{}".format(username, count)
def main(): parser = argparse.ArgumentParser(description=DESCRIPTION, epilog=EPILOG, fromfile_prefix_chars='@') parser.set_defaults(**OPT_DEFAULTS) parser.add_argument('comment_identifier', help='The comment ID or the URL to the comment (permalink).') parser.add_argument('-C', '--client-id', required=True, help='Imgur API Client-ID to use. Required, if not provided by an @ file ' 'like @default.args.') parser.add_argument('-u', '--user', help='Imgur username. For compatibility only; not required.') parser.add_argument('-r', '--recursive', action='store_true', help='Show the comment, then show its parent, etc, all the way up the ' 'thread.') new_argv = imgurlib.include_args_from_file(sys.argv, CONFIG_FILE) args = parser.parse_args(new_argv) match = re.search(PERMALINK_PATTERN, args.comment_identifier) if match: comment_id = match.group(1) else: match = re.search(COMMENT_ID_PATTERN, args.comment_identifier) if match: comment_id = args.comment_identifier else: fail('Error: Unrecognized comment identifier "'+args.comment_identifier +'"') if comment_id == '0': fail('Error: That\'s the root comment! (The comment you gave is not a ' 'reply.)') comments = [] while comment_id != '0': path = API_PATH+comment_id (response, comment) = imgurlib.make_request( path, args.client_id, user_agent=USER_AGENT, domain=API_DOMAIN ) imgurlib.handle_status(response.status) comments.append(comment) if args.recursive: comment_id = str(comment['parent_id']) else: break for comment in reversed(comments): print imgurlib.details_format(comment)
def username_to_id(user, client_id, user_agent=USER_AGENT): api_path = ACCOUNT_PATH.format(user) (response, account_data) = imgurlib.make_request( api_path, client_id, user_agent=user_agent, domain=API_DOMAIN ) imgurlib.handle_status(response.status, fatal=False) return str(account_data['id'])
def get_live_comment_chunks(user, client_id, cutoff_date=0, limit=0, per_page=100, user_agent=USER_AGENT, verbosity=0): """Same as get_comments(), but yield lists of comments at a time instead of individual ones. (Each list == one page == one request.)""" api_path = COMMENTS_PATH.format(user) params = { 'perPage':str(per_page), } total = 0 page_num = 0 still_searching = True while still_searching: # make request params['page'] = str(page_num) (response, comments_page) = imgurlib.make_request( api_path, client_id, user_agent=user_agent, params=params, domain=API_DOMAIN ) imgurlib.handle_status(response.status, fatal=False) assert is_iterable(comments_page), ('Error: Expected comments to be an ' 'iterable.') if len(comments_page) == 0: still_searching = False if verbosity >= 2: sys.stderr.write('Reached end of comments. All were retrieved.\n') if limit == 0 and cutoff_date == 0: comments_group = comments_page else: comments_group = [] for comment in comments_page: total+=1 # Exceeded limit? Discard comment. if comment['datetime'] < cutoff_date or (limit and total > limit): still_searching = False if verbosity >= 1: sys.stderr.write('Found more comments than the limit.\n') break else: comments_group.append(comment) page_num+=1 if len(comments_group) == 0: raise StopIteration else: yield comments_group