def execute(self, dbSession, telegram, peer, arguments):
        if not len(arguments) == 2:
            telegram.send_msg(peer.id, 'Wrong number of arguments.')
            return

        if not self._isUserAuthorized(dbSession, peer):
            telegram.send_msg(peer.id, 'Not authorized.')
            return

        if self._subscriptionExists(dbSession, telegram, peer, arguments[0]):
            telegram.send_msg(peer.id, 'You are already subscribed to this URL.')
            return

        try:
            if not WHCrawlFactory.crawlerExists(arguments[0]):
                raise Exception("Could not find a suitable crawler for the site provided!")
            if not WHCrawlFactory.isCrawlingPeriodValid(arguments[0], int(arguments[1])):
                raise Exception("Given crawling period is not valid.")

            # add URL
            urlId = self._makeSureUrlExists(dbSession, arguments[0])

            # add Subscription of user to URL
            subscription = Subscription(user_id=self._queryUser(dbSession, peer).id, url_id=urlId, query_period=int(arguments[1]))
            dbSession.add(subscription)
            dbSession.commit()
            telegram.send_msg(peer.id, 'URL added successfully.')
        except Exception as exc:
            telegram.send_msg(peer.id, "Could not add URL: {0}".format(exc))
            dbSession.rollback()
Beispiel #2
0
 def processSubscriptions(self, dbSession):
     now = datetime.datetime.now()
     # iterate through subscriptions
     for user, subscription, subscription_url in dbSession.query(User, Subscription, Url).filter(User.id == Subscription.user_id).filter(Subscription.url_id == Url.id).all():
         # determine which subscriptions have to be queried again
         if self.__isSubscriptionDue(now, subscription):
             # query relevant subscription pages
             crawler = WHCrawlFactory.getCrawler(subscription_url.location)
             self.__processOffers(crawler.crawl(), dbSession, user, subscription)
             # update last_query to now
             subscription.last_query = now
             dbSession.commit()