Esempio n. 1
0
    def post(self):
        try:
            data = self.parser.parse_args()['groups']
            body = data['body']
            date = data['date']
            user_id = get_jwt_identity()
            user: UserModel = UserModel.find_by_id(user_id)
            user.groups_date = date
            groups = dict()
            for group in body:
                link = group['link']
                name = group['name']
                number = group['number']
                group_id = re.search(r'https://my.ukma.edu.ua/course/(\d+)',
                                     link).group(1)
                groups[group_id] = number
                subject_model = GroupModel(name, group_id, group['number'],
                                           user_id)
                subject_model.save_to_db()
                signuper = GroupSignuper(groups, user.get_cookies())
                if date is None:
                    signuper.execute()
                else:
                    telegram.start(signuper)

        except Exception as e:
            logger.error(e)
            raise e
            # return {"message": "Some error occurred"}, 500
        return {"message": "Successfully added Groups"}, 201
Esempio n. 2
0
    def post(self, name):
        if GroupModel.find_by_name(name):
            return {'message': "A group with name '{}' already exists.".format(name)}, 400

        group = GroupModel(name)
        try:
            group.save_to_db()
        except Exception as e:
            return {"message": f"An error occurred: {e}"}, 500

        return group.json(), 201
Esempio n. 3
0
    def post(self):
        data = Group.parser.parse_args()

        if GroupModel.find_by_name(data['name']):
            return {'message': 'Group with that name already exists.'}, 400

        group = GroupModel(data['name'], data['description'])

        group.save_to_db()

        return group.json(), 201
Esempio n. 4
0
 def post(self, name=None):
     data = json.loads(request.data)
     name = data.get("name", name)
     if GroupModel.find_by_name(name):
         return {
             'error': "A group with name '{}' already exists.".format(name)
         }, 400
     group = GroupModel(**data)
     try:
         group.save_to_db()
     except:
         return {"error": "An error occurred creating the group."}, 500
     return group.json(), 201
Esempio n. 5
0
    def post(self):
        data = request.get_json()
        if self._is_added(data):
            """
            Add new group to DB
            """
            telegram_group = TelegramModel.find_by_group_chat_id(
                data[MESSAGE][CHAT][ID])
            group = GroupModel.find_by_id(telegram_group.group_fk)
            if group:
                logger.info('Group already exists in the database')
                return None
            group = GroupModel(data[MESSAGE][CHAT][TITLE],
                               data[MESSAGE][CHAT][ID])
            telegram = TelegramModel(data[MESSAGE][CHAT][ID],
                                     data[MESSAGE][CHAT][TITLE], group.id)
            try:
                group.save_to_db()
                telegram.save_to_db()
                logger.info('Group added to database')
            except:
                logger.error('unable to add group to database')

        if self._is_removed(data):
            # remove group from db
            telegram_group = TelegramModel.find_by_group_chat_id(
                data[MESSAGE][CHAT][ID])
            group = GroupModel.find_by_id(telegram_group.group_fk)
            if group:
                group.delete_from_db()
                telegram_group.delete_from_db()
                logger.info('Group successfully deleted from database')
            else:
                logger.warning(
                    'Delete executed on a group which does not exist in the database.'
                )

        if self._is_title_changed(data):
            telegram_group = TelegramModel.find_by_group_chat_id(
                data[MESSAGE][CHAT][ID])
            telegram_group.group_name = data[MESSAGE][CHAT][NEW_CHAT_TITLE]
            try:
                group.save_to_db()
                logger.info('Group title updated.')
            except:
                logger.error('Failed to update Group title to {}'.format(
                    data[MESSAGE][CHAT][NEW_CHAT_TITLE]))
Esempio n. 6
0
 def check_update(self):
     WebDriverWait(self.browser, 50).until(EC.presence_of_element_located(
         (By.XPATH, "//DIV[@id='pane-side']//DIV[@class='_3j7s9']")))
     chats = self.browser.find_elements_by_xpath(
         "//DIV[@id='pane-side']//DIV[@class='_3j7s9']")
     for chat in chats:
         try:
             chat.click()
         except WebDriverException as e:
             continue
         WebDriverWait(self.browser, 50).until(EC.presence_of_element_located(
             (By.CLASS_NAME, '_3AwwN')))
         # collect name of the group
         group_name = self.browser.find_element_by_xpath(
             "//HEADER[@class='_3AwwN']//SPAN[@dir='auto']").text
         whatsapp_group = WhatsAppModel.find_by_name(group_name)
         # check the membership 
         is_removed = None
         try:
             is_removed = self.browser.find_element_by_xpath(
                 "//DIV[@class='_2XiC1']")
         except NoSuchElementException:
             logger.info('')
         if is_removed:
             if whatsapp_group:
                 group = GroupModel.find_by_id(whatsapp_group.group_fk)
                 whatsapp_group.delete_from_db()
                 group.delete_from_db()
             try:
                 self.browser.find_element_by_xpath(
                     "(//SPAN[@data-icon='menu'])[2]"
                 ).click()
                 self.browser.find_element_by_xpath(
                     "//DIV[@class='_3lSL5 _2dGjP'][text()='Delete group']"
                 ).click()
                 self.browser.find_element_by_xpath(
                     "//DIV[@class='_1WZqU PNlAR'][text()='Delete']"
                 ).click()
             
                 self.browser.find_element_by_xpath(
                     "//DIV[@class='_3I_df']"
                 ).click()
                 self.browser.find_element_by_xpath(
                     "//DIV[@class='_1WZqU PNlAR'][text()='Clear']"
                 ).click()
             except:
                 logger.info('')
             continue
         if whatsapp_group:
             continue
         try:
             self.browser.find_element_by_class_name('_3AwwN').click()
         except:
             logger.info('')
         WebDriverWait(self.browser, 50).until(EC.presence_of_element_located(
             (By.CLASS_NAME, '_1xGbt')))
         chat_type = self.browser.find_element_by_class_name('_1xGbt').text
         if chat_type == 'Group info':
             # check if the bot is member of the group or not. if new membership
             # is found add the the membership to the database. if removal of 
             # membership is noticed then reflect the same on the database.
             logger.info('new group found')
             group_model = GroupModel('whatsapp',group_identifier=group_name)
             group_model.save_to_db()
             group_fk = group_model.id
             whatsapp_group = WhatsAppModel(group_name,group_fk)
             whatsapp_group.save_to_db()