Пример #1
0
    def analyze(self) -> bool:
        self.pic = self.host.API.get_gfycat(self.id)
        if not self.pic:
            return False
        try:
            self.id = self.pic['gfyName']
        except KeyError:
            print("GfyName missing, is it deleted?")
            return False

        self.url = self.pic["webmUrl"]
        self.type = consts.WEBM
        if self.url == "":  # If we received no URL, the GIF was brought down or otherwise missing
            print("{} gif missing".format(self.host.name))
            return False
        self.duration = self.pic['numFrames'] / self.pic['frameRate']
        audio = self.pic['hasAudio']
        frames = self.pic['numFrames']
        self.file = BytesIO(requests.get(self.url).content)
        if int(self.pic['nsfw']):
            print("{} says it's nsfw".format(self.host.name))
            # pprint(self.pic)
        # self.nsfw = self.nsfw or int(self.pic['nsfw']) # Gfycat's NSFW flag is essentially useless
        self.size = self.pic['webmSize'] / 1000000
        self.files.append(GifFile(self.file, self.host, self.type, self.size, self.duration, audio=audio))
        self.files.append(GifFile(self.file, self.host, consts.GIF, self.size, self.duration, frames, audio=audio))
        return True
Пример #2
0
    def analyze(self) -> bool:
        """Analyze an imgur gif and determine how to reverse and upload"""

        if not self.pic:
            return False
        # pprint(self.pic)

        if not self.pic['animated']:
            print("Not a gif!")
            return False
        r = requests.get(self.pic['mp4'])
        file = BytesIO(r.content)
        self.duration = get_duration(file)

        self.files.append(GifFile(file, host=self.host, gif_type=consts.MP4, duration=self.duration,
                                  size=self.pic['mp4_size']/1000000))

        # If the file type is a gif, add it as an option and prioritize it
        if self.pic['type'] == 'image/gif':
            r = requests.get(self.pic['gifv'][:-1])
            gif = BytesIO(r.content)
            if is_valid(gif):
                gif_file = GifFile(gif, host=self.host, gif_type=consts.GIF, duration=self.duration)
            # else:
            #     gif_file = GifFile(file, host=self.host, gif_type=consts.GIF, duration=self.duration)
                print("added gif file")
                self.files.insert(0, gif_file)
        print(self.files)
        return True
Пример #3
0
    def analyze(self) -> bool:
        headers = {"User-Agent": consts.spoof_user_agent}
        r = requests.get("https://v.redd.it/{}".format(self.id),
                         headers=headers)
        submission_id = REPatterns.reddit_submission.findall(r.url)
        url = None
        audio = False
        if not submission_id:
            print("Deleted?")
            return False
        elif submission_id[0][2]:
            submission = self.host.ghm.reddit.submission(
                id=submission_id[0][2])
            if submission.is_video:
                url = submission.media['reddit_video']['fallback_url']
        else:  # Maybe it was deleted?
            print("Deleted?")
            return False

        r = requests.get(url)
        file = BytesIO(r.content)

        r = requests.get("https://v.redd.it/{}/audio".format(self.id),
                         headers=headers)
        if r.status_code == 200:
            file = concat(file, BytesIO(r.content))
            audio = True

        self.type = consts.MP4
        self.size = file.getbuffer().nbytes / 1000000
        self.files.append(
            GifFile(file, self.host, self.type, self.size, audio=audio))
        self.files.append(GifFile(file, self.host, consts.GIF, self.size))
        return True
Пример #4
0
 def analyze(self):
     r = requests.get(self.url)
     file = BytesIO(r.content)
     if not is_valid(file):
         return False
     file = GifFile(file, self.host, consts.GIF)
     self.files.append(file)
     vid_file = GifFile(file,
                        self.host,
                        self.id.split(".")[-1],
                        audio=False)
     if self.id[-3:] == consts.GIF:
         self.files.append(vid_file)
     else:
         self.files.insert(0, vid_file)
     return True
Пример #5
0
 def analyze(self):
     info = streamable.download_video(self.id)
     if not info:
         return False
     file = BytesIO(requests.get(info['url']).content)
     self.files.append(GifFile(file, host=self.host, gif_type=consts.MP4, audio=False, duration=info['duration'],
                               size=info['size']/1000000))
     return True
