Esempio n. 1
0
    def add(cls, story):
        story_id = str(story.get('id'))
        if memcache.get(story_id):
            logging.info('STOP: {} in memcache'.format(story_id))
            return
        if ndb.Key(cls, story_id).get():
            logging.info('STOP: {} in DB'.format(story_id))
            memcache.set(story_id, 1)
            return
        logging.info('SEND: {}'.format(story_id))
        story['title'] = story.get('title').encode('utf-8')

        hn_url = "https://news.ycombinator.com/item?id={}".format(story_id)
        short_hn_url = shorten_url(hn_url)
        comments_count = story.get('descendants', 0)
        story_url = story.get('url')
        buttons = []

        if story_url:
            short_url = shorten_url(story_url)
            buttons.append({'text': 'Read', 'url': story_url})
        else:
            short_url = short_hn_url
            story['url'] = hn_url

        buttons.append({
            'text': '{}+ Comments'.format(comments_count),
            'url': hn_url
        })

        # Add title
        message = '<b>{title}</b> (Score: {score}+)\n\n'.format(**story)
        # Add link
        message += '<b>Link:</b> {}\n'.format(short_url)
        # Add comments Link(don't add it for `Ask HN`, etc)
        if story_url:
            message += '<b>Comments:</b> {}\n'.format(short_hn_url)
        # Add text
        text = story.get('text')
        if text:
            text = text.replace('<p>', '\n').replace('&#x27;', "'") \
                       .replace('&#x2F;', '/').encode('utf-8')
            message += "\n{}\n".format(text)

        if development():
            result = send_message('@hacker_news_feed_st', message,
                                  {'inline_keyboard': [buttons]})
        else:
            result = send_message('@hacker_news_feed', message,
                                  {'inline_keyboard': [buttons]})
        if result:
            cls(id=story_id,
                title=story.get('title'),
                url=story.get('url'),
                score=story.get('score'),
                text=story.get('text'),
                short_url=short_url,
                short_hn_url=short_hn_url).put()
            memcache.set(story_id, 1)
Esempio n. 2
0
    def add(cls, story):
        story_id_int = story.get('id')
        story_id = str(story_id_int)
        short_id = shortener.encode(story_id_int)
        hn_url = "https://news.ycombinator.com/item?id={}".format(story_id)
        story_url = story.get('url')

        # Check memcache and databse, maybe this story was already sent
        if memcache.get(story_id):
            logging.info('STOP: {} in memcache'.format(story_id))
            return
        post = ndb.Key(cls, story_id).get()
        if post:
            logging.info('STOP: {} in DB'.format(story_id))
            post.add_memcache()
            return
        logging.info('SEND: {}'.format(story_id))

        story['title'] = story.get('title').encode('utf-8')
        comments_count = story.get('descendants', 0)
        buttons = []

        if development():
            short_hn_url = 'http://*****:*****@hacker_news_500', message,
                                  {'inline_keyboard': [buttons]})
        else:
            result = send_message(CHANNEL_NAME, message,
                                  {'inline_keyboard': [buttons]})

        logging.info('Telegram response: {}'.format(result))

        telegram_message_id = None
        if result and result.get('ok'):
            telegram_message_id = result.get('result').get('message_id')
        post = cls(id=story_id,
                   title=story.get('title'),
                   url=story.get('url'),
                   score=story.get('score'),
                   text=story.get('text'),
                   short_url=short_url,
                   short_hn_url=short_hn_url,
                   message=message,
                   telegram_message_id=telegram_message_id)
        post.put()
        post.add_memcache()
Esempio n. 3
0
  def add(cls, story):
    story_id_int = story.get('id')
    story_id = str(story_id_int)
    short_id = shortener.encode(story_id_int)
    hn_url = "https://news.ycombinator.com/item?id={}".format(story_id)
    story_url = story.get('url')

    # Check memcache and databse, maybe this story was already sent
    if memcache.get(story_id):
      logging.info('STOP: {} in memcache'.format(story_id))
      return
    post = ndb.Key(cls, story_id).get()
    if post:
      logging.info('STOP: {} in DB'.format(story_id))
      post.add_memcache()
      return
    logging.info('SEND: {}'.format(story_id))

    story['title'] = story.get('title').encode('utf-8')
    comments_count = story.get('descendants', 0)
    buttons = []

    if development():
      short_hn_url = 'http://*****:*****@hacker_news_feed_st', message,
                            {'inline_keyboard': [buttons]})
    else:
      result = send_message('@hacker_news_feed', message,
                            {'inline_keyboard': [buttons]})

    logging.info('Telegram response: {}'.format(result))

    telegram_message_id = None
    if result and result.get('ok'):
      telegram_message_id = result.get('result').get('message_id')
    post = cls(id=story_id, title=story.get('title'), url=story.get('url'),
               score=story.get('score'), text=story.get('text'),
               short_url=short_url, short_hn_url=short_hn_url,
               message=message, telegram_message_id=telegram_message_id)
    post.put()
    post.add_memcache()
