Example #1
0
def update():
	browser= webdriver.PhantomJS(executable_path = "path to PhantomJS")
	browser.get(url)

	html = browser.page_source
	browser.close()

	content = BeautifulSoup(html, 'html5lib')
	usage = {}
	if len(content.find_all(id="bwm-daily-grid")) == 0:
		## exit if content not found or connection error
		return False
	for row in content.find_all(id="bwm-daily-grid")[0].find_all("tr"):
		if len(row.find_all(class_="total")) == 0:
			continue ##skip header rows or rows not containing data
		date  = str(row.find_all(class_="rtitle")[0].text)
		dl    = float((str(row.find_all(class_="dl")[0].text)).split(" ")[0])
		ul    = float((str(row.find_all(class_="ul")[0].text)).split(" ")[0])
		total = float((str(row.find_all(class_="total")[0].text)).split(" ")[0])
		usage[date] = {"download":dl,"upload":ul,"total":total}

		objs= Day.objects.all().filter(date=date)
		if (len(objs) > 0):
			obj = objs[0]
			if (obj.download != dl or obj.upload != ul or obj.total != total):
				obj.download = dl
				obj.upload= ul
				obj.total = total
				obj.save()
		else:
			obj= Day(download=dl,upload=ul,total=total,date=date)
			obj.save()
	return True
Example #2
0
def update():
    browser = webdriver.PhantomJS(executable_path="path")
    browser.get(url)

    html = browser.page_source
    browser.close()

    content = BeautifulSoup(html, 'html5lib')
    usage = {}
    if len(content.find_all(id="bwm-daily-grid")) == 0:
        ## exit if content not found or connection error
        return False
    for row in content.find_all(id="bwm-daily-grid")[0].find_all("tr"):
        if len(row.find_all(class_="total")) == 0:
            continue  ##skip header rows or rows not containing data
        date = str(row.find_all(class_="rtitle")[0].text)
        dl = float((str(row.find_all(class_="dl")[0].text)).split(" ")[0])
        ul = float((str(row.find_all(class_="ul")[0].text)).split(" ")[0])
        total = float(
            (str(row.find_all(class_="total")[0].text)).split(" ")[0])
        usage[date] = {"download": dl, "upload": ul, "total": total}

        objs = Day.objects.all().filter(date=date)
        if (len(objs) > 0):
            obj = objs[0]
            if (obj.download != dl or obj.upload != ul or obj.total != total):
                obj.download = dl
                obj.upload = ul
                obj.total = total
                obj.save()
        else:
            obj = Day(download=dl, upload=ul, total=total, date=date)
            obj.save()
    return True
Example #3
0
    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(PlotSymbolView, self).get_context_data(**kwargs)
        symbols = sim.normalize_symbols(self.kwargs['symbols'])
        df = sim.price_dataframe(symbols=symbols,
                                 start=datetime.datetime(2010, 1, 1),
                                 end=datetime.datetime(2010, 12, 31),
                                 price_type='close')
        # TODO: test!
        qs = []
        for i, symbol in enumerate(df.columns):
            for row in df.records():
                qs += [
                    Day(date=datetime.date(row[0].year, row[0].month,
                                           row[0].day),
                        close=row[i + 1],
                        symbol=symbol)
                ]
        Day.objects.bulk_create(qs)

        context['df'] = df
        return d3_plot_context(context,
                               table=df,
                               title='Price History',
                               xlabel='Date',
                               ylabel='Adjusted Close')
Example #4
0
    def post(self):
        # create a new day
        data = request.get_json()
        if not data:
            ret = {
                'message': 'Provide data.',
            }
            return jsonify(ret), 422
        exercises = []
        if data.get('exercises'):
            exercises = [
                Exercise.get(each_exercise)
                for each_exercise in data.get('exercises')
            ]

        day = Day(name=data['name'], exercises=filter(None, exercises))
        db.session.add(day)
        db.session.commit()
        db.session.refresh(day)
        ret = {
            'message': 'Day created successfully.',
            'id': day.id,
            'name': data['name']
        }
        return jsonify(ret), 200
