예제 #1
0
    def test_get_post(self):
        # text post
        post, status, err_msg = reddit.get_post('r/showerthoughts')
        self.assertEqual(status, 'success')
        self.assertIsNone(err_msg)
        self.assertEqual(post.subreddit.lower(), 'r/showerthoughts')
        self.assertIsNotNone(post.title)
        self.assertIn('https://www.reddit.com/r/showerthoughts', post.footer)

        # photo post
        post, status, err_msg = reddit.get_post('r/pics')
        self.assertEqual(status, 'success')
        self.assertIsNone(err_msg)
        self.assertEqual(post.subreddit.lower(), 'r/pics')
        self.assertIsNotNone(post.title)
        self.assertIn('https://www.reddit.com/r/pics', post.footer)
        self.assertIsNotNone(post.media_url)
        self.assertEqual(post.media_type, 'photo')
예제 #2
0
def tweet(api):
    img_valid = True
    subreddit_value = reddit.get_subreddit()
    reddit.get_post(subreddit_value)
    title, url, image_url = reddit.get_post(subreddit_value)
    print(title)
    print(url)
    print(image_url)
    try:
        urllib.request.urlretrieve(image_url, "img.jpg")
    except:
        img_valid = False

    if img_valid:
        api.update_with_media(
            "img.jpg", "Top post from r/" + subreddit_value.display_name +
            "\n" + url + "\n\n" + title)
    else:
        api.update_status("Top post from r/" + subreddit_value + "\n" + url +
                          "\n\n" + title)
예제 #3
0
def process_mention(mention):
    """ Processes user mentions."""
    requester = str(mention.author).lower()  # Author of the mention
    # subject = str(mention.subject).lower()  # Subject of the mention
    content = str(mention.body)  # Content of the mention
    parent_id = str(mention.parent_id)  # parent ID of the mention
    comment_id = str(mention.name)  # comment id

    logging.info("%s:%s: Processing User Mention...", parent_id, requester)

    # get info for comment
    comment_id = comment_id.replace("t1_", "")
    comment = reddit.get_comment(comment_id)

    # check whether mention is a top level comment
    if "t3_" in parent_id:
        post_id = parent_id.replace("t3_", "")
    else:
        logging.info("%s:%s: Mention is not a top level comment. Ending <process_mention> process.", parent_id, requester)
        error = "  \n User mention must be a top level comment. "
        reddit.post_comment(comment, config.parse_mention_errormessage + error, parent_id, requester)
        return

    # get info for post
    post = reddit.get_post(post_id)
    post_author = str(post.author).lower()

    # only OP should be able to run the giveaway
    if requester == post_author:
        logging.info("%s:%s: Requester matches giveaway OP, processing request.", parent_id, requester)

        giveaway_args = utils.parse_pm(content, requester, parent_id)
        if giveaway_args is None:
            error = "  \n Double check the formatting and try again."
            reddit.post_comment(comment, config.parse_mention_errormessage + error, parent_id, requester)
            logging.warning("%s:%s: Failed to parse arguments, check formatting. Ending <process_mention> process.",
                        parent_id, requester)
            return

        # args OK, launch giveaway
        identifier = str(utils.gen_code())
        job_id_mention = '%s:%s:PROCESS_MENTION' % (identifier, requester)
        codes = None
        utils.scheduler.add_job(process, id=job_id_mention,
                                args=[requester, identifier, giveaway_args, codes, post, comment])
        logging.info("%s:%s: Completed, OK", parent_id, requester)
    else:
        error = "  \n Only the OP can run a giveaway in this post.  \n ^Your ^username ^does ^not ^match ^OP."
        reddit.post_comment(comment, config.parse_mention_errormessage + error, parent_id, requester)
        logging.info("%s:%s: Requester does not match giveaway OP. Ending <process_mention> process.", parent_id, requester)
        return
예제 #4
0
def edit_result(bot, update):
    '''
    Edits the given message with a new post from that subreddit, and edits the
    keyboard markup to give the user the ability to edit or confirm the message.
    '''
    message = update.effective_message
    chat_id = message.chat_id
    message_id = message.message_id
    text = (message.caption or message.text) + '\n'
    subreddit = helpers.get_subreddit_name(text)
    if subreddit is None:
        return

    keyboard = InlineKeyboardMarkup([[
        InlineKeyboardButton(text="↻", callback_data="edit"),
        InlineKeyboardButton(text="✓", callback_data="send")
    ]])
    tries = 0
    while tries < MAX_TRIES:
        post, status, err_msg = reddit.get_post(subreddit)
        if status != 'success':
            continue
        if '/comments/' not in post.content_url and message.caption is not None:
            media = InputMediaPhoto(post.content_url, post.msg, 'Markdown')
            bot.editMessageMedia(chat_id,
                                 message_id,
                                 media=media,
                                 reply_markup=keyboard)
            break
        elif '/comments/' in post.content_url and message.caption is None:
            bot.editMessageText(post.msg,
                                chat_id,
                                message_id,
                                parse_mode='Markdown',
                                reply_markup=keyboard,
                                disable_web_page_preview=True)
            break
        tries += 1

    if tries >= MAX_TRIES:
        keyboard = InlineKeyboardMarkup([[
            InlineKeyboardButton(text="Retry  ↻", callback_data="edit"),
            InlineKeyboardButton(text="✓", callback_data="send")
        ]])
        bot.editMessageReplyMarkup(chat_id, message_id, reply_markup=keyboard)
예제 #5
0
def send_post(bot, chat_id, subreddit=None, post_url=None):
    '''
    Sends a random post from the given subreddit if not None, sends the post
    from post_url otherwise.
    '''
    post, status, err_msg = reddit.get_post(subreddit, post_url)
    if status == 'not_found':
        bot.sendMessage(chat_id, err_msg)
    elif status != 'success':
        return status, err_msg

    try:
        # if it is not a random post (e.g. shared via link) don't show the edit
        # custom keyboard
        if post_url:
            keyboard = InlineKeyboardMarkup([[
                InlineKeyboardButton(text="Show another one",
                                     callback_data="more")
            ]])
        else:
            keyboard = InlineKeyboardMarkup([[
                InlineKeyboardButton(text="↻", callback_data="edit"),
                InlineKeyboardButton(text="✓", callback_data="send")
            ]])
        if 'www.youtube.com' in post.media_url:
            bot.sendMessage(chat_id, f"I'm sorry, youtube videos are not"
                            "supported yet :(",
                            parse_mode='Markdown')
            return 'success', None

        if '/comments/' in post.content_url:
            # check if the post is a text post
            bot.sendMessage(chat_id,
                            text=post.msg,
                            parse_mode='Markdown',
                            reply_markup=keyboard,
                            disable_web_page_preview=True)
        elif post.media_type == 'gif':
            bot.sendDocument(chat_id,
                             post.media_url,
                             caption=post.msg,
                             parse_mode='Markdown',
                             reply_markup=keyboard,
                             disable_web_page_preview=True)
        elif post.media_type == 'video':
            bot.sendVideo(chat_id,
                          post.media_url,
                          caption=post.msg,
                          parse_mode='Markdown',
                          reply_markup=keyboard,
                          disable_web_page_preview=True)
        else:
            bot.sendPhoto(chat_id,
                          post.media_url,
                          caption=post.msg,
                          parse_mode='Markdown',
                          reply_markup=keyboard,
                          disable_web_page_preview=True)

    except Exception as e:
        capture_exception(e)
        traceback.print_exc()
        return 'retry', "I'm sorry, an error occurred in sending the post"\
            " :(\nThe developer must have missed an if statement!"

    return 'success', None