Exemple #1
0
    def get_app_dictionary(self):
        if self.enabled:
            # we wan't to update every VALUES_UPDATE_INTERVAL minutes
            if self.status is None or timezone.now(
            ) >= self.last_activity + timedelta(
                    minutes=settings.VALUES_UPDATE_INTERVAL):
                # Weboob 1.2 example http://dev.weboob.org/
                weboob = Weboob()
                weboob.load_backends(CapParcel)
                for backend in list(weboob.iter_backends()):
                    if backend.name == self.parcel_carrier:
                        parcel = backend.get_parcel_tracking(self.parcel)
                        parcel_dict = parcel.to_dict()
                        self.status = parcel_dict['status']
                        if parcel_dict['arrival'] == 'Not loaded':
                            self.arrival = None
                        else:
                            pass
                            #parcel_dict['arrival']
                        self.url = parcel_dict['url']
                        self.info = parcel_dict['info']
                        self.save()

        return {
            'parcel': self.parcel,
            'parcel_carrier': self.parcel_carrier,
            'arrival': self.arrival,
            'status': self.status,
            'info': self.info
        }
Exemple #2
0
def retrieve_accounts(bank_name):
    w = Weboob()
    w.load_backends(caps=(CapBank, ))
    bank_backend = w.get_backend(bank_name)
    if bank_backend:
        bank = Bank.get_or_create(name=bank_name)
        accounts = bank_backend.iter_accounts()
        for account in accounts:
            logger.info(
                f'[Accounts] Retrieving account {account.label} - {account.balance} from {bank_name}'
            )

            db_account = Account.get_or_none(bank=bank[0],
                                             account_id=account.id)
            if db_account is None:
                db_account = Account.create(bank=bank[0],
                                            account_id=account.id,
                                            user=1,
                                            label=account.label,
                                            balance=account.balance)
            else:
                db_account.label = account.label
                db_account.balance = account.balance
                db_account.save()
            transactions = bank_backend.iter_history(account)
            for transaction in transactions:
                db_transanction = Transaction.get_or_create(
                    account=db_account.id,
                    label=transaction.label,
                    category=transaction.category,
                    amount=transaction.amount,
                    date=transaction.date)
Exemple #3
0
class MyThread(Thread):
    daemon = True

    def __init__(self, bot):
        Thread.__init__(self)
        self.weboob = Weboob(storage=StandardStorage(STORAGE_FILE))
        self.weboob.load_backends()
        self.bot = bot
        self.bot.weboob = self.weboob

    def run(self):
        for ev in self.bot.joined.itervalues():
            ev.wait()

        self.weboob.repeat(300, self.check_board)
        self.weboob.repeat(600, self.check_dlfp)

        self.weboob.loop()

    def find_keywords(self, text):
        for word in [
                'weboob', 'videoob', 'havesex', 'havedate', 'monboob',
                'boobmsg', 'flatboob', 'boobill', 'pastoob', 'radioob',
                'translaboob', 'traveloob', 'handjoob', 'boobathon', 'boobank',
                'boobtracker', 'comparoob', 'wetboobs', 'webcontentedit',
                'weboorrents', u'sàt', u'salut à toi', 'assnet',
                'budget insight', 'budget-insight', 'budgetinsight', 'budgea'
        ]:
            if word in text.lower():
                return word
        return None

    def check_dlfp(self):
        for backend, msg in self.weboob.do('iter_unread_messages',
                                           backends=['dlfp']):
            word = self.find_keywords(msg.content)
            if word is not None:
                url = msg.signature[msg.signature.find('https://linuxfr'):]
                self.bot.send_message('[DLFP] %s talks about %s: %s' %
                                      (msg.sender, word, url))
            backend.set_message_read(msg)

    def check_board(self):
        def iter_messages(backend):
            with backend.browser:
                return backend.browser.iter_new_board_messages()

        for backend, msg in self.weboob.do(iter_messages, backends=['dlfp']):
            word = self.find_keywords(msg.message)
            if word is not None and msg.login != 'moules':
                message = msg.message.replace(word, '\002%s\002' % word)
                self.bot.send_message('[DLFP] <%s> %s' % (msg.login, message))

    def stop(self):
        self.weboob.want_stop()
        self.weboob.deinit()
Exemple #4
0
class MyThread(Thread):
    daemon = True

    def __init__(self, bot):
        Thread.__init__(self)
        self.weboob = Weboob(storage=StandardStorage(STORAGE_FILE))
        self.weboob.load_backends()
        self.bot = bot
        self.bot.set_weboob(self.weboob)

    def run(self):
        for ev in self.bot.joined.itervalues():
            ev.wait()

        self.weboob.repeat(300, self.check_board)
        self.weboob.repeat(600, self.check_dlfp)

        self.weboob.loop()

    def find_keywords(self, text):
        for word in [
            'weboob', 'videoob', 'havesex', 'havedate', 'monboob', 'boobmsg',
            'flatboob', 'boobill', 'pastoob', 'radioob', 'translaboob', 'traveloob', 'handjoob',
            'boobathon', 'boobank', 'boobtracker', 'comparoob', 'wetboobs',
            'webcontentedit', 'weboorrents', u'sàt', u'salut à toi', 'assnet',
                'budget insight', 'budget-insight', 'budgetinsight', 'budgea']:
            if word in text.lower():
                return word
        return None

    def check_dlfp(self):
        for backend, msg in self.weboob.do('iter_unread_messages', backends=['dlfp']):
            word = self.find_keywords(msg.content)
            if word is not None:
                url = msg.signature[msg.signature.find('https://linuxfr'):]
                self.bot.send_message('[DLFP] %s talks about %s: %s' % (
                    msg.sender, word, url))
            backend.set_message_read(msg)

    def check_board(self):
        def iter_messages(backend):
            with backend.browser:
                return backend.browser.iter_new_board_messages()

        for backend, msg in self.weboob.do(iter_messages, backends=['dlfp']):
            word = self.find_keywords(msg.message)
            if word is not None and msg.login != 'moules':
                message = msg.message.replace(word, '\002%s\002' % word)
                self.bot.send_message('[DLFP] <%s> %s' % (msg.login, message))

    def stop(self):
        self.weboob.want_stop()
        self.weboob.deinit()
Exemple #5
0
def get_accounts(bname):
    w = Weboob()

    w.load_backends(names=[bname])
    backend = w.get_backend(bname)

    results = {}
    for account in backend.iter_accounts():
        # a unicode bigger than 8 characters used as key of the table make some bugs in the C++ code
        # convert to string before to use it
        results[str(account.id)] = {'name':    account.label,
                               'balance': int(account.balance * 100),
                               'type':    int(account.type),
                              }
    return results
Exemple #6
0
def get_accounts(bname):
    w = Weboob()

    w.load_backends(names=[bname])
    backend = w.get_backend(bname)

    results = {}
    for account in backend.iter_accounts():
        # a unicode bigger than 8 characters used as key of the table make some bugs in the C++ code
        # convert to string before to use it
        results[str(account.id)] = {
            'name': account.label,
            'balance': int(account.balance * 100),
            'type': int(account.type),
        }
    return results
Exemple #7
0
def get_transactions(bname, accid, maximum):
    w = Weboob()

    w.load_backends(names=[bname])
    backend = w.get_backend(bname)

    acc = backend.get_account(accid)
    results = {}
    results['id'] = acc.id
    results['name'] = acc.label
    results['balance'] = int(acc.balance * 100)
    results['type'] = int(acc.type)
    results['transactions'] = []

    try:
        count = int(maximum)
        if count < 1:
            count = 0
    except:
        count = 0
    i = 0
    first = True
    rewriteid = False
    seen = set()
    for tr in backend.iter_history(acc):
        if first:
            if tr.id == u'0' or tr.id == u'':
                rewriteid = True
            first = False
        if rewriteid:
            tr.id = tr.unique_id(seen)
        t = {
            'id': tr.id,
            'date': tr.date.strftime('%Y-%m-%d'),
            'rdate': tr.rdate.strftime('%Y-%m-%d'),
            'type': int(tr.type),
            'raw': tr.raw,
            'category': tr.category,
            'label': tr.label,
            'amount': int(tr.amount * 100),
        }
        results['transactions'].append(t)
        i += 1
        if count != 0 and i >= count:
            break

    return results
