def test_nsfw_keyword(self): correct = {'comment': 't1_ekyb5cv', 'nsfw': True, 'url': 'https://i.redd.it/ykr4josxacs21.gif'} comment = reddit.comment('ekyb5cv') context = CommentContext(reddit, comment) obj = context.to_json() print(obj) self.assertEqual(correct, extractDictAFromB(correct, obj))
def test_nsfw_post(self): correct = {'comment': 't1_ekf25nx', 'nsfw': True, 'url': 'https://imgur.com/Xo73bxR.gifv'} comment = reddit.comment('ekf25nx') context = CommentContext(reddit, comment) obj = context.to_json() print(obj) self.assertEqual(correct, extractDictAFromB(correct, obj))
def test_nsfw_keyword(self): correct = {'comment': 't1_ekf5cl8', 'nsfw': True, 'url': 'https://imgur.com/5PVtWsf.gifv'} comment = reddit.comment('ekf5cl8') context = CommentContext(reddit, comment) obj = context.to_json() print(obj) self.assertEqual(correct, extractDictAFromB(correct, obj))
def test_normal(self): correct = {'comment': 't1_ekf4i80', 'rereverse': False, 'unnecessary_manual': False, 'nsfw': False, 'distinguish': False, 'reupload': False, 'url': 'https://imgur.com/5PVtWsf.gifv'} comment = reddit.comment('ekf4i80') context = CommentContext(reddit, comment) obj = context.to_json() print(obj) self.assertEqual(correct, extractDictAFromB(correct, obj))
def test_nsfw_sub(self): sub = reddit.subreddit('pmdevita') sub.mod.update(over_18=True) correct = {'comment': 't1_ekf4i80', 'nsfw': True, 'url': 'https://imgur.com/5PVtWsf.gifv'} comment = reddit.comment('ekf4i80') context = CommentContext(reddit, comment) obj = context.to_json() print(obj) sub.mod.update(over_18=False) self.assertEqual(correct, extractDictAFromB(correct, obj))
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
def process_comment(reddit, comment): # Check if comment is deleted if not comment.author: print("Comment doesn't exist????") print(vars(comment)) return USER_FAILURE print("New request by " + comment.author.name) # Create the comment context object context = CommentContext(reddit, comment) if not context.url: # Did our search return nothing? print("Didn't find a URL") return USER_FAILURE if context.rereverse: # Is the user asking to rereverse? reply(context, context.url) return SUCCESS # Create object to grab gif from host print(context.url) gif_host = GifHost.open(context, reddit) # If the link was not recognized, return if not gif_host: 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 # Check database for gif before we reverse it gif = check_database(original_gif) # If it was in the database, reuse it if gif: reply(context, gif.url) return SUCCESS # Analyze how the gif should be reversed method = gif_host.analyze() # If there was some problem analyzing, exit if not method: return USER_FAILURE reversed_gif = None if isinstance(gif_host.url, str): r = requests.get(gif_host.url) elif isinstance(gif_host.url, requests.Response): r = gif_host.url # If we 404, it must not exist if r.status_code == 404: print("Gif not found at URL") return USER_FAILURE # Reverse it as a GIF if method == consts.GIF: # With reversed gif with reverse_gif(BytesIO(r.content)) as f: # Give to gif_host's uploader reversed_gif = gif_host.upload_gif(f) # Reverse it as a video elif method == consts.VIDEO: with reverse_mp4(BytesIO(r.content), original_gif.audio) as f: reversed_gif = gif_host.upload_video(f) # Defer to the object's unique method elif method == consts.OTHER: reversed_gif = gif_host.reverse() if reversed_gif: # Add gif to database if reversed_gif.log: add_to_database(gif_host.get_gif(), reversed_gif) # Reply print("Replying!", reversed_gif.url) reply(context, reversed_gif.url) return SUCCESS else: return UPLOAD_FAILURE
import praw from core.credentials import CredentialsLoader from core import constants as consts from core.context import CommentContext from core.video import VideoHostManager """These tests rely on comments on in my test subreddit so they'll need some work for other people to use""" credentials = CredentialsLoader().get_credentials() 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']) ghm = VideoHostManager(reddit) context = CommentContext(reddit, reddit.comment('eomsgqq'), ghm) print(vars(context)) # # video = ghm.host_names['Gfycat'].get_video(text="https://gfycat.com/SkeletalShallowArmadillo") # video.analyze()
def signal_handler(sig, frame): print("Exiting...") q.exit_queue() sys.exit(0) signal.signal(signal.SIGINT, signal_handler) while True: try: for i in q.get_jobs(): print(i, i.origin_host, i.origin_id) result = process_comment(reddit, original_context=CommentContext.from_json( reddit, dict(i.context))) if result == SUCCESS or result == USER_FAILURE: q.remove_job(i) time.sleep(consts.sleep_time * failure_counter / 4) except prawcore.exceptions.ResponseException as e: # Something funky happened q.exit_queue() print("Did a comment go missing?", e, vars(e)) time.sleep(consts.sleep_time) except prawcore.exceptions.RequestException: # Unable to connect to Reddit q.exit_queue() print("Unable to connect to Reddit, is the internet down?") time.sleep(consts.sleep_time * 2) except KeyboardInterrupt:
def process_comment(reddit, comment=None, queue=None, original_context=None): vhm = VideoHostManager(reddit) if not original_context: # If we were not provided context, make our own # Check if comment is deleted if not comment.author: print("Comment doesn't exist????") print(vars(comment)) return USER_FAILURE print("New request by " + comment.author.name) # Create the comment context object context = CommentContext(reddit, comment, vhm) 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 video from host # print(context.url) # video_host = VideoHost.open(context, reddit) # new_original_video = ghm.extract_video(context.url, context=context) new_original_video = context.url print(new_original_video) # If the link was not recognized, return # if not video_host: # return USER_FAILURE if not new_original_video: return USER_FAILURE # If the video was unable to be acquired, return # original_video = video_host.get_video() # if not original_video: # return USER_FAILURE if not new_original_video.id: return USER_FAILURE if queue: # Add to queue print("Adding to queue...") queue.add_job(context.to_json(), new_original_video) return SUCCESS # Check database for video before we reverse it video = check_database(new_original_video) # Requires new database setup # db_video = check_database(new_original_video) if video: # db_video # If we were asked to reupload, double check the video if context.reupload: print("Doing a reupload check...") if not is_reupload_needed(reddit, video): # No reupload needed, do normal stuff reply(context, video) print("No reupload needed") return SUCCESS else: # Reupload is needed, delete this from the database delete_from_database(video) print("Reupload needed") # Proceed as normal else: # If it was in the database, reuse it reply(context, video) return SUCCESS # Analyze how the video should be reversed # in_format, out_format = video_host.analyze() # If there was some problem analyzing, exit # if not in_format or not out_format: # return USER_FAILURE if not new_original_video.analyze(): return USER_FAILURE uploaded_video = None # This video cannot be uploaded and it is not our fault cant_upload = False # Try every option we have for reversing a video for file in new_original_video.files: original_video_file = file upload_video_host = vhm.get_upload_host(file) if not upload_video_host: print("File too large {}s {}MB".format( new_original_video.files[0].duration, new_original_video.files[0].size)) cant_upload = True continue else: cant_upload = False # Using the provided host, perform the upload for i in range(2): result = upload_video_host.upload(original_video_file.id, original_video_file.type, new_original_video.nsfw, original_video_file.audio) # If the host simply cannot accept this file at all if result == CANNOT_UPLOAD: cant_upload = True break # If the host was unable to accept the video at this time elif result == UPLOAD_FAILED: cant_upload = False continue # Try again? # No error and not None, success! elif result: uploaded_video = result break # If we have the uploaded video, break out and continue if uploaded_video: 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_video: return UPLOAD_FAILURE if uploaded_video: # Add video to database # if reversed_video.log: add_to_database(new_original_video, uploaded_video) # Reply print("Replying!", uploaded_video.url) reply(context, uploaded_video) return SUCCESS else: return UPLOAD_FAILURE
q.enter_queue() def signal_handler(sig, frame): print("Exiting...") q.exit_queue() sys.exit(0) signal.signal(signal.SIGINT, signal_handler) while True: try: for i in q.get_jobs(): print(i, i.origin_host, i.origin_id) result = process_comment(reddit, original_context=CommentContext.from_json(reddit, dict(i.context))) if result == SUCCESS or result == USER_FAILURE: q.remove_job(i) time.sleep(consts.sleep_time * failure_counter / 4) except prawcore.exceptions.ResponseException as e: # Something funky happened q.exit_queue() print("Did a comment go missing?", e, vars(e)) time.sleep(consts.sleep_time) except prawcore.exceptions.RequestException: # Unable to connect to Reddit q.exit_queue() print("Unable to connect to Reddit, is the internet down?") time.sleep(consts.sleep_time * 2) except KeyboardInterrupt: