def callback(bot: telegram.Bot, update: telegram.Update): query = update.callback_query like_type = query.data if settings.DEBUG: logger.debug('update: %s', update) chat_tg_id = query.message.chat_id message_tg_id = query.message.message_id user_tg_id = query.from_user.id try: with db: msg, created = Message.get_or_create( chat_tg_id=chat_tg_id, message_tg_id=message_tg_id, defaults={'user_tg_id': -1}, ) if created: logger.warning('missing message: chat_id = %s, msg_id = %s', chat_tg_id, message_tg_id) like = msg.likes.filter(Like.user_tg_id == user_tg_id).first() if like: if like_type == str(like.type): like.delete_instance() else: like.type = like_type like.save() else: if user_tg_id == msg.user_tg_id: like_dict = msg.get_likes_by_type() if like_type == str(Like.DISLIKE) and like_dict[ Like.LIKE] < settings.DELETE_THRESHOLD: bot.delete_message(chat_tg_id, message_tg_id) query.answer() return else: Like.create(message_id=msg.id, user_tg_id=user_tg_id, type=like_type) like_dict = msg.get_likes_by_type() if like_dict[Like.LIKE] + settings.DELETE_THRESHOLD <= like_dict[ Like.DISLIKE] + like_dict[Like.OLD]: bot.delete_message(chat_tg_id, message_tg_id) else: reply_markup = get_reply_markup(like_dict) kwargs = dict(chat_id=chat_tg_id, message_id=message_tg_id, reply_markup=reply_markup) if query.message.text: bot.edit_message_text(text=query.message.text, **kwargs) else: bot.edit_message_caption(caption=query.message.caption, **kwargs) except Exception: logger.exception('exception while adding like %s', update) query.answer()
def like_post(driver): url = get_url(driver) try: Like.select().where(Like.url == url).get() except Like.DoesNotExist: pass else: log.info('Post has already been liked {url}'.format(url=url)) return False try: like_btn = driver.find_element_by_xpath("//span[@aria-label='Like']") except NoSuchElementException: log.info('Could not find like button {}'.format(driver.current_url)) time.sleep(1) return False else: log.info('Found like button. Trying to like {}'.format( driver.current_url)) like_btn.click() Like.create(url=url) log.info('Liked picture {url}'.format(url=url)) return True
def move_db() -> None: logging_settings = settings.LOGGING logging_settings['loggers']['peewee']['level'] = 'INFO' logging.config.dictConfig(settings.LOGGING) sqlite_db = get_db() initdb.create_tables() with peewee_db: cur = sqlite_db.execute('select count(*) AS cnt from messages') messages_count = cur.fetchone()['cnt'] cur = sqlite_db.execute('select chat_id, message_id, user_id from messages') logger.info('moving %s messages', messages_count) for index, row in enumerate(cur): if index % 100 == 0: logger.info('%s messages...', index) Message.get_or_create( chat_tg_id=row['chat_id'], message_tg_id=row['message_id'], user_tg_id=row['user_id'], ) cur = sqlite_db.execute('select count(*) AS cnt from likes') likes_count = cur.fetchone()['cnt'] cur = sqlite_db.execute('select chat_id, message_id, user_id, like_type from likes') logger.info('moving %s likes', likes_count) for index, row in enumerate(cur): if index % 100 == 0: logger.info('%s likes...', index) msg, _ = Message.get_or_create( chat_tg_id=row['chat_id'], message_tg_id=row['message_id'], defaults={'user_tg_id': -1}, ) like = Like.filter( Like.message_id == msg.id, Like.user_tg_id == row['user_id'], ).first() if like: like.type = row['like_type'] like.save() else: Like.create( message_id=msg.id, user_tg_id=row['user_id'], type=row['like_type'], )
def post(self): data = parser_like.parse_args() app.logger.info(request) like = data['like'] post_id = data['post_id'] username = get_jwt_identity() user_id = User.select().where(User.username == username) if Post.select().where((Post.user_id == user_id) & (Post.id == post_id)).count() > 0: app.logger.info( 'This post is yours. You are not entitled to this action') return { 'message': 'This post is yours. You are not entitled to this action', 'code': 200 } else: if like == 'True': if Like.select().where((Like.user_id == user_id) & (Like.post_id == post_id)).count() > 0: resp = jsonify({ 'refresh': True, 'action': 'like', 'code': 200 }) return resp elif Unlike.select().where((Unlike.user_id == user_id) & ( Unlike.post_id == post_id)).count() > 0: try: category = Unlike.get((Unlike.user_id == user_id) & (Unlike.post_id == post_id)) category.delete_instance() app.logger.info( 'delete_instance_Unlike{}'.format(post_id)) except: app.logger.error('Something went wrong (False)') return { 'message': 'Something went wrong (False)', 'code': 500 } try: Like.create(user_id=user_id, post_id=post_id) resp = jsonify({ 'refresh': True, 'action': 'like', 'code': 200 }) return resp except: app.logger.error( 'Something went wrong (delete_instance_Unlike)') return { 'message': 'Something went wrong (delete_instance_Unlike)', 'code': 500 } elif like == 'False': if Unlike.select().where((Unlike.user_id == user_id) & ( Unlike.post_id == post_id)).count() > 0: resp = jsonify({ 'refresh': True, 'action': 'unlike', 'code': 200 }) return resp elif Like.select().where((Like.user_id == user_id) & ( Like.post_id == post_id)).count() > 0: try: category = Like.get((Like.user_id == user_id) & (Like.post_id == post_id)) category.delete_instance() app.logger.info( 'delete_instance_Like{}'.format(post_id)) except: app.logger.error( 'Something went wrong (delete_instance_Like)') return { 'message': 'Something went wrong (delete_instance_Like)', 'code': 500 } try: Unlike.create(user_id=user_id, post_id=post_id) # resp = jsonify({ 'refresh': True, 'action': 'unlike', 'code': 200 }) return resp except: app.logger.error('Something went wrong (False)') return { 'message': 'Something went wrong (False)', 'code': 500 }
def test_api(db_connection): u1 = User.create(username="******") u2 = User.create(username="******") u3 = User.create(username="******") u4 = User.create(username="******") u5 = User.create(username="******") u6 = User.create(username="******") u7 = User.create(username="******") Follow.create(follower=u1, followee=u2) Follow.create(follower=u1, followee=u3) Follow.create(follower=u1, followee=u4) Follow.create(follower=u2, followee=u1) Follow.create(follower=u2, followee=u6) Follow.create(follower=u3, followee=u2) Follow.create(follower=u4, followee=u3) Follow.create(follower=u4, followee=u5) Follow.create(follower=u5, followee=u1) Follow.create(follower=u6, followee=u2) Follow.create(follower=u6, followee=u3) Follow.create(follower=u6, followee=u7) Follow.create(follower=u7, followee=u1) Follow.create(follower=u7, followee=u2) p1 = Post.create(author=u2, content="lorem ipsum") p2 = Post.create(author=u3, content="dolorum") p3 = Post.create(author=u6, content="foo bar") Like.create(user=u1, post=p1) Like.create(user=u1, post=p2) Like.create(user=u1, post=p3) schema = Schema(query=Query) query = """ { users(usernames: ["user1"]) { username, following { username followers { username } } likes { author { username } content } } posts { content } } """ result = schema.execute(query) assert result.data == { u"users": [{ u"following": [ { u"followers": [ { u"username": u"user1" }, { u"username": u"user3" }, { u"username": u"user6" }, { u"username": u"user7" }, ], u"username": u"user2", }, { u"followers": [ { u"username": u"user1" }, { u"username": u"user4" }, { u"username": u"user6" }, ], u"username": u"user3", }, { u"followers": [{ u"username": u"user1" }], u"username": u"user4" }, ], u"likes": [ { u"author": { u"username": u"user2" }, u"content": u"lorem ipsum" }, { u"author": { u"username": u"user3" }, u"content": u"dolorum" }, { u"author": { u"username": u"user6" }, u"content": u"foo bar" }, ], u"username": u"user1", }], u"posts": [ { u"content": u"lorem ipsum" }, { u"content": u"dolorum" }, { u"content": u"foo bar" }, ] } assert db_connection.queries_executed == 2