Exemple #8
0
def get_transactions(bname, accid, maximum):
    w = Weboob()

    w.load_backends(names=[bname])
    backend = w.get_backend(bname)

    acc = backend.get_account(accid)
    results = {}
    results['id'] = acc.id
    results['name'] = acc.label
    results['balance'] = int(acc.balance * 100)
    results['type'] = int(acc.type)
    results['transactions'] = []

    try:
        count = int(maximum)
        if count < 1:
            count = 0
    except:
        count = 0
    i = 0
    first = True
    rewriteid = False
    seen = set()
    for tr in backend.iter_history(acc):
        if first:
            if tr.id == u'0' or tr.id == u'':
                rewriteid = True
            first = False
        if rewriteid:
            tr.id = tr.unique_id(seen)
        t = {'id':          tr.id,
             'date':        tr.date.strftime('%Y-%m-%d'),
             'rdate':       tr.rdate.strftime('%Y-%m-%d'),
             'type':        int(tr.type),
             'raw':         tr.raw,
             'category':    tr.category,
             'label':       tr.label,
             'amount':      int(tr.amount * 100),
        }
        results['transactions'].append(t)
        i += 1
        if count != 0 and i >= count:
            break

    return results
Exemple #9
0
def new_jobs():
    w = Weboob()
    w.load_backends(CapJob)
    #w['popolemploi']
    words = u'python'
    jobs = w.search_job(words)
    jobs = list(jobs)
    Poleemploi.query.delete()
    for job in jobs:
        new = Poleemploi(society_name=job.society_name,
                         place=job.place,
                         title=job.title,
                         contract_type=job.contract_type,
                         publication_date=job.publication_date)
        db.session.add(new)
        db.session.commit()
    return redirect(url_for('jobs_index'))
Exemple #10
0
class BackendTest(TestCase):
    MODULE = None

    def __init__(self, *args, **kwargs):
        super(BackendTest, self).__init__(*args, **kwargs)

        self.backends = {}
        self.backend_instance = None
        self.backend = None
        self.weboob = Weboob()

        # Skip tests when passwords are missing
        self.weboob.requests.register('login', self.login_cb)

        if self.weboob.load_backends(modules=[self.MODULE]):
            # provide the tests with all available backends
            self.backends = self.weboob.backend_instances

    def login_cb(self, backend_name, value):
        raise SkipTest('missing config \'%s\' is required for this test' %
                       value.label)

    def run(self, result):
        """
        Call the parent run() for each backend instance.
        Skip the test if we have no backends.
        """
        # This is a hack to fix an issue with nosetests running
        # with many tests. The default is 1000.
        sys.setrecursionlimit(10000)
        try:
            if not len(self.backends):
                self.backend = self.weboob.build_backend(self.MODULE,
                                                         nofail=True)
                TestCase.run(self, result)
            else:
                # Run for all backend
                for backend_instance in self.backends.keys():
                    print(backend_instance)
                    self.backend = self.backends[backend_instance]
                    TestCase.run(self, result)
        finally:
            self.weboob.deinit()

    def shortDescription(self):
        """
        Generate a description with the backend instance name.
        """
        # do not use TestCase.shortDescription as it returns None
        return '%s [%s]' % (str(self), self.backend_instance)

    def is_backend_configured(self):
        """
        Check if the backend is in the user configuration file
        """
        return self.weboob.backends_config.backend_exists(
            self.backend.config.instname)
Exemple #11
0
class BackendTest(TestCase):
    MODULE = None

    def __init__(self, *args, **kwargs):
        super(BackendTest, self).__init__(*args, **kwargs)

        self.backends = {}
        self.backend_instance = None
        self.backend = None
        self.weboob = Weboob()

        # Skip tests when passwords are missing
        self.weboob.requests.register('login', self.login_cb)

        if self.weboob.load_backends(modules=[self.MODULE]):
            # provide the tests with all available backends
            self.backends = self.weboob.backend_instances

    def login_cb(self, backend_name, value):
        raise SkipTest('missing config \'%s\' is required for this test' % value.label)

    def run(self, result):
        """
        Call the parent run() for each backend instance.
        Skip the test if we have no backends.
        """
        # This is a hack to fix an issue with nosetests running
        # with many tests. The default is 1000.
        sys.setrecursionlimit(10000)
        try:
            if not len(self.backends):
                self.backend = self.weboob.build_backend(self.MODULE, nofail=True)
                TestCase.run(self, result)
            else:
                # Run for all backend
                for backend_instance in self.backends.keys():
                    print(backend_instance)
                    self.backend = self.backends[backend_instance]
                    TestCase.run(self, result)
        finally:
            self.weboob.deinit()

    def shortDescription(self):
        """
        Generate a description with the backend instance name.
        """
        # do not use TestCase.shortDescription as it returns None
        return '%s [%s]' % (str(self), self.backend_instance)

    def is_backend_configured(self):
        """
        Check if the backend is in the user configuration file
        """
        return self.weboob.backends_config.backend_exists(self.backend.config.instname)
Exemple #12
0
class Boobpeg:
    """ Class that performs requests using weboob capabitilities """
    def __init__(self):
        logger.debug("class init")
        self.peg_perco_names = []
        self.weboob = Weboob()
        self.weboob.load_backends(CapBank)
        logger.debug('backends loaded: %s' % list(self.weboob.iter_backends()))
        self.investments = {}

    def set_peg_perco_names(self, peg_perco_names):
        logger.debug("set_peg_perco_names(): peg_perco_names: %s" %
                     peg_perco_names)
        self.peg_perco_names = peg_perco_names

    def retrieve_investments(self, account):
        logger.debug("retrieve_investments(): account: %s" % account)
        account_investments = {}
        for investment in list(self.weboob.iter_investment(account)):
            account_investments[investment.code] = str(investment.unitvalue)
        logger.debug("retrieve_investments(): return: %s" %
                     account_investments)
        return account_investments

    def update_investments(self):
        logger.debug("update_investments()")
        accounts = list(self.weboob.iter_accounts())
        logger.debug("update_investments(): accounts: %s" % accounts)
        for account in accounts:
            if account.label in self.peg_perco_names:
                logger.debug('update_investments(): account "%s" is in the '
                             'account list to check' % account.label)
                self.investments.update(self.retrieve_investments(account))
            else:
                logger.debug('update_investments(): account "%s" is NOT in the'
                             ' account list to check' % account.label)

        logger.debug("update_investments(): return: %s" % self.investments)
        return self.investments
