def extract_gif_from_comment(ghm: GifHostManager, text: str) -> Optional[GifHost]: """Checks a string for valid urls""" # Look through every word to see if one is a url gif = ghm.extract_gif(text) if gif: return gif # Check markdown links too for i in REPatterns.link.findall(text): if ghm.extract_gif(i): return i return None
reddit = praw.Reddit(user_agent=consts.user_agent, client_id=credentials['reddit']['client_id'], client_secret=credentials['reddit']['client_secret'], username=credentials['reddit']['username'], password=credentials['reddit']['password']) def get_date(seconds): return datetime.datetime.utcfromtimestamp(seconds).\ replace(tzinfo=datetime.timezone.utc).astimezone(tz=None) print("GifReversingBot Statistics v{} Ctrl+C to stop".format(consts.version)) ghm = GifHostManager(reddit) counter = 0 users = defaultdict(int) karma = {} gifs = {} for comment in reddit.user.me().comments.new(limit=None): counter += 1 author = comment.parent().author print(counter, comment.id, author, extract_gif_from_comment(ghm, comment.body), get_date(comment.created_utc)) if author: users[author.name] += 1 # pprint(vars(comment)) karma[comment.id] = comment.ups - comment.downs
class Gif(db.Entity): id = PrimaryKey(int, auto=True) origin_host = Required(GifHosts, reverse='origin_gifs') origin_id = Required(str) reversed_host = Required(GifHosts, reverse='reversed_gifs') reversed_id = Required(str) time = Required(date) nsfw = Optional(bool) total_requests = Optional(int) last_requested_date = Optional(date) bind_db(db) ghm = GifHostManager() def sync_hosts(): # Double check gifhost bindings with db_session: for host in ghm.hosts: q = select(h for h in GifHosts if h.name == host.name).first() if not q: new = GifHosts(name=host.name) sync_hosts() def check_database(original_gif: NewGif_object):
def process_comment(reddit, comment=None, queue=None, original_context=None): ghm = GifHostManager(reddit) if not original_context: # If we were not provided context, make our own # Check if comment is deleted try: if not comment.author: print("Comment doesn't exist????") print(vars(comment)) return USER_FAILURE except praw.exceptions.PRAWException as e: # Operator.instance().message(str(vars(comment)) + " " + str(vars(e)), "Funny business") # print(e) # I expected this to be a user failure since I thought it would mean the comment is getting # removed. However, it seems that this happens when the comment is too new for Reddit to # return any data on it. So if we mark it as an UPLOAD_FAILURE, we should be able to return # to it later and it should work then??? return UPLOAD_FAILURE print("New request by " + comment.author.name) # Create the comment context object context = CommentContext(reddit, comment, ghm) # Add context to operator in case we need to log it later Operator.set_request_info(context.to_json()) if not context.url: # Did our search return nothing? print("Didn't find a URL") return USER_FAILURE if context.rereverse and not context.reupload: # Is the user asking to rereverse? reply(context, context.url) return SUCCESS else: # If we are the client, context is provided to us context = original_context # Create object to grab gif from host # print(context.url) # gif_host = GifHost.open(context, reddit) # new_original_gif = ghm.extract_gif(context.url, context=context) new_original_gif = context.url print(new_original_gif) # If the link was not recognized, return # if not gif_host: # return USER_FAILURE if not new_original_gif: return USER_FAILURE # If the gif was unable to be acquired, return # original_gif = gif_host.get_gif() # if not original_gif: # return USER_FAILURE if not new_original_gif.id: return USER_FAILURE if queue: # Add to queue print("Adding to queue...") queue.add_job(context.to_json(), new_original_gif) return SUCCESS # Check database for gif before we reverse it gif = check_database(new_original_gif) # Requires new database setup # db_gif = check_database(new_original_gif) if gif: # db_gif # If we were asked to reupload, double check the gif if context.reupload: print("Doing a reupload check...") if not is_reupload_needed(reddit, gif): # No reupload needed, do normal stuff reply(context, gif) print("No reupload needed") return SUCCESS else: # Reupload is needed, delete this from the database delete_from_database(gif) print("Reuploadng needed") # Proceed as normal else: # If it was in the database, reuse it reply(context, gif) return SUCCESS # Analyze how the gif should be reversed # in_format, out_format = gif_host.analyze() # If there was some problem analyzing, exit # if not in_format or not out_format: # return USER_FAILURE if not new_original_gif.analyze(): return USER_FAILURE uploaded_gif = None # This gif cannot be uploaded and it is not our fault cant_upload = False # Try every option we have for reversing a gif options = ghm.get_upload_host(new_original_gif) if not options: print("File too large {}s {}MB".format( new_original_gif.files[0].duration, new_original_gif.files[0].size)) cant_upload = True else: cant_upload = False for option in options: upload_gif_host = option['hosts'][0] original_gif_file = option['file'] # Temporarily halt any uploads to Redgifs if upload_gif_host == ghm['Redgifs']: print("Blocked Redgifs upload") cant_upload = False # break return USER_FAILURE r = original_gif_file.file # Reverse it as a GIF if original_gif_file.type == consts.GIF: # With reversed gif f = reverse_gif(original_gif_file, format=original_gif_file.type) # Give to gif_host's uploader reversed_gif_file = GifFile(f, original_gif_file.host, consts.GIF, duration=original_gif_file.duration, frames=original_gif_file.frames) # reversed_gif = upload_gif_host.upload(f, consts.GIF, new_original_gif.context.nsfw) # Reverse it as a video else: f = reverse_mp4(r, original_gif_file.audio, format=original_gif_file.type, output=upload_gif_host.video_type) if isinstance(f, list): Operator.instance().message( "It appears the video was too big to be reversed\n\n{} from {} {}{} {}" .format(new_original_gif.url, comment.author, "NSFW " if context.nsfw else "", *f), "Notification") cant_upload = False return USER_FAILURE reversed_gif_file = GifFile(f, original_gif_file.host, upload_gif_host.video_type, duration=original_gif_file.duration, audio=original_gif_file.audio) # reversed_gif = upload_gif_host.upload(f, upload_gif_host.video_type, new_original_gif.context.nsfw) # Attempt a first upload options = ghm.get_upload_host(new_original_gif, file=reversed_gif_file) # If there was no suitable upload host, this format cannot be uploaded if not options: cant_upload = True continue # Using the provided host, perform the upload for i in range(2): result = options[0]['hosts'][0].upload(reversed_gif_file.file, reversed_gif_file.type, new_original_gif.nsfw, reversed_gif_file.audio) # If the host simply cannot accept this file at all if result == CannotUpload: cant_upload = True break # If the host was unable to accept the gif at this time elif result == UploadFailed: cant_upload = False continue # Try again? # No error and not None, success! elif result: uploaded_gif = result break # If we have the uploaded gif, break out and continue if uploaded_gif: break # If there was an error, return it if cant_upload: return USER_FAILURE # It's not that it was an impossible request, there was something else elif not uploaded_gif: return UPLOAD_FAILURE if uploaded_gif: # Add gif to database # if reversed_gif.log: add_to_database(new_original_gif, uploaded_gif) # Reply print("Replying!", uploaded_gif.url) reply(context, uploaded_gif) return SUCCESS else: return UPLOAD_FAILURE
reddit = praw.Reddit(user_agent=consts.user_agent, client_id=credentials['reddit']['client_id'], client_secret=credentials['reddit']['client_secret'], username=credentials['reddit']['username'], password=credentials['reddit']['password']) def get_date(seconds): return datetime.datetime.utcfromtimestamp(seconds).\ replace(tzinfo=datetime.timezone.utc).astimezone(tz=None) print("GifReversingBot Statistics v{} Ctrl+C to stop".format(consts.version)) ghm = GifHostManager(reddit) counter = 0 users = defaultdict(int) karma = {} gifs = {} for comment in reddit.user.me().comments.new(limit=None): counter += 1 author = comment.parent().author print(counter, comment.id, author, ghm.extract_gif(comment.body), get_date(comment.created_utc)) if author: users[author.name] += 1 # pprint(vars(comment)) karma[comment.id] = comment.ups - comment.downs # if not counter % 200: