def tagspage(): ptdb = ptimedb.PTimeDb() tmpt = {} for tag in ptdb.list('tag'): ta = ptdb.tagged_as(tag[0]) ttaim = {} for t in ta['game']: for tt in ptdb.list('playtime', what2={ 'game': t, 'aggr': 'monthly' }): if tt[1] in ttaim: ttaim[tt[1]] += tt[0] else: ttaim[tt[1]] = tt[0] #print(ptdb.nameof('game',t), flush=True) ttaim = [(ttaim[x], x) for x in ttaim.keys()] tmpt[tag[0]] = ptdb.monthly_table(ttaim) tags = [] for tag in ptdb.toptags(): tag['mpt'] = tmpt[tag['name']] tags.append(tag) ptdb.db.close() return render_template('ptimetags.html', title='ptime tags', tags=tags)
def ptimeinfopage(): ptdb = ptimedb.PTimeDb() pt = [x[:-1] for x in ptdb.list('playtime')] pt = [(x[6], ptdb.nameof('game', x[1]), ptdb.nameof('platform', x[2]), x[3], x[4], x[5]) for x in pt] ptc = ptdb.list('playtime', what2={'aggr': 'none', 'count': ''}) games = [(x[2], x[0]) for x in ptdb.list('game')] platforms = [(x[2], x[0]) for x in ptdb.list('platform')] tags = [(x[2], x[0]) for x in ptdb.list('tag')] ptdb.db.close() start = datetime.datetime(2017, 1, 1) dbstats = { 'daysgoing': (datetime.datetime.now() - start).days + 1, 'noofentries': ptc[0][0], 'noofgames': len(games), 'noofplatforms': len(platforms), 'nooftags': len(tags), } return render_template('ptimeinfo.html', title='ptime info', pt=pt, games=games, platforms=platforms, tags=tags, dbstats=dbstats)
def impspage(): ptdb = ptimedb.PTimeDb() l7 = ptdb.getCachedData('alltime-impressions', {'content': []}, singl=True) bigrunners = {} for imp in l7['content']: if imp['name'] in bigrunners: bigrunners[imp['name']] += 1 else: bigrunners[imp['name']] = 1 bigrunners = [{'name': x, 'time': bigrunners[x]} for x in bigrunners] bigrunners.sort(key=lambda x: x['time'], reverse=True) bigrunners = [x for x in bigrunners if x['time'] != 1] tops = [ { 'title': 'Big runners', 'list': { 'content': bigrunners } }, { 'title': 'Top ' + str(len(l7['content'])) + ' impressions of all time (well, 2017-)', 'list': l7 }, ] ptdb.db.close() return render_template('ptime.html', title='impressions', tops=tops)
def ptimebalancepage(): ptdb = ptimedb.PTimeDb() bal = ptdb.balance() ptdb.db.close() return render_template( 'ptimebalance.html', title='ptime balance', bal=bal, )
def ptime_formadd(msg=[]): ptdb = ptimedb.PTimeDb() form = PTForm() form.platform.choices = [(x[2], x[0]) for x in ptdb.list('platform')] form.platform.choices.insert(0, (0, '---')) ptdb.db.close() if form.validate_on_submit(): d = form.date.data.strftime('%d/%m/%y') cd = form.confirm_date.data.strftime('%d/%m/%y') if d == cd: ptdb = ptimedb.PTimeDb() m = ptdb.addPTime(form.game.data, form.platform.data, d, d, form.ptime.data) if 'duna' in m: msg = """<h2>""" + m['duna'] + """</h2>""" for pt in [x for x in ptdb.list('playtime')][:10]: msg += '<p class="larger">' + pt[3][:10] + ' - ' + str( pt[-2]) + ' added for ' msg += ptdb.nameof('game', pt[1]) + ' (' + ptdb.nameof( 'platform', pt[2]) + ')</p>' msg += """<h1><a href="/ptime/add">+</a></h1>""" msg += """<br><br><h1><a href="/ptime/balance">BAL</a></h1>""" return msg else: msg = [ m['error'], ] ptdb.db.close() else: msg = [ 'Dates don\'t seem to match.', ] else: form.date.data = datetime.datetime.now() form.confirm_date.data = datetime.datetime.now() return render_template( 'ptform.html', title='add PTime', msg=msg, form=form, )
def no1spage(): ptdb = ptimedb.PTimeDb() def m(m, l, tot=True): if len(l) > m: r = l[:m] else: r = l[:-1] if tot: r += l[-1:] return r no1data = {'games': {}, 'platforms': {}} for what in no1data.keys(): for when in ['monthly', 'yearly']: no1s = [] bigrunners = {} yms = _ym_up_til_now( )[::-1] if when == 'monthly' else _y_up_til_now()[::-1] for ym in yms: curr = ptdb.getCachedData(when + '-' + what + '-' + ym, [], True) if curr: curr = m(1, curr['content']) if len(curr) > 1: no1s.append({ 'ym': ym, 'name': curr[0]['name'], 'time': curr[0]['time'], 'perc': curr[0]['perc'], }) if curr[0]['name'] in bigrunners: bigrunners[curr[0]['name']] += 1 else: bigrunners[curr[0]['name']] = 1 else: no1s.append({ 'ym': ym, 'name': 'n/a', }) bigrunners = [(x, bigrunners[x]) for x in bigrunners] bigrunners.sort(key=lambda x: x[1], reverse=True) bigrunners = [x for x in bigrunners if x[1] != 1] no1data[what][when] = {'no1s': no1s, 'bigrunners': bigrunners} ptdb.db.close() return render_template('ptimeno1s.html', title='ptime number 1s', no1data=no1data)
def stime_formadd(msg=[]): form = STForm() if form.validate_on_submit(): ptdb = ptimedb.PTimeDb() m = ptdb.addSTime(form.sdate.data.strftime('%d/%m/%y'), form.stime.data) ptdb.db.close() if 'duna' in m: msg = """<p class="larger">""" + m[ 'duna'] + """</p><br><a href="/ptime/add">+</a>""" return msg else: msg = [ m['error'], ] return render_template('stform.html', title='add STime', msg=msg, form=form)
def ptime_jsonadd(): ptdb = ptimedb.PTimeDb() content = request.json if 'davidsstuff' in content: if content['davidsstuff'] == 'game' or content[ 'davidsstuff'] == 'platform' or content['davidsstuff'] == 'tag': if 'toadd' in content and 'name' in content['toadd']: r = json.dumps( ptdb.add(content['davidsstuff'], content['toadd']['name'])) else: r = 'What is this stuff?' elif content['davidsstuff'] == 'tagging': if 'totag' in content and 'what' in content[ 'totag'] and 'name' in content[ 'totag'] and 'tag' in content['totag']: r = json.dumps( ptdb.tag(content['totag']['what'], content['totag']['name'], content['totag']['tag'])) else: r = 'What is this stuff?' elif content['davidsstuff'] == 'ptime': try: ta = content['toadd'] r = json.dumps( ptdb.addPTime(ta['game'], ta['platform'], ta['start'], ta['end'], ta['ptime'])) except: r = 'What is this stuff?' elif content['davidsstuff'] == 'rules': if 'rules' in content: r = json.dumps( ptdb.setRules(content['rules']['ym'], content['rules'], web=True)) else: r = 'What is this stuff?' else: #print('baka') r = 'What is this stuff?' ptdb.db.close() return r
def platspage(): ptdb = ptimedb.PTimeDb() platsmonths = {} pmpts = {} strongestmonth = [] for platform in ptdb.list('platform'): platmonths = ptdb.list('playtime', what2={ 'platform': platform[0], 'aggr': 'monthly' }) pmpt = ptdb.monthly_table(platmonths) pmpts[platform[0]] = pmpt platmonths.sort(key=lambda x: x[0], reverse=True) strongestmonth.append( (platform[0], platmonths[0][0], platmonths[0][1])) platmonths = [(str(int(x[0] / 60)) + 'h ' + str(x[0] % 60) + 'm', x[1]) for x in platmonths[:5]] platsmonths[platform[0]] = platmonths strongestmonth.sort(key=lambda x: x[1], reverse=True) strongestmonth = [(n + 1, x[0], str(int(x[1] / 60)) + 'h ' + str(x[1] % 60) + 'm', x[2]) for n, x in enumerate(strongestmonth)] alltimeplats = ptdb.getCachedData('alltime-platforms', [], singl=True) platsmonths = [{ 'plat': x['name'], 'data': platsmonths[x['name']], 'mpt': pmpts[x['name']] } for x in alltimeplats['content']] ptdb.db.close() return render_template('ptimeplats.html', title='ptime platforms', strongestmonth=strongestmonth, alltimeplats=alltimeplats, platsmonths=platsmonths)
def ptimesinglpage(msg=[]): ptdb = ptimedb.PTimeDb() form = PTsForm() form.game.choices = [(x[2], x[0]) for x in ptdb.list('game')] form.game.choices.insert(0, (0, '---')) ptdb.db.close() tot = [] mpt, mec = '', '' pt = [] stats = {} if form.validate_on_submit(): gamename = form.game.data ptdb = ptimedb.PTimeDb() mtaim = ptdb.list('playtime', what2={ 'game': form.game.data, 'aggr': 'monthly' }) mpt = ptdb.monthly_table(mtaim) mecount = ptdb.list('playtime', what2={ 'game': form.game.data, 'aggr': 'monthly', 'count': '' }) mec = ptdb.monthly_table(mecount, timedata=False) taim = ptdb.list('playtime', what2={'game': form.game.data}) taim.sort(key=lambda x: x[4], reverse=True) pt = [x[:-1] for x in taim][:50] tot = ptdb.top(what='platform', what2={ 'game': form.game.data, 'gameperplatform': '' }) stats = {} alltimer = [ x for x in ptdb.getCachedData('alltime-games', [], singl=True) ['content'] if gamename in x['name'] ] if alltimer: stats['alltimerank'] = alltimer[0]['rank'] topimpressions = [ x for x in ptdb.getCachedData('alltime-impressions', [], singl=True)['content'] if gamename in x['name'] ] if topimpressions: stats['topimpressions'] = topimpressions ptdb.db.close() return render_template('ptsingl.html', title='PTime singl', msg=msg, form=form, tot=tot, mpt=mpt, mec=mec, stats=stats, pt=pt)
def ptimepage(): ptdb = ptimedb.PTimeDb() form = PTarchiveForm() form.ym.choices = [(x, x) for x in _ym_up_til_now()][::-1] def m(m, l, tot=True): if len(l) > m: r = l[:m] else: r = l[:-1] if tot: r += l[-1:] return r if form.validate_on_submit(): ym = form.ym.data getfromcache = request.form['btn'] == 'OK' else: ym = form.ym.choices[0][0] getfromcache = True curryear = ym[:-3] curryearmonth = calendar.month_name[int(ym[-2:])] + ' ' + curryear if getfromcache: l2 = ptdb.getCachedData('monthly-games-' + ym, {'content': []}, singl=True) l4 = ptdb.getCachedData('monthly-platforms-' + ym, {'content': []}, singl=True) l1 = ptdb.getCachedData('yearly-games-' + curryear, {'content': []}, singl=True) l3 = ptdb.getCachedData('yearly-platforms-' + curryear, {'content': []}, singl=True) l5 = ptdb.getCachedData('alltime-games', {'content': []}, singl=True) l6 = ptdb.getCachedData('alltime-platforms', {'content': []}, singl=True) else: l2 = ptdb.top(what2={'ym': ym, 'gameperplatform': ''}) l2 = ptdb.setCachedData('monthly-games-' + ym, l2) l4 = ptdb.top(what='platform', what2={'ym': form.ym.data})[:-1] l4 = ptdb.setCachedData('monthly-platforms-' + ym, l4) l1 = ptdb.top(what2={'y': curryear, 'gameperplatform': ''}) l1 = ptdb.setCachedData('yearly-games-' + curryear, l1) l3 = ptdb.top(what='platform', what2={'y': curryear}) l3 = ptdb.setCachedData('yearly-platforms-' + curryear, l3) l5 = m(50, ptdb.top(what2={})) l5 = ptdb.setCachedData('alltime-games', l5) l6 = m(20, ptdb.top(what='platform', what2={}), False) l6 = ptdb.setCachedData('alltime-platforms', l6) l7 = m(100, ptdb.top(what2={'impressions': ''}), False) l7 = ptdb.setCachedData('alltime-impressions', l7) l1['content'] = m(20, l1['content']) l3['content'] = m(10, l3['content'], False) mtaim = ptdb.list('playtime', what2={'aggr': 'monthly'}) mpt = ptdb.monthly_table(mtaim) mecount = ptdb.list('playtime', what2={'aggr': 'monthly', 'count': ''}) mec = ptdb.monthly_table(mecount, timedata=False) mtcount = ptdb.list('playtime', what2={ 'aggr': 'monthly', 'count': 'titles' }) mtc = ptdb.monthly_table(mtcount, timedata=False, nototal=True) tops = [ { 'title': str(len(l2['content']) - 1) + ' games in ' + curryearmonth, 'list': l2 }, { 'title': str(len(l4['content'])) + ' platforms in ' + curryearmonth, 'list': l4 }, { 'title': 'Top ' + str(len(l1['content']) - 1) + ' games in ' + curryear, 'list': l1 }, { 'title': 'Top ' + str(len(l3['content'])) + ' platforms in ' + curryear, 'list': l3 }, { 'title': 'Top ' + str(len(l5['content']) - 1) + ' games of all time (well, 2017-)', 'list': l5 }, { 'title': 'Top ' + str(len(l6['content'])) + ' platforms of all time (well, 2017-)', 'list': l6 }, ] ptdb.db.close() return render_template('ptime.html', title='ptime', form=form, mpt=mpt, mec=mec, mtc=mtc, tops=tops)
def zongpage(): z = 'CDEFGAB' zz = ['left', 'right'] zzz = '12345' zongo = z[random.randint(0, 6)] zongoo = zz[random.randint(0, 1)] + ' ' + zzz[random.randint(0, 4)] return render_template('zong.html', zongo=zongo, zongoo=zongoo) # PPP TTTTT I M M EEEE # P P T I MM MM E # PPP T I M M M EEE # P T I M M E # P T I M M EEEE ptdb = ptimedb.PTimeDb() GAMES = [x[0] for x in ptdb.list('game')] ptdb.db.close() @app.route("/autocomplete", methods=['GET']) def autocomplete(): search = request.args.get('term') return Response(json.dumps(GAMES), mimetype='application/json') def _ym_up_til_now(): yms = [ '2017-01', ] while yms[-1] != datetime.datetime.now().strftime('%Y-%m'):