class RboorrentDownload(object):
    def __init__(self, _id, no_tracker):
        self.id, self.backend_name = _id.split("@")
        self.no_tracker = no_tracker
        self.weboob = Weboob()
        self.backend = self.weboob.load_backends(
            modules=[self.backend_name])[self.backend_name]

    def get_magnet(self, torrent):
        if self.no_tracker:
            return "&".join([
                _ for _ in torrent.magnet.split("&") if not _.startswith("tr=")
            ])
        else:
            return torrent.magnet

    def write_meta(self, torrent):
        dest = "meta-%s-%s.torrent" % (torrent.id, torrent.name)
        magnet = self.get_magnet(torrent)
        buf = "d10:magnet-uri%d:%se" % (len(magnet), magnet)
        try:
            with open(dest, 'w') as f:
                f.write(buf)
        except IOError as e:
            print('Unable to write "%s": %s' % (dest, e.message))

    def write_torrent(self, torrent):
        dest = "%s-%s.torrent" % (torrent.id, torrent.name)
        try:
            buf = self.backend.get_torrent_file(torrent.id)
            if buf:
                try:
                    with open(dest, 'w') as f:
                        f.write(buf)
                except IOError as e:
                    print('Unable to write "%s": %s' % (dest, e))
        except Exception as e:
            print("Could not get torrent file for %s@%s" %
                  (self.id, self.backend_name))

    def run(self):
        try:
            torrent = self.backend.get_torrent(self.id)
            if torrent.magnet:
                self.write_meta(torrent)
            else:
                self.write_torrent(torrent)
        except HTTPNotFound:
            print("Could not find %s@%s" % (self.id, self.backend_name))
Exemple #14
0
    def _get_all_transactions(self):

        weboob = Weboob()
        backend = weboob.load_backends(
            names=['bp'])['bp']  # TODO Make it generic

        account_transactions = []
        for account in backend.iter_accounts():
            if account.label != 'COMPTE BANCAIRE':  # TODO Make it generic
                continue
            for weboob_transaction in backend.iter_history(account):
                transaction = Transaction(date=weboob_transaction.date,
                                          amount=float(
                                              weboob_transaction.amount),
                                          label=weboob_transaction.label)
                account_transactions.append(transaction)
        return account_transactions
Exemple #15
0
class RboorrentDownload(object):
    def __init__(self, _id, no_tracker):
        self.id, self.backend_name = _id.split("@")
        self.no_tracker = no_tracker
        self.weboob = Weboob()
        self.backend = self.weboob.load_backends(modules=[self.backend_name])[self.backend_name]

    def get_magnet(self, torrent):
        if self.no_tracker:
            return "&".join([_ for _ in torrent.magnet.split("&") if not _.startswith("tr=")])
        else:
            return torrent.magnet

    def write_meta(self, torrent):
        dest = "meta-%s-%s.torrent" % (torrent.id, torrent.name)
        magnet = self.get_magnet(torrent)
        buf = "d10:magnet-uri%d:%se" % (len(magnet), magnet)
        try:
            with open(dest, 'w') as f:
                f.write(buf)
        except IOError as e:
            print('Unable to write "%s": %s' % (dest, e.message))

    def write_torrent(self, torrent):
        dest = "%s-%s.torrent" % (torrent.id, torrent.name)
        try:
            buf = self.backend.get_torrent_file(torrent.id)
            if buf:
                try:
                    with open(dest, 'w') as f:
                        f.write(buf)
                except IOError as e:
                    print('Unable to write "%s": %s' % (dest, e))
        except Exception as e:
            print("Could not get torrent file for %s@%s" % (self.id, self.backend_name))

    def run(self):
        try:
            torrent = self.backend.get_torrent(self.id)
            if torrent.magnet:
                self.write_meta(torrent)
            else:
                self.write_torrent(torrent)
        except HTTPNotFound:
            print("Could not find %s@%s" % (self.id, self.backend_name))
Exemple #16
0
class BackendTest(TestCase):
    BACKEND = None

    def __init__(self, *args, **kwargs):
        TestCase.__init__(self, *args, **kwargs)

        self.backend = None
        self.weboob = Weboob()

        if self.weboob.load_backends(modules=[self.BACKEND]):
            self.backend = choice(self.weboob.backend_instances.values())

    def run(self, result):
        if not self.backend:
            result.startTest(self)
            result.stopTest(self)
            raise SkipTest()

        return TestCase.run(self, result)
Exemple #17
0
class BackendTest(TestCase):
    MODULE = None

    def __init__(self, *args, **kwargs):
        TestCase.__init__(self, *args, **kwargs)

        self.backends = {}
        self.backend_instance = None
        self.backend = None
        self.weboob = Weboob()

        if self.weboob.load_backends(modules=[self.MODULE]):
            # provide the tests with all available backends
            self.backends = self.weboob.backend_instances
            # chose one backend (enough for most tests)
            self.backend_instance = choice(self.backends.keys())
            self.backend = self.backends[self.backend_instance]

    def run(self, result):
        """
        Call the parent run() for each backend instance.
        Skip the test if we have no backends.
        """
        # This is a hack to fix an issue with nosetests running
        # with many tests. The default is 1000.
        sys.setrecursionlimit(10000)
        try:
            if not len(self.backends):
                result.startTest(self)
                result.stopTest(self)
                raise SkipTest('No backends configured for this module.')
            TestCase.run(self, result)
        finally:
            self.weboob.deinit()

    def shortDescription(self):
        """
        Generate a description with the backend instance name.
        """
        # do not use TestCase.shortDescription as it returns None
        return '%s [%s]' % (str(self), self.backend_instance)
Exemple #18
0
class BoobankChecker():
    def __init__(self):
        self.ind = appindicator.Indicator.new(APPINDICATOR_ID,
                                              os.path.abspath(resource_filename('boobank_indicator.data',
                                                                                'indicator-boobank.png')),
                                              appindicator.IndicatorCategory.APPLICATION_STATUS)

        self.menu = Gtk.Menu()
        self.ind.set_menu(self.menu)

        logging.basicConfig()
        if 'weboob_path' in os.environ:
            self.weboob = Weboob(os.environ['weboob_path'])
        else:
            self.weboob = Weboob()

        self.weboob.load_backends(CapBank)

    def clean_menu(self, menu):
        for i in menu.get_children():
            submenu = i.get_submenu()
            if submenu:
                self.clean_menu(i)
            menu.remove(i)

    def check_boobank(self):
        self.ind.set_status(appindicator.IndicatorStatus.ACTIVE)
        self.clean_menu(self.menu)

        total = 0
        currency = ''
        threads = []

        try:
            for account in self.weboob.do('iter_accounts'):

                balance = account.balance
                if account.coming:
                    balance += account.coming

                if account.type != Account.TYPE_LOAN:
                    total += balance
                    image = "green_light.png" if balance > 0 else "red_light.png"
                else:
                    image = "personal-loan.png"

                currency = account.currency_text
                label = "%s: %s%s" % (account.label, balance, account.currency_text)
                account_item = create_image_menu_item(label, image)
                thread = BoobankTransactionsChecker(self.weboob, account_item, account)
                thread.start()
                threads.append(thread)

        except CallErrors as errors:
            self.bcall_errors_handler(errors)

        for thread in threads:
            thread.join()

        for thread in threads:
            self.menu.append(thread.menu)
            thread.menu.show()

        if len(self.menu.get_children()) == 0:
            Notify.Notification.new('<b>Boobank</b>',
                                    'No Bank account found\n Please configure one by running boobank',
                                    'notification-message-im').show()

        sep = Gtk.SeparatorMenuItem()
        self.menu.append(sep)
        sep.show()

        total_item = Gtk.MenuItem("%s: %s%s" % ("Total", total, currency))
        self.menu.append(total_item)
        total_item.show()

        sep = Gtk.SeparatorMenuItem()
        self.menu.append(sep)
        sep.show()

        btnQuit = Gtk.ImageMenuItem()
        image = Gtk.Image()
        image.set_from_stock(Gtk.STOCK_QUIT, Gtk.IconSize.BUTTON)
        btnQuit.set_image(image)
        btnQuit.set_label('Quit')
        btnQuit.set_always_show_image(True)
        btnQuit.connect("activate", self.quit)
        self.menu.append(btnQuit)
        btnQuit.show()

    def quit(self, widget):
        Gtk.main_quit()

    def bcall_errors_handler(self, errors):
        """
        Handler for the CallErrors exception.
        """
        self.ind.set_status(appindicator.IndicatorStatus.ATTENTION)
        for backend, error, backtrace in errors.errors:
            notify = True
            if isinstance(error, BrowserIncorrectPassword):
                msg = 'invalid login/password.'
            elif isinstance(error, BrowserSSLError):
                msg = '/!\ SERVER CERTIFICATE IS INVALID /!\\'
            elif isinstance(error, BrowserForbidden):
                msg = unicode(error) or 'Forbidden'
            elif isinstance(error, BrowserUnavailable):
                msg = unicode(error)
                if not msg:
                    msg = 'website is unavailable.'
            elif isinstance(error, NotImplementedError):
                notify = False
            elif isinstance(error, UserError):
                msg = unicode(error)
            elif isinstance(error, MoreResultsAvailable):
                notify = False
            else:
                msg = unicode(error)

            if notify:
                Notify.Notification.new('<b>Error Boobank: %s</b>' % backend.name,
                                        msg,
                                        'notification-message-im').show()

    def main(self):
        self.check_boobank()
        GObject.timeout_add(PING_FREQUENCY * 1000, self.check_boobank)
        Gtk.main()