Esempio n. 4
0
    def add(cls, story):
        story_id = story.get('id')
        story_id_int = int(story_id)
        short_id = shortener.encode(story_id_int)
        dn_url = "https://www.designernews.co/stories/{}".format(story_id)
        story_url = story.get('url')

        # Check memcache and database, maybe this story was already sent
        if memcache.get(story_id):
            logging.info('STOP: {} in memcache'.format(story_id))
            return
        post = ndb.Key(cls, story_id).get()
        if post:
            logging.info('STOP: {} in DB'.format(story_id))
            post.add_memcache()
            return
        logging.info('SEND: {}'.format(story_id))

        story['title'] = story.get('title').encode('utf-8')
        comments_count = story.get('comment_count', 0)
        buttons = []

        if development():
            short_dn_url = 'http://*****:*****@designer_news_st', message,
                                  {'inline_keyboard': [buttons]})
        else:
            result = send_message('@designer_news', message,
                                  {'inline_keyboard': [buttons]})

        logging.info('Telegram response: {}'.format(result))

        telegram_message_id = None
        if result and result.get('ok'):
            telegram_message_id = result.get('result').get('message_id')
        post = cls(id=story_id,
                   title=story.get('title'),
                   url=story.get('url'),
                   score=story.get('vote_count'),
                   text=story.get('comment'),
                   short_url=short_url,
                   short_dn_url=short_dn_url,
                   message=message,
                   telegram_message_id=telegram_message_id)
        post.put()
        post.add_memcache()
    def add(cls, story, with_bot):
        logging.debug("adding into database with bot %s" % with_bot)
        story_id = str(story.get('fbid'))
        if cls.checkPostIdAlreadyExist(story_id):
            return
        logging.info('SEND: {}'.format(story_id))

        short_url = "https://fb.com/{}".format(story_id)
        # app_url = "fb://post/{}".format(story_id)
        mobile_url = 'https://m.facebook.com/story.php?story_fbid={}&id={}'.format(
            *list(reversed(story_id.split('_'))))

        message = u'\U0001f517 {}\n'.format(short_url)
        # message = u'\U0001f4f1 {}\n'.format(app_url)
        message += u'\U0001f4f1 {}\n'.format(mobile_url)
        message += u'\n{}\n\n'.format(story.get('message'))

        reactions = 0
        str_reaction_icons = ''
        if story.get('likes'):
            reactions += story.get('likes')
            str_reaction_icons += u'\U0001f44d'
        if story.get('loves'):
            reactions += story.get('loves')
            str_reaction_icons += u'\u2764'
        if story.get('wows'):
            reactions += story.get('wows')
            str_reaction_icons += u'\U0001f632'
        if story.get('hahas'):
            reactions += story.get('hahas')
            str_reaction_icons += u'\U0001f606'
        if story.get('sads'):
            reactions += story.get('sads')
            str_reaction_icons += u'\U0001F622'
        if story.get('angries'):
            reactions += story.get('angries')
            str_reaction_icons += u'\U0001F621'
        if reactions > 0:
            message += str_reaction_icons
            message += ' %d' % reactions
            message += '\n'

        message += u'%d 回應  %d 分享' % (story.get('comments'),
                                      story.get('shares'))

        if development():
            result = send_message(with_bot, '@d09n0fcuk_hk429_b0t_7327',
                                  message)
        else:
            result = send_message(with_bot, '@hk_acg_feeds', message)
        if result:
            logging.debug("writing into datastore: %s" % story.get('fbid'))
            logging.debug(story)
            cls(id=story.get('fbid'),
                message=story.get('message'),
                draft_time=story.get('draft_time'),
                post_time=story.get('post_time'),
                likes=story.get('likes'),
                loves=story.get('loves'),
                wows=story.get('wows'),
                hahas=story.get('hahas'),
                sads=story.get('sads'),
                angries=story.get('angries'),
                comments=story.get('comments'),
                shares=story.get('shares')).put()
            memcache.set(story_id, 1)