コード例 #1
0
ファイル: kimai.py プロジェクト: alexfrancavilla/kimai-cli
def get_todays_records():
    """Returns all records for the current day"""
    payload = _build_payload('getTimesheet', config.get('ApiKey'),
                             dates.parse('today at 00:00').isoformat(),
                             dates.parse('today at 23:59:59').isoformat())
    response = KimaiResponse(_do_request(payload))
    return [Record(r) for r in response.items]
コード例 #2
0
def add_record(start_time, end_time, duration, favorite, project_id, task_id,
               comment):
    if not end_time and not duration:
        print_error('Need either an end time or a duration.')
        return

    if not favorite and not (project_id and task_id):
        print_error(
            'Need either the name of a favorite or a task id and project id')
        return

    start_time = dates.parse(start_time)

    if start_time is None:
        print_error('Could not parse start date')
        return

    if duration:
        # We assume that any duration should be added to the start time
        # since it doesn't make sence to have the end time be before the
        # start time
        end_time = dates.parse('+' + duration, start_time)
    else:
        end_time = dates.parse(end_time)

    if end_time is None:
        print_error('Could not parse end date')
        return

    if favorite:
        try:
            favorite = fav.get_favorite(favorite)
            project_id = favorite.Project
            task_id = favorite.Task
        except RuntimeError as e:
            print_error(str(e))
            return

    kimai.add_record(start_time,
                     end_time,
                     project_id,
                     task_id,
                     comment=comment)
コード例 #3
0
ファイル: hut.py プロジェクト: Jazzparrr/HutCrawler
    def get_classes(self, club_id, tomorrow=False):
        classes = []

        url = 'https://www.myhut.pt/myhut/functions/get-aulas.php'

        headers = {
            'Referer': 'https://www.myhut.pt/myhut/aulas/',
            'X-Requested-With': 'XMLHttpRequest',
        }

        get_data = {
            'id':
            club_id,
            'date':
            dates.f(
                dates.now() if not tomorrow else dates.add(
                    dates.now(), days=1), '%Y-%m-%d'),
            'rnd':
            '1453419300746'
        }

        response, _, tree = self.url_etree('GET',
                                           url,
                                           headers=headers,
                                           params=get_data)

        panels = tree.cssselect('div.panel')

        for c in panels:
            info = {}

            panel_class = c.get('class').split()
            if 'panel-default' in panel_class:
                info['status'] = 'available'
            elif 'panel-red' in panel_class:
                info['status'] = 'full'
            elif 'panel-off' in panel_class:
                info['status'] = 'unavailable'

            c = c.cssselect('a.accordion-toggle')[0]
            info['class_id'] = c.get('href')[len('#aula'):]

            divs = c.xpath('./div')
            info['time'] = dates.parse(
                divs[0].xpath('.//span')[0].text.strip(), '%H:%M')
            info['class_name'] = divs[1].xpath('.//span')[0].text.strip()
            info['studio'] = divs[2].text.strip()
            info['duration'] = divs[3].text.strip()
            classes.append(info)

        return classes
コード例 #4
0
ファイル: hut.py プロジェクト: C1rcu17/HutCrawler
    def get_classes(self, club_id, tomorrow=False):
        classes = []

        url = 'https://www.myhut.pt/myhut/functions/get-aulas.php'

        headers = {
            'Referer': 'https://www.myhut.pt/myhut/aulas/',
            'X-Requested-With': 'XMLHttpRequest',
        }

        get_data = {
            'id': club_id,
            'date': dates.f(dates.now() if not tomorrow else dates.add(dates.now(), days=1), '%Y-%m-%d'),
            'rnd': '1453419300746'
        }

        response, _, tree = self.url_etree('GET', url, headers=headers, params=get_data)

        panels = tree.cssselect('div.panel')

        for c in panels:
            info = {}

            panel_class = c.get('class').split()
            if 'panel-default' in panel_class:
                info['status'] = 'available'
            elif 'panel-red' in panel_class:
                info['status'] = 'full'
            elif 'panel-off' in panel_class:
                info['status'] = 'unavailable'

            c = c.cssselect('a.accordion-toggle')[0]
            info['class_id'] = c.get('href')[len('#aula'):]

            divs = c.xpath('./div')
            info['time'] = dates.parse(divs[0].xpath('.//span')[0].text.strip(), '%H:%M')
            info['class_name'] = divs[1].xpath('.//span')[0].text.strip()
            info['studio'] = divs[2].text.strip()
            info['duration'] = divs[3].text.strip()
            classes.append(info)

        return classes