Exemple #19
0
class MyThread(Thread):
    daemon = True

    def __init__(self, bot):
        Thread.__init__(self)
        self.weboob = Weboob(storage=StandardStorage(STORAGE_FILE))
        self.weboob.load_backends()
        self.bot = bot
        self.bot.set_weboob(self.weboob)

    def run(self):
        for ev in self.bot.joined.itervalues():
            ev.wait()

        self.weboob.repeat(300, self.check_board)
        self.weboob.repeat(600, self.check_dlfp)
        self.weboob.repeat(600, self.check_twitter)

        self.weboob.loop()

    def find_keywords(self, text):
        for word in [
            'weboob', 'videoob', 'havesex', 'havedate', 'monboob', 'boobmsg',
            'flatboob', 'boobill', 'pastoob', 'radioob', 'translaboob', 'traveloob', 'handjoob',
            'boobathon', 'boobank', 'boobtracker', 'comparoob', 'wetboobs',
            'webcontentedit', 'weboorrents', 'assnet', 'budget insight', 'budget-insight', 'budgetinsight', 'budgea']:
            if word in text.lower():
                return word
        return None

    def check_twitter(self):
        nb_tweets = 10

        for backend in self.weboob.iter_backends(module='twitter'):
            for thread in list(itertools.islice(backend.iter_resources(None, ['search', 'weboob']),
                                                0,
                                                nb_tweets)):

                if not backend.storage.get('lastpurge'):
                    backend.storage.set('lastpurge', datetime.now() - timedelta(days=60))
                    backend.storage.save()

                if thread.id not in backend.storage.get('seen', default={}) and\
                   thread.date > backend.storage.get('lastpurge'):
                    _item = thread.id.split('#')
                    url = 'https://twitter.com/%s/status/%s' % (_item[0], _item[1])
                    for msg in self.bot.on_url(url):
                        self.bot.send_message('%s: %s' % (_item[0], url))
                        self.bot.send_message(msg)

                    backend.set_message_read(backend.fill_thread(thread, ['root']).root)

    def check_dlfp(self):
        for msg in self.weboob.do('iter_unread_messages', backends=['dlfp']):
            word = self.find_keywords(msg.content)
            if word is not None:
                url = msg.signature[msg.signature.find('https://linuxfr'):]
                self.bot.send_message('[DLFP] %s talks about %s: %s' % (
                    msg.sender, word, url))
            self.weboob[msg.backend].set_message_read(msg)

    def check_board(self):
        def iter_messages(backend):
            with backend.browser:
                return backend.browser.iter_new_board_messages()

        for msg in self.weboob.do(iter_messages, backends=['dlfp']):
            word = self.find_keywords(msg.message)
            if word is not None and msg.login != 'moules':
                message = msg.message.replace(word, '\002%s\002' % word)
                self.bot.send_message('[DLFP] <%s> %s' % (msg.login, message))

    def stop(self):
        self.weboob.want_stop()
        self.weboob.deinit()
Exemple #20
0
class Downloadboob(object):

    def __init__(self, backend_name, download_directory, links_directory):
        self.download_directory = download_directory
        self.links_directory = links_directory
        self.backend_name = backend_name
        self.backend = None
        self.weboob = Weboob()
        self.weboob.load_backends(modules=[self.backend_name])
        self.backend=self.weboob.get_backend(self.backend_name)

    def purge(self):
        if not os.path.isdir(self.links_directory):
            return
        dirList=os.listdir(self.links_directory)
        for local_link_name in dirList:
            link_name = self.links_directory + "/" + local_link_name
            if not self.check_link(link_name):
                print u"Remove %s" % link_name
                os.remove(link_name)
            else:
                print u"Keep %s" % link_name

    def check_link(self, link_name):
        if os.path.islink(link_name):
            file_name = os.readlink(link_name)
            absolute_file_name = os.path.join(self.links_directory, file_name)
            if os.path.isfile(absolute_file_name):
                return True
            return False
        else:
            return True

    def download(self, pattern=None, sortby=CapVideo.SEARCH_RELEVANCE, nsfw=False, max_results=None, title_exclude=[], id_regexp=None):
        print "For backend %s, search for '%s'" % (backend_name, pattern)

        # create directory for links
        print "  create link to %s" % self.links_directory
        if not os.path.isdir(self.links_directory):
            os.makedirs(self.links_directory)

        # search for videos
        videos = []
        for i, video in enumerate(self.backend.search_videos(pattern, sortby, nsfw)):
            if i == max_results:
                break

            if not self.is_downloaded(video):
                self.backend.fill_video(video, ('url','title', 'url', 'duration'))
                if not(self.is_excluded(video.title, title_exclude)) and self.id_regexp_matched(video.id, id_regexp):
                    print "  %s\n    Id:%s\n    Duration:%s" % (video.title, video.id, video.duration)
                    videos.append(video)
            else:
                print "Already downloaded, check %s" % video.id
                self.backend.fill_video(video, ('url','title', 'url', 'duration'))
                linkname = self.get_linkname(video)
                if not os.path.exists(linkname):
                    self.remove_download(video)

        # download videos
        print "Downloading..."
        for video in videos:
            self.do_download(video)

    def is_excluded(self, title, title_exclude):
        for exclude in title_exclude:
            if title.find(exclude) > -1:
                return True
        return False

    def id_regexp_matched(self, video_id, id_regexp):
        if id_regexp:
            return re.search(id_regexp, video_id) is not None
        return True

    def get_filename(self, video, relative=False):
        if relative:
            directory = os.path.join("..", DOWNLOAD_DIRECTORY, self.backend_name)
        else:
            directory = os.path.join(self.download_directory, self.backend_name)
            if not os.path.exists(directory):
                os.makedirs(directory)

        ext = video.ext
        if not ext:
            ext = 'avi'

        return u"%s/%s.%s" % (directory, removeNonAscii(video.id), ext)

    def get_linkname(self, video):
        if not os.path.exists(self.links_directory):
            os.makedirs(self.links_directory)

        ext = video.ext
        if not ext:
            ext = 'avi'

        misc = video.date
        if not misc:
            misc = video.id

        return u"%s/%s (%s).%s" % (self.links_directory, removeSpecial(video.title), removeSpecial(misc), ext)

    def is_downloaded(self, video):
        # check if the file is 0 byte
        return os.path.isfile(self.get_filename(video))

    def remove_download(self, video):
        path = self.get_filename(video)
        if os.stat(path).st_size == 0:
            # already empty
            return

        print 'Remove video %s' % video.title

        # Empty it to keep information we have already downloaded it.
        with open(path, 'w'):
            pass

    def set_linkname(self, video):
        linkname = self.get_linkname(video)
        idname = self.get_filename(video, relative=True)
        absolute_idname = self.get_filename(video, relative=False)
        if not os.path.islink(linkname) and os.path.isfile(absolute_idname):
            print "%s -> %s" % (linkname, idname)
            os.symlink(idname, linkname)

    def do_download(self, video):
        if not video:
            print >>sys.stderr, 'Video not found: %s' %  video
            return 3

        if not video.url:
            print >>sys.stderr, 'Error: the direct URL is not available.'
            return 4

        def check_exec(executable):
            with open('/dev/null', 'w') as devnull:
                process = subprocess.Popen(['which', executable], stdout=devnull)
                if process.wait() != 0:
                    print >>sys.stderr, 'Please install "%s"' % executable
                    return False
            return True

        dest = self.get_filename(video)

        if video.url.startswith('rtmp'):
            if not check_exec('rtmpdump'):
                return 1
            args = ('rtmpdump', '-e', '-r', video.url, '-o', dest)
        elif video.url.startswith('mms'):
            if not check_exec('mimms'):
                return 1
            args = ('mimms', video.url, dest)
        else:
            if not check_exec('wget'):
                return 1
            args = ('wget', video.url, '-O', dest)

        os.spawnlp(os.P_WAIT, args[0], *args)
        self.set_linkname(video)