Пример #6
0
    def analyze(self) -> bool:
        headers = {"User-Agent": consts.spoof_user_agent}
        r = requests.get("https://v.redd.it/{}".format(self.id), headers=headers)
        submission_id = REDDIT_SUBMISSION.findall(r.url)
        url = None
        audio = False
        if not submission_id:
            print("Deleted?")
            return False
        elif submission_id[0][3]:
            submission = self.host.ghm.reddit.submission(id=submission_id[0][3])
            try:
                if submission.is_video:
                    if submission.media:
                        if submission.media['reddit_video'].get('fallback_url', None):
                            url = submission.media['reddit_video']['fallback_url']
                        elif submission.media['reddit_video'].get("transcoding_status", None) == "error":
                            print("Reddit had an error transcoding this video")
                            return False
                    else:
                        print("Submission is video but there is no media data")
                        return False
            except ResponseException as e:
                print("Video is inaccessible, likely deleted")
                return False

        else:  # Maybe it was deleted?
            print("Deleted?")
            return False

        r = requests.get(url)
        if r.status_code != 200:
            print("Getting Reddit Video returned status, deleted?", r.status_code)
            return False
        file = BytesIO(r.content)

        r = requests.get("https://v.redd.it/{}/DASH_audio.mp4".format(self.id), headers=headers)
        if r.status_code == 200:
            file = concat(file, BytesIO(r.content))
            audio = True

        if not audio:
            # Audio could be here instead
            r = requests.get("https://v.redd.it/{}/audio".format(self.id), headers=headers)
            if r.status_code == 200:
                file = concat(file, BytesIO(r.content))
                audio = True

        self.type = consts.MP4
        self.size = file.getbuffer().nbytes / 1000000
        self.files.append(GifFile(file, self.host, self.type, self.size, audio=audio))
        # self.files.append(GifFile(file, self.host, consts.GIF, self.size))
        return True
Пример #7
0
    def analyze(self) -> bool:
        """Analyze an imgur gif using the imgurpython library and determine how to reverse and upload"""

        if not self.pic:
            return False
        pprint(vars(self.pic))

        if not self.pic.animated:
            print("Not a gif!")
            return False
        r = requests.get(self.pic.mp4)
        file = BytesIO(r.content)
        self.duration = get_duration(file)

        self.files.append(
            GifFile(file,
                    host=self.host,
                    gif_type=consts.MP4,
                    duration=self.duration,
                    size=self.pic.mp4_size / 1000000))

        # If the file type is a gif, add it as an option and prioritize it
        if self.pic.type == 'image/gif':
            r = requests.get(self.pic.gifv[:-1])
            gif = BytesIO(r.content)
            if is_valid(gif):
                gif_file = GifFile(gif,
                                   host=self.host,
                                   gif_type=consts.GIF,
                                   duration=self.duration)
            else:
                gif_file = GifFile(file,
                                   host=self.host,
                                   gif_type=consts.GIF,
                                   duration=self.duration)
            self.files.insert(0, gif_file)

        return True
Пример #8
0
 def analyze(self) -> bool:
     # Safely download the gif to determine if it
     self.size = get_response_size(self.url, 400)
     if not self.size:
         return False
     # Is it a gif?
     r = requests.get(self.url)
     header = r.content[:3]
     if header != b'GIF':
         return None
     self.type = consts.GIF
     self.file = BytesIO(r.content)
     self.files.append(
         GifFile(self.file, self.host, self.type, self.size, self.duration))
     return True
Пример #9
0
 def analyze(self) -> bool:
     # Safely download the gif to determine if it
     self.size = get_response_size(self.url, 400)
     if not self.size:
         return False
     # Is it a gif?
     headers = {"User-Agent": consts.spoof_user_agent}
     try:
         r = requests.get(self.url, headers=headers)
     except ConnectionError as e:
         print("got rejected, waiting for a second")
         time.sleep(15)
         r = requests.get(self.url, headers=headers)
     header = r.content[:3]
     if header != b'GIF':
         return None
     self.type = consts.GIF
     self.file = BytesIO(r.content)
     self.files.append(
         GifFile(self.file, self.host, self.type, self.size, self.duration))
     return True
Пример #10
0
 def analyze(self) -> bool:
     self.type = consts.GIF
     self.file = BytesIO(requests.get(self.url).content)
     self.size = self.file.getbuffer().nbytes / 1000000
     self.files.append(GifFile(self.file, self.host, self.type, self.size))
     return True
Пример #11
0
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