Example #5
0
def api():
    token = request.headers.get('Authorization')
    content = request.json
    if token and token.startswith('Basic') and len(content):
        fetched_content = content.pop()
        parsed_key = base64.b64decode(token.split()[1]).decode('utf-8')
        user = User.get_user(parsed_key)
        if user and fetched_content:
            payload = dict()
            payload['user_last_name'] = user.last_name
            payload_for_today = Payload.check_today(user.last_name)
            current_month = Month.check_month(user.last_name)
            if payload_for_today:
                last_time = payload_for_today.last_time
                current_time = get_time(fetched_content['time'])
                delta = hms_to_m(make_delta(last_time, current_time))
                if delta > 10:
                    payload_for_today.last_time = current_time
                    db.session.commit()
                    return Response(status='200')
                current_day = Day.get_current_day(user.last_name)
                payload['date'] = get_today()
                payload['running_min'] = delta

                if current_day:
                    current_day_running_min = current_day.running_min
                    current_day.running_min = current_day_running_min + delta
                else:
                    db.session.add(Day(**payload))

                if current_month:
                    current_month_running_min = current_month.running_min
                    current_month.running_min = current_month_running_min + delta
                else:
                    db.session.add(Month(**payload))

                payload_for_today.last_time = current_time
                db.session.commit()
            else:
                payload['last_time'] = get_time(fetched_content['time'])
                db.session.add(Payload(**payload))
                db.session.commit()
    return Response(status='200')
Example #6
0
def create():
    form = AddForm(request.form)

    if form.validate_on_submit():
        day = Day(name=form.name.data)
        db.session.add(day)
        db.session.commit()
        return redirect(url_for('days.index'))

    return render_template('days/create.html', form=form)
Example #7
0
def add_blank_day(event_id):
    name = request.data['name']
    info = Day(event_id=event_id,
               name=name,
               result_pdf=None,
               schedule_pdf=None)
    session.add(info)
    session.commit()
    session.close()
    return redirect('/dashboard')
    return {'status': 'OK'}
Example #8
0
def month(m):
    y, m = [int(x) for x in m.split("-")]
    days = Day.all().filter('year =', y).filter('month =', m).order('day').fetch(100)

    if "place_bet" in request.values:
        return raise_bet("%s-%s-%s" % (y, m, request.values.get("day")))
   
    my_bets = []
    if session.get("id"):
        my_bets = db.get([db.Key.from_path(u'Bet', "%s/%s-%s-%s" % (session['id'], y, m, d)) for d in range(1,32)])
    return render_template("spa.html", days=days, year=y, month=m, my_bets=my_bets)
Example #9
0
def day_data(y, m, d):
    if date(y, m, d) < app.config["DAY0"]:
        abort(404)
    if date(y, m, d) > date.today():
        abort(404)

    day = Day.get_day(y, m, d)
    if not day.actual_value or date(y, m, d) == date.today():
        day.do_sync(send_mail=False, mark_synced=False)
    
    if not day.actual_value:
        abort(500, "Data currently not available")

    return jsonify(to_dict(day))
Example #10
0
def populate():
    for i in range(7):
        m = Movie(
            week = i
        )
        for i in range(0,24,3):
            m.day.append(Day(
                hour = i,
                people = 0
            ))
        db.session.add(m)
        pass
            
    db.session.commit()
    return jsonify( { 'status': 'successful'} ), 200
Example #11
0
def populate_today(user):
    """
	Populate the database (`day` and `event` tables) with a list of the user's 
	events that occur today.

	:param background.models.User user: the user for which to get events
	"""
    # Add a new Day to the user's days table if it doesn't already exist
    day = user.days.filter(Day.date == TODAY).first()
    if day is None:
        day = Day(date=datetime(TODAY.year, TODAY.month, TODAY.day), user=user)
        session.add(day)
        session.commit()

    # Get today's events from WEconnect and add them to the day's events table
    activity_events = weconnect.get_todays_events(user)
    for wc_act in activity_events:
        act = user.activities.filter(
            Activity.wc_act_id == wc_act["activityId"]).first()
        for wc_ev in wc_act["events"]:
            # If the event doesn't already exist for today, add it
            event = session.query(Event).filter(
                Event.eid == wc_ev["eid"]).first()
            if event:
                modified = datetime.strptime(wc_act["dateModified"],
                                             weconnect.DATE_FMT)
                if modified >= datetime.now() - timedelta(days=1):
                    event.start_time = datetime.strptime(
                        wc_ev["dateStart"], weconnect.DATE_FMT)
                    event.end_time = act.start_time + timedelta(
                        minutes=wc_ev["duration"])
                    event.completed = wc_ev["didCheckin"]
            else:
                st = datetime.strptime(wc_ev["dateStart"], weconnect.DATE_FMT)
                et = st + timedelta(minutes=wc_ev["duration"])
                event = Event(eid=wc_ev["eid"],
                              start_time=st,
                              end_time=et,
                              completed=wc_ev["didCheckin"],
                              day=day,
                              activity=act)
                session.add(event)
    session.commit()