Exemple #21
0
class MyThread(Thread):
    daemon = True

    def __init__(self, bot):
        Thread.__init__(self)
        self.weboob = Weboob(storage=StandardStorage(STORAGE_FILE))
        self.weboob.load_backends()
        self.bot = bot
        self.bot.set_weboob(self.weboob)

    def run(self):
        for ev in self.bot.joined.values():
            ev.wait()

        self.weboob.repeat(5, self.check_tasks)
        self.weboob.repeat(300, self.check_board)
        self.weboob.repeat(600, self.check_dlfp)
        self.weboob.repeat(600, self.check_twitter)

        self.weboob.loop()

    def find_keywords(self, text):
        for word in [
            'weboob', 'videoob', 'havesex', 'havedate', 'monboob', 'boobmsg',
            'flatboob', 'boobill', 'pastoob', 'radioob', 'translaboob', 'traveloob', 'handjoob',
            'boobathon', 'boobank', 'boobtracker', 'comparoob', 'wetboobs',
            'webcontentedit', 'weboorrents', 'assnet', 'budget insight', 'budget-insight', 'budgetinsight', 'budgea']:
            if word in text.lower():
                return word
        return None

    def check_twitter(self):
        nb_tweets = 10

        for backend in self.weboob.iter_backends(module='twitter'):
            for thread in list(itertools.islice(backend.iter_resources(None, ['search', 'weboob']),
                                                0,
                                                nb_tweets)):

                if not backend.storage.get('lastpurge'):
                    backend.storage.set('lastpurge', datetime.now() - timedelta(days=60))
                    backend.storage.save()

                if thread.id not in backend.storage.get('seen', default={}) and\
                   thread.date > backend.storage.get('lastpurge'):
                    _item = thread.id.split('#')
                    url = 'https://twitter.com/%s/status/%s' % (_item[0], _item[1])
                    for msg in self.bot.on_url(url):
                        self.bot.send_message('%s: %s' % (_item[0], url))
                        self.bot.send_message(msg)

                    backend.set_message_read(backend.fill_thread(thread, ['root']).root)

    def check_dlfp(self):
        for msg in self.weboob.do('iter_unread_messages', backends=['dlfp']):
            word = self.find_keywords(msg.content)
            if word is not None:
                url = msg.signature[msg.signature.find('https://linuxfr'):]
                self.bot.send_message('[DLFP] %s talks about %s: %s' % (
                    msg.sender, word, url))
            self.weboob[msg.backend].set_message_read(msg)

    def check_board(self):
        def iter_messages(backend):
            return backend.browser.iter_new_board_messages()

        for msg in self.weboob.do(iter_messages, backends=['dlfp']):
            word = self.find_keywords(msg.message)
            if word is not None and msg.login != 'moules':
                message = msg.message.replace(word, '\002%s\002' % word)
                self.bot.send_message('[DLFP] <%s> %s' % (msg.login, message))

    def check_tasks(self):
        for task in list(self.bot.tasks_queue):
            if task.datetime < datetime.now():
                self.bot.send_message(task.message, task.channel)
                self.bot.tasks_queue.remove(task)

    def stop(self):
        self.weboob.want_stop()
        self.weboob.deinit()
Exemple #22
0
# =============== It doesn't work ============ enter the good path !
# capApplicationDict = init_CapApplicationDict(w)
# print(list(capApplicationDict.keys()))

# for cle in capApplicationDict:
#     print('\n ==========> \n {}\n <==========\n'.format(cle))
# ==================================================================

if not w.repositories.check_repositories():
    w.repositories.update()
    print("WeBoob a été mis à jour (répertoires de config) \n")
else:
    print("Repertoires à jour \n\n")
    # w.repositories.update()

w.load_backends(CapBank)

# capApplicationDict = w.init_CapApplicationDict()
# for cle in capApplicationDict:
#    print(cle)

# applications = capApplicationDict[cap]
# applications = capApplicationDict['CapBank']

# print('\n ==========> \n {}\n <==========\n'.format(application))

print('\n *************************\n {} \n *************************** \n'.
      format(list(w.iter_accounts())))

