Example #1
0
 def test_ordering(self):
     s1 = Status(user='******',
                 _value=Status.ABERTA,
                 _datetime=pendulum.yesterday()).save()
     Status(user='******', _value=Status.FECHADA,
            _datetime=pendulum.now()).save()
     self.assertAlmostEqual(Status.objects.count(), 2)
     self.assertEqual(Status.objects.first(), s1)
Example #2
0
def status(bot, update):
    """Send a sticker or message with the status of our home."""
    _status = Status.now()
    user = update.message.from_user
    if _status is not None:
        bot.send_sticker(sticker=Status.now().sticker, chat_id=user.id)
    else:
        update.message.reply_text(I_DONT_KNOW)
Example #3
0
    def test_set_value(self):
        s = Status.aberta('123')
        self.assertAlmostEqual(Status.objects.count(), 1)
        self.assertEqual(Status.now().value,
                         Status.CHOICES.get(Status.ABERTA).get('name'))

        s.value = 'f'
        self.assertEqual(s._value, 'f')

        s.value = 'a'
        self.assertEqual(s._value, 'a')

        self.assertRaises(ValueError, setattr, s, 'value', 'TESTE')
Example #4
0
def post(bot, db):
    try: 
        img = db.get_image()
        logging.info("Posting: {}".format(img))
        if False: #len(img.statuses):
            new_caption = (0,img.caption)
            for s in img.statuses:
                try:
                    favs = bot.get_favs(s.twitter_status_id)
                    if favs > new_caption[0]:
                        new_caption = (favs, s.twitter_status_text)
                except Exception as e:
                    logging.error("%s ignoring", e)
            img.caption = new_caption[1].replace(Config.get("twitter", "screen_name"), '')
        
        if img.caption != None: 
            status = img.caption
        else:
            status = Config.get("messages", "missing_caption")
        status_id = bot.publish(os.path.join(Config.get("main", "images_dir"),img.filename), status)
        img.times_published+=1
        img.statuses.append(Status(twitter_status_id=status_id, twitter_status_text=status))
        logging.info("Updating image to {}".format(img))
        db.update(img)
    except Exception as e:
        logging.error("Error while posting: %s", e)
Example #5
0
def fechada(bot, update):
    """Set the status of our home as closed and send referring sticker."""
    user = update.message.from_user
    if BotTelegramCore.is_admin(user.id):
        _status = Status.fechada(user.username)
        bot.send_sticker(sticker=_status.sticker,
                         chat_id=BotTelegramCore.instance().chat_id)
    else:
        update.message.reply_text(UNAUTHORIZED)
Example #6
0
def register(bot, update, args):
    args_msg = """Error, please register using the command:
```
/register [ssc_email] [ssc_password]
```

*WARNING: Your account information will be stored in plain text, use this bot at your own risk*"""

    exists_msg = """Error, account already exists.

To update your account information please use the command:
```
/update [ssc_email] [ssc_password]
```

*WARNING: Your account information will be stored in plain text, use this bot at your own risk*"""

    success_msg = "Registered successfully!"

    if len(args) != 2:
        bot.send_message(chat_id=update.message.chat_id,
                         text=args_msg,
                         parse_mode="Markdown")
        return

    telegram_id = update['message']['chat']['id']
    username = update['message']['chat']['username']

    ssc_username = args[0]
    ssc_password = args[1]

    session = Session()
    user = session.query(User).filter_by(telegram_id=telegram_id).first()

    if user:
        bot.send_message(chat_id=update.message.chat_id,
                         text=exists_msg,
                         parse_mode="Markdown")
        session.close()
        return

    user = User(telegram_id, username, ssc_username, ssc_password)
    status = Status(telegram_id)
    session.add(user)
    session.add(status)
    session.commit()
    session.close()

    bot.send_message(chat_id=update.message.chat_id,
                     text=success_msg,
                     parse_mode="Markdown")
    return
Example #7
0
    def replies(self, min_fav=1):
        user_timeline = self.api.user_timeline()
        if len(user_timeline) == 0:
            return dict()
        last = min([status.id_str for status in user_timeline])
        me = self.api.me()

        replies = dict()

        for t in self.api.search(q="@{}".format(me.screen_name),
                                 since_id=last):
            if t.favorite_count >= min_fav:
                if not t.in_reply_to_status_id_str in replies:
                    replies[t.in_reply_to_status_id_str] = []

                replies[t.in_reply_to_status_id_str].append(
                    Status(twitter_status_id=t.id_str,
                           twitter_status_text=t.text))

        return replies
Example #8
0
 def test_sticker(self):
     s = Status.aberta('123')
     self.assertAlmostEqual(Status.objects.count(), 1)
     self.assertEqual(s.sticker,
                      Status.CHOICES.get(Status.ABERTA).get('sticker'))
Example #9
0
 def test_get_value(self):
     s = Status.aberta('123')
     self.assertAlmostEqual(Status.objects.count(), 1)
     self.assertEqual(s.value,
                      Status.CHOICES.get(Status.ABERTA).get('name'))
Example #10
0
 def test_fechada(self):
     Status.fechada('123')
     self.assertAlmostEqual(Status.objects.count(), 1)
     self.assertEqual(Status.objects.first()._value, Status.FECHADA)
Example #11
0
 def test_aberta(self):
     Status.aberta('123')
     self.assertAlmostEqual(Status.objects.count(), 1)
     self.assertEqual(Status.objects.first()._value, Status.ABERTA)