Example #12
0
def start_before(date_before):
    # 获取日期内容
    url = 'https://news-at.zhihu.com/api/4/news/before/{}'.format(date_before)
    loop = asyncio.get_event_loop()
    task = asyncio.ensure_future(get_json_data(url))
    loop.run_until_complete(task)
    json_data = task.result()
    date = json_data['date']
    day_query = Day.query.filter_by(date=date)
    # 日期内容写入数据库
    if day_query.count() == 0:
        new_day = Day(date=date, data=json.dumps(json_data), update=datetime.now())
        db_session.add(new_day)
        db_session.commit()
    else:
        day_query.update({'data': json.dumps(json_data), 'update': datetime.now()})

    # 异步抓取文章
    tasks = [start_article(story, date) for story in json_data['stories']]
    loop.run_until_complete(asyncio.wait(tasks))
Example #13
0
def raise_bet(day):
    y, m, d = [int(x) for x in day.split("-")]
    a = session.get('id')
    if 'api_key' in request.values:
        spac = Spac.all().filter('api_key =', request.values.get('api_key')).get()
        if spac:
            a = spac.key().name()
    if not a:
        abort(404)
    day = Day.get_day(y, m, d)
    try:
        by = float(request.values["by"])
    except:
        abort(500, "Raise by what?")
    try:
        value = float(request.values["value"])
    except:
        abort(500, "Bet on what value?")    
    try:
        Bet.raise_bet(a, day, by=by, value=value)
    except Exception, e:
        return "Error: could not raise bet. (%s)" % e
Example #14
0
def add_day():

    date = request.json['date']
    breakfast = request.json['breakfast']
    lunch = request.json['lunch']
    dinner = request.json['dinner']
    rating = request.json['rating']
    gym = request.json['gym']
    notes = request.json['notes']

    day = Day(
        date = date,
        breakfast = breakfast,
        lunch = lunch,
        dinner = dinner, 
        rating = rating,
        notes = notes,
        gym = gym)

    db.session.add(day)
    db.session.commit()

    return jsonify(success=True)
Example #15
0
def day_calc(y, m, d):
    day = Day.get_day(y, m, d)
    day.do_calc()
    return str(day.bet_shares)
Example #16
0
def day_sync(y, m, d):
    day = Day.get_day(y, m, d)
    day.do_sync()
    return str(day.actual_value)
Example #17
0
def raise_bet2(a, y, m, d, by):
    day = Day.get_day(y, m, d)
    Bet.raise_bet(a, day, by, value=float(request.values.get("value", "0.0")))    
    return "done"
for building in scrape.getBuildingsOrdered():
    b = Building.query.filter_by(name=building.name.rstrip()).first()

    for room in building.getRooms2():

        r_id = 0
        for r in b.rooms:
            if (r.roomnumber == room.number):
                r_id = r.id
                break

        for day in room.getDays2():
            day.sortTime()
            times = day.timeString()
            #times.sort()
            d = Day(name=day.day, ranges="", room_id=r_id)
            print("Adding day:", day.day)
            print("Adding times: ", times)
            d.add_time(times)
            db.session.add(d)

db.session.commit()
#             print(timeObject)
#             print(type(timeObject))
#             print(dir(timeObject))
#             print(timeObject[0].toString())

# for building in scrape.getBuildings():
#     for room in scrape.getBuildings().get(building).getRooms():
#         for day in scrape.getBuildings().get(building).getRooms().get(room).getDays():
#             print("Today's schedule for ", building,room, day)
Example #19
0
def index():
    days = Day.days_ahead(7)
    return render_template("index.html", days=days)
Example #20
0
def main():
    bets = Bet.all().order('-ctime').fetch(1000)
    #days = Day.all().filter('year =', 2011).filter('month =', 6).order('day').fetch(100)
    days = Day.all().fetch(100)
    return render_template("main.html", bets=bets, days=days)