acc = next(iter(w.iter_accounts()))
bal = acc.balance
Exemple #23
0
class Downloadboob(object):

    def __init__(self, backend_name, download_directory, links_directory):
        self.download_directory = download_directory
        self.links_directory = links_directory
        self.backend_name = backend_name
        self.backend = None
        self.weboob = Weboob()
        self.weboob.load_backends(modules=[self.backend_name])
        self.backend=self.weboob.get_backend(self.backend_name)

    def purge(self):
        if not os.path.isdir(self.links_directory):
            return
        dirList=os.listdir(self.links_directory)
        for local_link_name in dirList:
            link_name = self.links_directory + "/" + local_link_name
            if not self.check_link(link_name):
                print u"Remove %s" % link_name
                os.remove(link_name)
            else:
                print u"Keep %s" % link_name

    def check_link(self, link_name):
        if os.path.islink(link_name):
            file_name = os.readlink(link_name)
            absolute_file_name = os.path.join(self.links_directory, file_name)
            if os.path.isfile(absolute_file_name):
                return True
            return False
        else:
            return True

    def download(self, pattern=None, sortby=ICapVideo.SEARCH_RELEVANCE, nsfw=False, max_results=None, title_exclude=[], id_regexp=None):
        print "For backend %s, search for '%s'" % (backend_name, pattern)

        # create directory for links
        print "  create link to %s" % self.links_directory
        if not os.path.isdir(self.links_directory):
            os.makedirs(self.links_directory)

        # search for videos
        videos = []
        for i, video in enumerate(self.backend.search_videos(pattern, sortby, nsfw)):
            if i == max_results:
                break

            if not self.is_downloaded(video):
                self.backend.fill_video(video, ('url','title', 'url', 'duration'))
                if not(self.is_excluded(video.title, title_exclude)) and self.id_regexp_matched(video.id, id_regexp):
                    print "  %s\n    Id:%s\n    Duration:%s" % (video.title, video.id, video.duration)
                    videos.append(video)
            else:
                print "Already downloaded, check %s" % video.id
                self.backend.fill_video(video, ('url','title', 'url', 'duration'))
                linkname = self.get_linkname(video)
                if not os.path.exists(linkname):
                    self.remove_download(video)

        # download videos
        print "Downloading..."
        for video in videos:
            self.do_download(video)

    def is_excluded(self, title, title_exclude):
        for exclude in title_exclude:
            if title.find(exclude) > -1:
                return True
        return False

    def id_regexp_matched(self, video_id, id_regexp):
        if id_regexp:
            return re.search(id_regexp, video_id) is not None
        return True

    def get_filename(self, video, relative=False):
        if relative:
            directory = os.path.join("..", DOWNLOAD_DIRECTORY, self.backend_name)
        else:
            directory = os.path.join(self.download_directory, self.backend_name)
            if not os.path.exists(directory):
                os.makedirs(directory)

        ext = video.ext
        if not ext:
            ext = 'avi'

        return u"%s/%s.%s" % (directory, removeNonAscii(video.id), ext)

    def get_linkname(self, video):
        if not os.path.exists(self.links_directory):
            os.makedirs(self.links_directory)

        ext = video.ext
        if not ext:
            ext = 'avi'

        misc = video.date
        if not misc:
            misc = video.id

        return u"%s/%s (%s).%s" % (self.links_directory, removeSpecial(video.title), removeSpecial(misc), ext)

    def is_downloaded(self, video):
        # check if the file is 0 byte
        return os.path.isfile(self.get_filename(video))

    def remove_download(self, video):
        path = self.get_filename(video)
        if os.stat(path).st_size == 0:
            # already empty
            return

        print 'Remove video %s' % video.title

        # Empty it to keep information we have already downloaded it.
        with open(path, 'w'):
            pass

    def set_linkname(self, video):
        linkname = self.get_linkname(video)
        idname = self.get_filename(video, relative=True)
        absolute_idname = self.get_filename(video, relative=False)
        if not os.path.islink(linkname) and os.path.isfile(absolute_idname):
            print "%s -> %s" % (linkname, idname)
            os.symlink(idname, linkname)

    def do_download(self, video):
        if not video:
            print >>sys.stderr, 'Video not found: %s' %  video
            return 3

        if not video.url:
            print >>sys.stderr, 'Error: the direct URL is not available.'
            return 4

        def check_exec(executable):
            with open('/dev/null', 'w') as devnull:
                process = subprocess.Popen(['which', executable], stdout=devnull)
                if process.wait() != 0:
                    print >>sys.stderr, 'Please install "%s"' % executable
                    return False
            return True

        dest = self.get_filename(video)

        if video.url.startswith('rtmp'):
            if not check_exec('rtmpdump'):
                return 1
            args = ('rtmpdump', '-e', '-r', video.url, '-o', dest)
        elif video.url.startswith('mms'):
            if not check_exec('mimms'):
                return 1
            args = ('mimms', video.url, dest)
        else:
            if not check_exec('wget'):
                return 1
            args = ('wget', video.url, '-O', dest)

        os.spawnlp(os.P_WAIT, args[0], *args)
        self.set_linkname(video)
Exemple #24
0
def retrieve_accounts():
    w = Weboob()
    w.load_backends(caps=(CapBank, ))

    for i in w.load_backends(CapBank).keys():
        Bank.get_or_create(name=i)

    user1 = User.get_or_none(username='******', password='******')
    user2 = User.get_or_none(username='******', password='******')
    if user1 is None:
        user1 = User.create(username='******', password='******')
    if user2 is None:
        user2 = User.create(username='******', password='******')

    for bank in Bank.select():
        bank_backend = w.get_backend(bank.name)

        accounts = bank_backend.iter_accounts()

        for account in accounts:
            logger.info(
                f'[Accounts] Retrieving account {account.id} - {account.label} - {account.balance} from {bank.name}'
            )

            db_account = Account.get_or_none(bank=bank, account_id=account.id)
            if db_account is None:
                if Account.select().where(Account.bank == 3):
                    db_account = Account.create(bank=bank,
                                                account_id=account.id,
                                                user=user2.id,
                                                label=account.label,
                                                balance=account.balance)
                else:
                    db_account = Account.create(bank=bank,
                                                account_id=account.id,
                                                user=user1.id,
                                                label=account.label,
                                                balance=account.balance)
            else:
                db_account.label = account.label
                db_account.balance = account.balance
                db_account.save()

            transactions = bank_backend.iter_history(bank)

            for transaction in transactions:
                logger.info(
                    f'[Transactions] Retrieving account {transaction.label} - {transaction.amount} from {bank.name}'
                )
                db_transaction = Transaction.get_or_none(
                    account=db_account, label=transaction.label)
                if db_transaction is None:
                    db_transaction = Transaction.create(
                        account=db_account,
                        label=transaction.label,
                        amount=transaction.amount,
                        vu=False)
                else:
                    db_transaction.label = transaction.label
                    db_transaction.amount = transaction.amount
                    db_transaction.save()
