Пример #1
0
def random():
    soup = random_parser()
    title = soup.find('h1', id='section_0').text
    body = soup.find('div', 'mw-parser-output')
    first = body.find('p').text
    first = re.sub('\[\d+\]', '', first)
    return extformat(prefs['wikipedia']['format'], locals())
Пример #2
0
def format_story(story, style):
    desc = BeautifulSoup(story.description.text, 'html.parser')
    head = BeautifulSoup(story.title.text, 'html.parser')

    timefmtprefix = '%a, %d %b %Y %H:%M:%S'
    try:
        pubdate = datetime.strptime(story.pubdate.text, timefmtprefix + ' %z')
    except ValueError:
        try:
            # possibly a named timezone
            pubdate = datetime.strptime(story.pubdate.text,
                                        timefmtprefix + ' %Z')
        except ValueError:
            try:
                # f**k the timezone
                pubdate = datetime.strptime(
                    story.pubdate.text[:story.pubdate.text.rfind(' ')],
                    timefmtprefix)
            except ValueError:
                # F**K THE TIME
                pubdate = datetime.min

    fstr = (prefs['news']['format']
            if style is None else prefs['news'][style + '_format'])

    return extformat(
        fstr,
        description=desc.text,
        title=head.text,
        url=story.guid.text,
        short_url=misc.shorten_pretty(story.guid.text),
        date=pubdate,
    )
Пример #3
0
def format_stock(dat, symbol):
    fstr = prefs['stocks']['format']
    if isinstance(symbol, dict):
        if 'style' in symbol:
            fstr = prefs['stocks'][symbol['style'] + '_format']
        if ('source' in symbol
                and symbol['source'] != prefs['stocks']['source']):
            dat = stock_dat(symbol['Symbol'], symbol['source'])
        if 'display_symbol' in symbol:
            dat['Symbol'] = symbol['display_symbol']

    if dat['Currency'] in prefs['stocks']['excluded_currencies']:
        dat['Currency'] = ''

    return extformat(fstr,
                     dat,
                     default=lambda: extformat(prefs['stocks']['format'], dat))
Пример #4
0
def format_event(time, event):
    today, tomorrow = today_times()

    leader = ' ' + prefs['vert'] + ' '
    if time == prefs['dates']['all_day'] and event['duration'].days > 1:
        time = extformat(prefs['dates']['all_day'], event)
        event['summary'] += (' (day ' +
                             str((today - event['start']).days + 1) + ' of ' +
                             str((event['end'] - event['start']).days) + ')')
    elif time == prefs['dates']['ending']:
        time = extformat(prefs['dates']['ending'],
                         event,
                         time=misc.hoursminutes(event['end']))

    return misc.format_left(event['summary'],
                            leader=leader,
                            firstline=time + leader)
Пример #5
0
def moon(location=prefs['weather']['location']):
    times = weather('astronomy', location)['moon_phase']
    phase = times['phaseofMoon'].lower()
    percent = times['percentIlluminated']
    graphic = (prefs['weather']['moon']['bright_graphic']
               if int(times['percentIlluminated']) > 50 else
               prefs['weather']['moon']['dark_graphic'])

    return extformat(prefs['weather']['moon']['format'], **locals())
Пример #6
0
def suntimes(location=prefs['weather']['location']):
    risetime, settime = sunrise(location), sunset(location)
    daylight = settime - risetime

    risefmt = extformat(prefs['weather']['sun']['rise_format'],
                        sunrise=misc.hoursminutes(risetime, fillchar=''))

    setfmt = extformat(prefs['weather']['sun']['set_format'],
                       sunset=misc.hoursminutes(settime, fillchar=''))

    dayfmt = extformat(prefs['weather']['sun']['daylight_format'],
                       daylight=misc.formatdelta(daylight))

    return extformat(
        prefs['weather']['sun']['format'],
        sunrise=risefmt,
        sunset=setfmt,
        daylight=dayfmt,
    )
Пример #7
0
def due(day=1):
    hws = homework()
    ret = []
    _, day = dates.today_times(day)
    for hw in hws:
        if 'due' in hw and hw['due'] <= day:
            ret.append(
                misc.format_left(extformat(
                    prefs['myhomework']['assignment_format'], hw),
                                 firstline=prefs['myhomework']['check']))
    return ''.join(ret).rstrip()
Пример #8
0
def format_tweet(tweet, style=None):
    fvars = tweet.AsDict()
    if 'retweeted_status' in fvars and fvars['retweeted_status'] is not None:
        fvars.update(fvars['retweeted_status'])
    fvars['text'] = html.unescape(fvars['text'])
    fvars.update({
        'date': get_date(tweet),
    })
    fvars.update({
        'pretty_text':
        misc.fill(
            extformat(prefs['twitter']['pretty_text_format'],
                      fvars,
                      text=fvars['text'].replace('\n', '\n\n')))
    })
    if style is not None:
        fstr = prefs['twitter'][style + '_format']
    else:
        fstr = prefs['twitter']['format']
    return extformat(fstr, fvars)
Пример #9
0
def day_forecast(day=0, location=prefs['weather']['location']):
    forecast = weather('forecast', location)['forecast']
    day_data = forecast['simpleforecast']['forecastday'][day]
    txt_data = forecast['txt_forecast']['forecastday'][day]

    return extformat(
        prefs['weather']['forecast_format'],
        forecast=forecast,
        day_data=day_data,
        txt_data=txt_data,
        high=int(day_data['high'][prefs['weather']['temp']]),
        low=int(day_data['low'][prefs['weather']['temp']]),
        precip=int(day_data['pop']),
        conds=day_data['conditions'],
        summary=txt_data['fcttext'],
    )
Пример #10
0
def today_classes(day=0):
    class_cals = calendar_match(prefs['calendar']['class_pat'])

    classes = []
    for cal in class_cals:
        classes.extend(today_events(cal['id'], day=day))

    out = []
    for event in classes:
        event = event_times(event)
        if '_daily_report_processed' not in event:
            event['summary'] += extformat(' ' + prefs['dates']['ending'],
                                          event,
                                          time=misc.hoursminutes(event['end']))
            event['_daily_report_processed'] = True
        out.append(format_event(misc.hoursminutes(event['start']), event))

    return ''.join(out).rstrip()
Пример #11
0
def report():
    msg = extformat(prefs['format'])

    return misc.deduplicate_rules(msg)