Example #21
0
def creation(date, buy, sell):
    date = strptime(date, '%m/%d/%y %H:%M')
    
    time_h = date.tm_hour
    time_d = date.tm_mday
    time_m = date.tm_mon
    time_y = date.tm_year
      
    complete_month = 0
    complete_day = 0
    
    monthDict={1:'January', 2:'February', 3:'March', 4:'April', 5:'May', 6:'June', 7:'July', 
               8:'August', 9:'September', 10:'October', 11:'November', 12:'December'}
    
    month = Aggregate.query.filter_by(month_number=time_m, year=time_y).first()
    day = Day.query.filter_by(month_number=time_m, day_number=time_d).first()
    
    if month is None:
        # Create Month, Day, Hour
        month = Aggregate(year=time_y, month=monthDict[time_m], month_number=time_m, 
                          average_buy=-1, average_sell=-1)
        
        day = Day(year=time_y, month=monthDict[time_m], month_number=time_m, 
                  average_buy=-1, average_sell=-1, parent_month=month, day_number=time_d)
        
        hour = Hours(hour_number=time_h, buy_price=buy, sell_price=sell, belong_day=time_d, hour=day)
        
        db.session.add(month)
        db.session.add(day)
        db.session.add(hour)
        db.session.commit()
        
    else:
        day = Day.query.filter_by(day_number=time_d, month_id=month.id).first()
        
        if day is None:
            day = Day(year=time_y, month=monthDict[time_m], month_number=time_m, 
                      average_buy=-1, average_sell=-1, parent_month=month, day_number=time_d)
            
            hour = Hours(hour_number=time_h, buy_price=buy, sell_price=sell, belong_day=time_d, hour=day)
            
            db.session.add(day)
            db.session.add(hour)
            db.session.commit()
            
        else:
            hour = Hours.query.filter_by(hour_number=time_h, day_id=day.id).first()
            
            if hour is None:
                hour = Hours(hour_number=time_h, buy_price=buy, sell_price=sell, belong_day=time_d, hour=day)
                
                db.session.add(hour)
                db.session.commit()

    # Enough information to compute day statistics
    if(day.priceDay() is not -1):
        day.getStats()
        
        predict = Predictions(id=1, right=False, buy_prediction=False, sell_prediction=False)
        print predict.id
        predict.predict()
        
    if(month.priceMonth() is not -1):
        month.getStats()
        
        Predictions(right=False, buy_prediction=False, sell_prediction=False).predict()
Example #22
0
def day(m, d):
    y, m = [int(x) for x in m.split("-")]
    day = Day.get_day(y, m, d)
    if request.path.endswith(".json"):
        return jsonify(day.as_dict())
    return render_template("day.html", day=day)
Example #23
0
def day_pay(y, m, d):
    day = Day.get_day(y, m, d)
    day.do_pay()
    return str(day.bet_count)
Example #24
0
from models import Day, Week
import stats as stats
import locale
import slack

#locale.setlocale(locale.LC_ALL, 'sv_SE')

today = Day.today()
week = Week.from_day(today)
weekdays = week.weekdays()

print(weekdays[0], weekdays[-1])
washes = stats.collect_washes(weekdays[0], weekdays[-1])

message = '{} bokningar för vecka {}:\n'.format(len(washes), week._number)
for day in weekdays:
    day_nbr = len([w for w in washes if w.date==day.date()])
    message += '{} {}'.format(day.short_name(), day_nbr)
    separator = ', ' if day.next().is_weekday() else ''
    message += separator   

slack.post(message)
Example #25
0
def days(last_name):
    user = User.query.filter_by(last_name=last_name).first()
    if user:
        return jsonify(data=[i.serialize for i in Day.days_by_user(user.last_name)])

    return Response(status='400')
Example #26
0
def get_todays_washes():
    today = Day.today()
    return get_washes_for(today)
Example #27
0
def creation(date, buy, sell):
    date = strptime(date, '%m/%d/%y %H:%M')
    print("%r" % date)

    time_h = date.tm_hour
    time_d = date.tm_mday
    time_m = date.tm_mon
    complete_month = 0
    complete_day = 0

    monthDict = {
        1: 'Jan',
        2: 'Feb',
        3: 'Mar',
        4: 'Apr',
        5: 'May',
        6: 'Jun',
        7: 'Jul',
        8: 'Aug',
        9: 'Sep',
        10: 'Oct',
        11: 'Nov',
        12: 'Dec'
    }

    month = Aggregate.query.filter_by(month_number=time_m).first()
    day = Day.query.filter_by(month_number=time_m).first()

    if month is None:
        # Create Month, Day, Hour
        month = Aggregate(month=monthDict[time_m],
                          month_number=time_m,
                          average_buy=-1,
                          average_sell=-1)
        day = Day(month=monthDict[time_m],
                  month_number=time_m,
                  average_buy=-1,
                  average_sell=-1,
                  parent_month=month,
                  day_number=time_d)
        hour = Hours(hour_number=time_h,
                     buy_price=buy,
                     sell_price=sell,
                     hour=day)

        db.session.add(month)
        db.session.add(day)
        db.session.add(hour)
        db.session.commit()

        complete_month = buyMonth(month)
        sellMonth(month)

    else:
        day = Day.query.filter_by(day_number=time_d, month_id=month.id).first()

        if day is None:
            day = Day(month=monthDict[time_m],
                      month_number=time_m,
                      average_buy=-1,
                      average_sell=-1,
                      parent_month=month,
                      day_number=time_d)
            hour = Hours(hour_number=time_h,
                         buy_price=buy,
                         sell_price=sell,
                         day_id=day.id)

            db.session.add(day)
            db.session.add(hour)
            db.session.commit()

            complete_day = buyDay(day)
            sellDay(day)

        else:
            hour = Hours.query.filter_by(hour_number=time_h,
                                         day_id=day.id).first()

            if hour is None:
                hour = Hours(hour_number=time_h,
                             buy_price=buy,
                             sell_price=sell,
                             day_id=day.id)

                db.session.add(hour)
                db.session.commit()