class DownloadBoob(object):
    """
        Search and download
        :param name:
        :param backend_name:
        :param my_download_directory:
        :param my_links_directory:
    """

    def __init__(self, name, backend_name, my_download_directory,
                 my_links_directory):
        self.download_directory = my_download_directory
        self.links_directory = my_links_directory
        self.backend_name = backend_name
        self.backend = None
        self.weboob = Weboob()
        self.weboob.load_backends(modules=self.backend_name, )
        self.backend = self.weboob.get_backend(self.backend_name)
        self.name = name

    def get_filename(self, video, relative=False, m3u=False):
        """
            Generate filename for the video
            :param relative:
            :param m3u:
            :param video:
            :rtype : string
        """
        if relative:
            directory = os.path.join("..", DOWNLOAD_DIRECTORY,
                                     self.backend_name)
        else:
            directory = os.path.join(self.download_directory, self.backend_name)
            if not os.path.exists(directory.encode('utf8')):
                os.makedirs(directory.encode('utf8'))
        if not m3u:
            ext = video.ext
            if not ext:
                ext = 'avi'
        else:
            ext = 'm3u'
        return "%s/%s.%s" % (directory, removenonascii(video.id), ext)

    def get_linkname(self, video, m3u=False):
        """
            Generate filename for the link
            :param m3u:
            :param video:
            :rtype : string
        """
        if not os.path.exists(self.links_directory.encode('utf8')):
            os.makedirs(self.links_directory.encode('utf8'))
        if not m3u:
            ext = video.ext
            if not ext:
                ext = 'avi'
        else:
            ext = 'm3u'
        if not kodi:
            misc = video.date.strftime("%y-%m-%d")
            if not misc:
                misc = video.id
            return "%s/%s (%s).%s" % (self.links_directory,
                                      removespecial(video.title),
                                      removespecial(misc), ext)
        else:
            return "%s/%s.%s" % (self.links_directory,
                                 removespecial(video.title), ext)

    def is_downloaded(self, video):
        """
            Check if the video has already been downloaded
            :param video:
            :rtype : bool
        """
        if (os.path.isfile(self.get_filename(video).encode('utf8')) or
            os.path.isfile(self.get_filename(video,
                                             m3u=True).encode('utf8'))):
            logging.info("%s Already downloaded : %s" %
                         (video.id, video.title))
            return True
        logging.debug("%s To be downloaded : %s" %
                      (video.id, video.title))
        return False

    def init_dir(self):
        """
            create directory
        """
        if not os.path.isdir(self.links_directory.encode('utf8')):
            logging.debug("  create link directory : %s" % self.links_directory)
            os.makedirs(self.links_directory.encode('utf8'))
        else:
            logging.debug("  existing link directory : %s" % self.links_directory)
        if kodi:
            file_name = os.path.join(self.links_directory, 'tvshow.nfo')
            show_name = self.links_directory.split("/")[-1]
            if not os.path.isfile(file_name.encode('utf8')):
                logging.debug("  create %s" % file_name)
                f = codecs.open(file_name, "w", "utf-8")
                f.write(u"<tvshow>\n")
                f.write(u"  <title>" + show_name + u"</title>\n")
                f.write(u"</tvshow>\n")
                f.close()
            else:
                logging.debug("  existing %s" % file_name)

    def do_search(self,
                  pattern=None,
                  pattern_type="search",
                  sortby=CapVideo.SEARCH_RELEVANCE,
                  nsfw=False):
        """
            Search for videos
            :param pattern:
            :param pattern_type:
            :param sortby:
            :param nsfw:
            :return:
        """
        logging.debug("  Searching for videos for %s" % self.name)
        list_videos = []
        if pattern_type == "search":
            list_videos = self.backend.search_videos(pattern, sortby, nsfw)
        elif pattern_type == "ls":
            sys.path.insert(
                0, backend_directory + "/" + self.backend.name + "/"
            )  # HACK
            if 'video' in sys.modules:
                del sys.modules['video']
            if self.backend.name == "arte":
                from video import ArteVideo as Video_Init
            elif self.backend.name == "canalplus":
                from video import CanalplusVideo as Video_Init
            elif self.backend.name == "arretsurimages":
                from video import ArretSurImagesVideo as Video_Init
            elif self.backend.name == "dailymotion":
                from video import DailymotionVideo as Video_Init
            elif self.backend.name == "nolifetv":
                from video import NolifeTVVideo as Video_Init
            elif self.backend.name == "youtube":
                from video import YoutubeVideo as Video_Init
            else:
                from weboob.capabilities.video import BaseVideo as Video_Init  # END OF HACK
            for videoid in videoob_list_rep(pattern, self.backend):
                list_videos.append(Video_Init(videoid))
        if list_videos:
            logging.debug("  found videos for %s" % self.name)
        else:
            logging.error("  did not found videos for %s" % self.name)
        return list_videos

    def filter_list(self, list_videos, title_regexp, id_regexp, author_regexp,
                    title_exclude, max_results):
        """
            Filter the list after the search
            :param list_videos:
            :param title_regexp:
            :param id_regexp:
            :param author_regexp:
            :param title_exclude:
            :param max_results:
            :return:
        """
        logging.debug("  filtering list of found video for %s" % self.name)
        videos = []
        num_videos = 0
        for video in list_videos:
            if is_ok(video, title_regexp, id_regexp, author_regexp,
                     title_exclude):
                if not self.is_downloaded(video):
                    videoob_get_info(self.backend, video)
                    if not video:
                        logging.error('Error in Video: %s' % video)
                    elif not video.url:
                        logging.error(
                            'Error: the URL is not available : %s (%s)' %
                            (video.url, video.id))
                    else:
                        if is_ok(video, title_regexp, id_regexp, author_regexp,
                                 title_exclude):
                            num_videos += 1
                            if not self.is_downloaded(video):
                                videos.append(video)
                                print("New Video :  %s" % video.title)
                                print("    Description:%s" % video.description)
                                print("    Author:%s" % video.author)
                                print("    Id:%s" % video.id)
                                print("    Duration:%s" % video.duration)
                                print("    Date:%s" % video.date)
                            if num_videos == max_results:
                                break
        return videos

    def write_m3u(self, video):
        """
            Write m3u file for streaming files
            :param video:
            :return:
        """
        logging.debug("  Write m3u for %s" % video.title)
        if video.ext == "m3u" or video.ext == "m3u8":
            return do_download(video, self.get_filename(video))
        else:
            if matched(video.url, "\.m3u") or matched(video.url, "\.m3u8"):
                return do_download(video, self.get_filename(video))
            else:
                dest = self.get_filename(video, m3u=True)
                show_name = self.links_directory.split("/")[-1]
                logging.debug("  create %s" % dest)
                f = codecs.open(dest, "w", "utf-8")
                f.write("#EXTINF: ")
                if video.duration:
                    f.write(str(video.duration))
                else:
                    f.write(str(-1))
                f.write(", " + show_name + " - " + video.title + "\n")
                f.write(video.url)
                f.close()
                return 0

    def set_link(self, video, m3u=False):
        """
            Create link file
            :param video:
            :param m3u:
        """
        linkname = self.get_linkname(video, m3u=m3u)
        idname = self.get_filename(video, relative=True, m3u=m3u)
        absolute_idname = self.get_filename(video, m3u=m3u)
        if not os.path.islink(linkname.encode('utf8')) and os.path.isfile(absolute_idname.encode('utf8')):
            logging.info("  %s -> %s" % (linkname, idname))
            os.symlink(idname.encode('utf8'), linkname.encode('utf8'))
        else:
            logging.debug("  Not generating link for %s" % video.title)

    def do_mv(self, video, m3u=False):
        """
            move video file after download
            :param video:
            :param m3u:
        """
        linkname = self.get_linkname(video, m3u=m3u)
        absolute_idname = self.get_filename(video, m3u=m3u)
        if not os.path.isfile(linkname.encode('utf8')) and os.path.isfile(absolute_idname.encode('utf8')):
            logging.info("  %s => %s" % (absolute_idname, linkname))
            os.rename(absolute_idname.encode('utf8'), linkname.encode('utf8'))
            open(absolute_idname, 'w').close()
        else:
            logging.debug("  Not moving file %s" % linkname)

    def download(self,
                 pattern=None,
                 sortby=CapVideo.SEARCH_RELEVANCE,
                 nsfw=False,
                 max_results=20,
                 title_regexp=None,
                 id_regexp=None,
                 pattern_type="search",
                 author_regexp=None,
                 title_exclude=None):
        # create directory for links
        """
            Main process
            :param pattern:
            :param sortby:
            :param nsfw:
            :param max_results:
            :param title_regexp:
            :param id_regexp:
            :param pattern_type:
            :param author_regexp:
            :param title_exclude:
        """
        self.init_dir()

        # search for videos
        list_videos = self.do_search(pattern, pattern_type, sortby, nsfw)

        # Filter the list of videos
        videos = self.filter_list(list_videos, title_regexp, id_regexp,
                                  author_regexp, title_exclude, max_results)

        # download videos
        if videos:
            for video in videos:
                print("Downloading... " + video.title)
                if kodi:  # THE "TITLE" BECOME "S00E00 - TITLE (ID)"
                    rewrite_title(video)
                if down_live:  # CREATE LIVE STREAM
                    ret = self.write_m3u(video)
                else:  # DOWNLOAD VIDEO
                    ret = do_download(video, self.get_filename(video)
                                      )  # FOR DIRECT LINKS
                    if not ret:
                        ret = do_conv(video, self.get_filename(video)
                                      )  # FOR INDIRECT LINKS
                if not ret:
                    if not kodi:
                        self.set_link(
                            video
                        )  # CREATE LINKS FOR A BEAUTIFULL LIBRARY
                    else:
                        self.do_mv(video)  # MOVE FILES FOR A BEAUTIFULL LIBRARY
                        # CREATE NFO FILES FOR KODI
                        write_nfo(self.get_linkname(video),
                                  self.links_directory, self.backend_name,
                                  video)
                    print("Downloaded : " + video.title)
                else:
                    assert isinstance(video.title, basestring)
                    print("Failed download :" + video.title)