コード例 #5
0
ファイル: app.py プロジェクト: Jazzparrr/HutCrawler
def book_class(job):
    objdump.stdout(job)
    time = dates.parse(job['time'], TIME_FORMAT)

    c = None

    # Wait for available
    while True:
        try:
            c = HUT.get_class(MEMBER_INFO['clubs'][job['club']], job['class'],
                              time, time)
            if not c:
                raise Exception('no class found')
        except Exception as e:
            print(str(e))
            print('waiting 10 seconds before retry check')
            sleep(10)
        else:
            break

    objdump.stdout(c)

    while True:
        try:
            HUT.do_login()
            HUT.book_class(c['class_id'], MEMBER_INFO['member_id'])
        except Exception as e:
            print(str(e))
            print('waiting 5 seconds before retry book')
            sleep(5)
        else:
            break

    CRAWLER_SMTP.send_email(
        HUT.email, 'Marcação de aula no Fitness Hut',
        'Bom dia :D\n\n\tMarquei uma aula de {} de {} no {} do clube de {} que começa hoje, às {}.\n\nAproveita, e bom treino\n\t{}'
        .format(c['class_name'], c['duration'], c['studio'], job['club'],
                job['time'], CRAWLER_SMTP.name))
コード例 #6
0
ファイル: app.py プロジェクト: C1rcu17/HutCrawler
def main(myhut_info, smtp_info, plan):
    global HUT, CRAWLER_SMTP, SCHEDULER

    HUT = Hut(myhut_info['username'], myhut_info['password'])
    CRAWLER_SMTP = smtp.Server(
        smtp_info['server'],
        smtp_info['port'],
        smtp_info['username'],
        smtp_info['password'],
        smtp_info['name'],
        smtp_info['email'])

    member_info_update()

    # job = {
    #   "club": "Braga",
    #   "class": "V-CLASS CYCLING",
    #   "day_of_week": "wed",
    #   "time": "10:30"
    # }
    # book_class(job)
    # return

    SCHEDULER = BlockingScheduler()
    SCHEDULER.add_job(member_info_update, trigger='cron', hour=0, minute=0)

    for job in plan:
        start_date = dates.parse(job['time'], TIME_FORMAT)
        book_date = dates.sub(start_date, hours=10)
        if book_date < MIDNIGHT:
            book_date = dates.dup(MIDNIGHT)

        # day_of_week = mon,tue,wed,thu,fri,sat,sun
        SCHEDULER.add_job(
            book_class, args=[job], trigger='cron',
            day_of_week=job['day_of_week'], hour=book_date.hour, minute=book_date.minute)

    SCHEDULER.start()