Example #28
0
time_h = date.hour
time_d = date.day
time_m = date.month

month = Aggregate.query.filter_by(month_number=time_m).first()

if month is None:
    # Create Month, Day, Hour
    month = Aggregate(month=date.strftime("%B"),
                      month_number=time_m,
                      average_buy=-1,
                      average_sell=-1)
    day = Day(month=date.strftime("%B"),
              month_number=time_m,
              average_buy=-1,
              average_sell=-1,
              parent_month=month,
              day_number=time_d)
    hour = Hours(hour_number=time_h, buy_price=-1, sell_price=-1, hour=day)

    db.session.add(month)
    db.session.add(day)
    db.session.add(hour)
    db.session.commit()

    buyHour(hour)
    sellHour(hour)

elif month:
    day = Day.query.filter_by(day_number=time_d, month_id=month.id).first()
Example #29
0
def displayWeek():
    try:
        choosenDate = datetime.strptime(request.args.get('date', ''),
                                        "%Y-%m-%d").date()
    except:
        choosenDate = date.today()
    monday = getMonday(choosenDate)
    try:
        message = request.args.get('message', '')
    except KeyError:
        message = ''
    year = monday.strftime("%Y")
    monthNames = [
        "Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August",
        "September", "Oktober", "November", "Dezember"
    ]
    monthNumbers = [
        "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"
    ]
    month = monthNames[monday.month - 1]
    day = monday.day
    week_number = choosenDate.isocalendar()[1]
    week = [(monday + timedelta(days=0)).day, (monday + timedelta(days=1)).day,
            (monday + timedelta(days=2)).day, (monday + timedelta(days=3)).day,
            (monday + timedelta(days=4)).day, (monday + timedelta(days=5)).day,
            (monday + timedelta(days=6)).day]
    weekdays = {
        (monday + timedelta(days=0)).day: 'Montag',
        (monday + timedelta(days=1)).day: 'Dienstag',
        (monday + timedelta(days=2)).day: 'Mittwoch',
        (monday + timedelta(days=3)).day: 'Donnerstag',
        (monday + timedelta(days=4)).day: 'Freitag',
        (monday + timedelta(days=5)).day: 'Samstag',
        (monday + timedelta(days=6)).day: 'Sonntag'
    }
    dishes = Dishes()
    daysObject = Days()
    # FEATURE: Abrufen des zugewiesenen Gerichts für den Tag
    # In der Schleife werden die dishes
    days = {}
    for day in range(0, 7):
        actualDay = monday + timedelta(days=day)
        dishesperDay = daysObject.list(actualDay)

        if len(dishesperDay) != 0:
            days[actualDay.day] = Day(year, int(actualDay.strftime("%m")),
                                      actualDay.strftime("%d"), dishesperDay,
                                      True)
        else:
            days[actualDay.day] = Day(year, int(actualDay.strftime("%m")),
                                      actualDay.strftime("%d"), dishesperDay,
                                      False)
        if actualDay == datetime.now().date():
            days[actualDay.day].isToday = True

    if message:
        flash(message)

    output = render_template(
        'index.html',
        linkCreateDish=url_for('formCreateDish'),
        linkchooseDish=url_for('formChooseDish'),
        nextWeek=(choosenDate + timedelta(days=7)).strftime("%Y-%m-%d"),
        lastWeek=(choosenDate + timedelta(days=-7)).strftime("%Y-%m-%d"),
        week_number=week_number,
        choosenDatum=monday,
        year=year,
        monthName=month,
        monthNames=monthNames,
        monthNumbers=monthNumbers,
        week=week,
        weekdays=weekdays,
        month=monday.month - 1,
        days=days,
        dishes=dishes)
    return output