Exemple #26
0
def load_transactions(request):
    '''
    Function to download transactions (or any related information) from banks through backends managed by Weboob
    And load them in the database managed by Gesfi
    :param request:
    :return: render: to render the list of transactions got from accounts managed by Weboob Backends
    '''

    w = Weboob()

    # Check if repositories where à located Backends are up to date
    check_weboob_repositories(w)

    # List of Banks in Backends with Weboob (for which we have te capability to connect with to get information)
    listbanks = w.load_backends(CapBank)

    # List of accounts got from Banks
    list_of_accounts = list(w.iter_accounts())

    # List of accounts in DataBase manage by Gesfipe
    db_accounts_list = Accounts.objects.all().filter(owner_of_account=request.user)
    print(db_accounts_list)

    # list_trans = Transactions.objects.all()
    list_uniques = list_unique_of_numbers()
    # TODO: Vérifier d'abord si la liste chargée existe déjà dans la base de données (via comparaison avec unique_id_of_transaction)

    list_of_transactions = []

    for real_account in list_of_accounts:
        # print(real_account)
        for db_account in db_accounts_list:
            if real_account.id == db_account.num_of_account:
                logger.warning("------------------------------------")
                logger.warning("real_account.id = {} ******  db_account.num_of_account = {}".format(real_account.id,
                                                                               db_account.num_of_account))
                logger.warning("Type of account = {}".format(db_account.type_int_of_account))
                logger.warning("------------------------------------")
                # TODO: Injecter la dernière date en base de donnée dans w.iter_history(real_account, date) afin de limiter la vérification
                transactions_of_banks_account = w.iter_history(real_account)

                for transaction in transactions_of_banks_account:
                    # print(transaction)
                    transac = {}
                    Trans = Transactions()

                    Trans.account = db_account
                    # print(Trans.account)

                    transac['date'] = transaction.date  # Debit date on the bank statement
                    Trans.date_of_transaction = transaction.date
                    # print(Trans.date_of_transaction)

                    # Real date, when the payment has been made; usually extracted from the label or from credit card info
                    if transaction.rdate:
                        transac['rdate'] = transaction.rdate
                        Trans.real_date_of_transaction = transaction.rdate
                    else:
                        transac['rdate'] = transaction.date
                        Trans.real_date_of_transaction = transaction.date
                    # print(Trans.real_date_of_transaction)

                    # Value date, or accounting date; usually for professional accounts
                    if transaction.vdate:
                        transac['vdate'] = transaction.vdate
                        Trans.value_date_of_transaction = transaction.vdate
                    else:
                        transac['vdate'] = transaction.rdate
                        Trans.value_date_of_transaction = transaction.rdate
                    # print(Trans.value_date_of_transaction)

                    transac[
                        'type'] = transaction.type  # Type of transaction, use TYPE_* constants', default=TYPE_UNKNOWN
                    Trans.type_int_of_transaction = transaction.type
                    # Trans.type_of_transaction = transaction.type
                    # print(Trans.type_int_of_transaction)

                    transac['raw'] = transaction.raw  # Raw label of the transaction
                    Trans.name_of_transaction = transaction.raw
                    # print(Trans.name_of_transaction)

                    transac['category'] = transaction.category  # Category of the transaction
                    Trans.type_of_transaction = transaction.category
                    # logger.warning('transaction.category ==> ==> ==> : %s', transaction.category)
                    # Trans.category_of_transaction = transaction.category
                    # print(Trans.type_of_transaction)

                    transac['label'] = transaction.label  # Pretty label
                    Trans.label_of_transaction = transaction.label
                    # print(Trans.label_of_transaction)

                    transac['amount'] = transaction.amount  # Amount of the transaction
                    Trans.amount_of_transaction = transaction.amount
                    # print(Trans.amount_of_transaction)

                    Trans.create_key_words()
                    # print(Trans.key_words)

                    Trans.unique_id_of_transaction = Trans.unique_id(account_id=db_account.num_of_account)
                    # print(Trans.unique_id_of_transaction)

                    if Trans.unique_id_of_transaction not in list_uniques:
                        Trans.save()
                        list_uniques.append(Trans.unique_id_of_transaction)
                        list_of_transactions.append(transac)
                        # print('Sauvegarde de Trans: ===>>>>>> {}\n'.format(Trans))
                        logger.warning('Sauvegarde de Trans: ===>>>>>> %s', Trans)

    context = {'list_of_accounts': list_of_accounts, 'list_of_transactions': list_of_transactions, }

    return render(request, 'ManageGesfi/load_transactions_from_account.html', context)
Exemple #27
0
def get_list_of_available_accounts(request):
    '''
    Function to get list of accounts of  bank with local backend (cf. ~/.config/weboob/backends
    :param request:
    :return: render: to render list_of_banks and list_of_accounts (but banks and account are not linked (to be updated)
    '''
    logger.warning("Entering in function get_list_of_available_accounts")
    w = Weboob()
    check_weboob_repositories(w)
    listbanks = w.load_backends(CapBank)
    list_of_banks = []
    list_of_db_accounts = []

    # db_accounts_list = db_Accounts.objects.all().filter(owner_of_account=request.user)
    db_accounts_list = db_Accounts.objects.all()

    for key in db_accounts_list:
        # print('db_accounts_list::::::: {}'.format(key.num_of_account))
        list_of_db_accounts.append(key.num_of_account)

    for key, val in listbanks.items():
        # print("Banque key (Dict) : {} \t\t\t ===> \t {}".format(key, val.description))
        data_bank = {}
        data_bank['module'] = key
        data_bank['name'] = val.DESCRIPTION
        list_of_banks.append(data_bank)
    list_of_banks.sort(key=lambda k: k['module'])

    # print('************************ : {}'.format(list_of_banks))
    logger.warning('list_of_banks ==> ==> ==> : %s', list_of_banks)

    # TODO: cela fonctionne car je n'ai qu'une banque  en backends mais sinon la liste des "accounts" sera globale.
    # TODO: il manque donc l'association account et banque

    list_of_accounts = list(w.iter_accounts())

    for real_account in list_of_accounts:
        # print('REAL ACCOUNT: {}'.format(real_account))
        # print('LIST_OF_DB_ACCOUNTS: {}'.format(list_of_db_accounts))
        if real_account.id in list_of_db_accounts:
            # print('if real_account.id in list_of_db_accounts: ==>> REAL ACCOUNT.ID: {}'.format(real_account.id))
            # print('if real_account.id in list_of_db_accounts: ==>> LIST_OF_DB_ACCOUNTS: {}'.format(list_of_db_accounts))
            # erase old data and replace by data coming from bank
            db_account = db_Accounts.objects.get(num_of_account=real_account.id)
            # print('if real_account.id == db_account.num_of_account en base de données: {}  -- à la banque: {}'.format(db_account.name_of_account, real_account.label))
            db_account.name_of_account = real_account.label
            db_account.type_int_of_account = real_account.type
            db_account.balance_of_account = real_account.balance
            # TODO: ajouter le nom de la banque au compte
            # TODO: ajouter le user en cours
            # db_account.bank = real_account.parent.name
            # u = User.objects.get(name=request.user.name)
            # u.db_accounts_set.add(db_account)
            # db_account.owner_of_account.set([request.user,])
            db_account.save()

        else:
            # print('Else: ==>> REAL ACCOUNT.ID: {}'.format(real_account.id))
            #  print('Else: ===> LIST_OF_DB_ACCOUNTS: {}'.format(list_of_db_accounts))
            # create account
            new_account = db_Accounts()
            new_account.num_of_account = real_account.id
            new_account.name_of_account = real_account.label
            new_account.type_int_of_account = real_account.type
            new_account.balance_of_account = real_account.balance
            # TODO: affecter un user + le type (en texte)
            # new_account.owner_of_account = request.user
            # print('new_account.name_of_account = {}'.format(new_account.name_of_account))
            new_account.save()
            # new_account.owner_of_account.set(request.user)
            # new_account.create()

    # list_of_accounts.sort(key=lambda k: k['label'])
    context = {'list_of_banks': list_of_banks, 'list_of_accounts': list_of_accounts, }

    logger.warning("Entering in function get_list_of_available_accounts")

    return render(request, 'ManageGesfi/list_of_available_accounts.html', context)