コード例 #7
0
ファイル: app.py プロジェクト: Jazzparrr/HutCrawler
def main(myhut_info, smtp_info, plan):
    global HUT, CRAWLER_SMTP, SCHEDULER

    HUT = Hut(myhut_info['username'], myhut_info['password'])
    CRAWLER_SMTP = smtp.Server(smtp_info['server'], smtp_info['port'],
                               smtp_info['username'], smtp_info['password'],
                               smtp_info['name'], smtp_info['email'])

    member_info_update()

    # job = {
    #   "club": "Braga",
    #   "class": "V-CLASS CYCLING",
    #   "day_of_week": "wed",
    #   "time": "10:30"
    # }
    # book_class(job)
    # return

    SCHEDULER = BlockingScheduler()
    SCHEDULER.add_job(member_info_update, trigger='cron', hour=0, minute=0)

    for job in plan:
        start_date = dates.parse(job['time'], TIME_FORMAT)
        book_date = dates.sub(start_date, hours=10)
        if book_date < MIDNIGHT:
            book_date = dates.dup(MIDNIGHT)

        # day_of_week = mon,tue,wed,thu,fri,sat,sun
        SCHEDULER.add_job(book_class,
                          args=[job],
                          trigger='cron',
                          day_of_week=job['day_of_week'],
                          hour=book_date.hour,
                          minute=book_date.minute)

    SCHEDULER.start()
コード例 #8
0
ファイル: app.py プロジェクト: C1rcu17/HutCrawler
def book_class(job):
    objdump.stdout(job)
    time = dates.parse(job['time'], TIME_FORMAT)

    c = None

    # Wait for available
    while True:
        try:
            c = HUT.get_class(MEMBER_INFO['clubs'][job['club']], job['class'], time, time)
            if not c:
                raise Exception('no class found')
        except Exception as e:
            print(str(e))
            print('waiting 10 seconds before retry check')
            sleep(10)
        else:
            break

    objdump.stdout(c)

    while True:
        try:
            HUT.do_login()
            HUT.book_class(c['class_id'], MEMBER_INFO['member_id'])
        except Exception as e:
            print(str(e))
            print('waiting 5 seconds before retry book')
            sleep(5)
        else:
            break

    CRAWLER_SMTP.send_email(HUT.email, 'Marcação de aula no Fitness Hut',
        'Bom dia :D\n\n\tMarquei uma aula de {} de {} no {} do clube de {} que começa hoje, às {}.\n\nAproveita, e bom treino\n\t{}'.format(
            c['class_name'], c['duration'], c['studio'], job['club'], job['time'], CRAWLER_SMTP.name
        ))
コード例 #9
0
ファイル: app.py プロジェクト: Jazzparrr/HutCrawler
import smtp
import objdump
import dates
from apscheduler.schedulers.blocking import BlockingScheduler
from hut import Hut, HutException
from time import sleep

dates.set_tz('Europe/Lisbon')

TIME_FORMAT = '%H:%M'
MIDNIGHT = dates.parse('00:00', TIME_FORMAT)
HUT = None
CRAWLER_SMTP = None
SCHEDULER = None
MEMBER_INFO = None


def main(myhut_info, smtp_info, plan):
    global HUT, CRAWLER_SMTP, SCHEDULER

    HUT = Hut(myhut_info['username'], myhut_info['password'])
    CRAWLER_SMTP = smtp.Server(smtp_info['server'], smtp_info['port'],
                               smtp_info['username'], smtp_info['password'],
                               smtp_info['name'], smtp_info['email'])

    member_info_update()

    # job = {
    #   "club": "Braga",
    #   "class": "V-CLASS CYCLING",
    #   "day_of_week": "wed",
コード例 #10
0
ファイル: app.py プロジェクト: C1rcu17/HutCrawler
import smtp
import objdump
import dates
from apscheduler.schedulers.blocking import BlockingScheduler
from hut import Hut, HutException
from time import sleep

dates.set_tz('Europe/Lisbon')

TIME_FORMAT = '%H:%M'
MIDNIGHT = dates.parse('00:00', TIME_FORMAT)
HUT = None
CRAWLER_SMTP = None
SCHEDULER = None
MEMBER_INFO = None


def main(myhut_info, smtp_info, plan):
    global HUT, CRAWLER_SMTP, SCHEDULER

    HUT = Hut(myhut_info['username'], myhut_info['password'])
    CRAWLER_SMTP = smtp.Server(
        smtp_info['server'],
        smtp_info['port'],
        smtp_info['username'],
        smtp_info['password'],
        smtp_info['name'],
        smtp_info['email'])

    member_info_update()