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 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('-u', '--user', required=True, help='The username whose comments will be downloaded. Required (if not ' 'provided by an @ file like @default.args).') 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('-o', '--output-file', help='A file to save the comments in, instead of printing to stdout.') parser.add_argument('-l', '--limit', type=int, help='Maximum number of comments to output. Default: no limit.') parser.add_argument('-q', '--quiet', dest='quiet', action='store_true', help='Do not print anything but the results (even if there are none). ' 'Default: False') parser.add_argument('-v', '--verbose', action='store_true', help='Verbose output (print more than just the results). Default: True') new_argv = imgurlib.include_args_from_file(sys.argv, CONFIG_FILE) args = parser.parse_args(new_argv) if args.verbose: verbosity = 2 elif args.quiet: verbosity = 0 else: verbosity = 2 comment_generator = imgurcache.get_live_comments(args.user, args.client_id, limit=args.limit, user_agent=USER_AGENT, verbosity=verbosity) comments = list(comment_generator) if args.output_file: with open(args.output_file, 'w') as filehandle: json.dump(comments, filehandle) else: print json.dumps(comments) if verbosity > 0: sys.stderr.write('Saved '+str(len(comments))+' comments.\n')
def main(): parser = argparse.ArgumentParser(description=DESCRIPTION, epilog=EPILOG, fromfile_prefix_chars='@') parser.set_defaults(**OPT_DEFAULTS) parser.add_argument('query', help='String to search for.') parser.add_argument('-u', '--user', required=True, help='The username whose comments will be searched. Required, if not ' 'provided by an @ file like @default.args.') 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('-c', '--case-sensitive', dest='ignore_case', action='store_false', help='Don\'t ignore case when searching. Default: ' +str(not OPT_DEFAULTS['ignore_case'])) parser.add_argument('-i', '--ignore-case', dest='ignore_case', action='store_true', help='Ignore case when searching. Default: ' +str(OPT_DEFAULTS['ignore_case'])) parser.add_argument('-r', '--regex', action='store_true', help='Use search string as a Python regex instead of a literal string to ' 'match.') parser.add_argument('-v', '--invert', action='store_true', help='Find comments that *do not* match the query. Like grep\'s "-v".') parser.add_argument('-l', '--limit', type=int, help='Maximum number of results to return. Set to 0 for no limit. ' 'Default: %(default)s') parser.add_argument('-L', '--links', dest='format', action='store_const', const='links', help='Print permalinks to the comments instead of the full info. Turns off ' 'verbose mode, unless overriden. Default: ' +str(OPT_DEFAULTS['format'] == 'links')) parser.add_argument('-f', '--feeling-lucky', dest='limit', action='store_const', const=1, help='Stop searching once the first hit is found. A shorthand for -l 1. ' 'Default: False') parser.add_argument('-q', '--quiet', dest='quiet', action='store_true', help='Do not print anything but the results (even if there are none). ' 'Default: '+str(not OPT_DEFAULTS['verbose_mode'])) parser.add_argument('-V', '--verbose', action='store_true', help='Verbose output (print more than just the results). Default: ' +str(OPT_DEFAULTS['verbose_mode'])) new_argv = imgurlib.include_args_from_file(sys.argv, CONFIG_FILE) args = parser.parse_args(new_argv) if args.verbose is None and args.quiet is None: if args.format == 'human': args.verbose_mode = OPT_DEFAULTS['verbose_mode'] elif args.format == 'links': args.verbose_mode = False else: args.verbose_mode = bool(args.verbose or not args.quiet) comments = imgurcache.get_cached_and_live_comments(args.user, args.client_id, user_agent=USER_AGENT) hits = 0 for comment in comments: match = is_match(comment['comment'], args) if (not args.invert and match) or (args.invert and not match): hits+=1 if args.format == 'human': print imgurlib.human_format(comment) elif args.format == 'links': print imgurlib.link_format(comment) if args.limit and hits >= args.limit: break if args.verbose_mode: sys.stderr.write('Found '+str(hits)+' hits.\n') if args.limit and hits >= args.limit: sys.stderr.write('Reached the results limit. There may be more ' 'matching comments than are shown here. Raise the search limit ' '(currently '+str(args.limit)+') with the -l option to show more.\n') else: sys.stderr.write('Search complete. All matching comments were printed.\n')