Skip to content

Instantly share code, notes, and snippets.

@trK54Ylmz
Last active October 29, 2022 05:33
Show Gist options
  • Save trK54Ylmz/5591143904de4b756a23da6354a4b898 to your computer and use it in GitHub Desktop.
Save trK54Ylmz/5591143904de4b756a23da6354a4b898 to your computer and use it in GitHub Desktop.
Interactive game listing from Metacritics by user score
import streamlit as st
import pandas
import requests
from bs4 import BeautifulSoup
from datetime import datetime, timedelta
from IPython.core.display import display, HTML
def main():
st.sidebar.title('Select page')
st.sidebar.number_input('Page', step=1, min_value=0, on_change=load, key='number')
def image(path):
return f'<img src="{path}" width="120" >'
def load():
url = 'https://www.metacritic.com/browse/games/score/userscore/all/pc/filtered'
query = {
'sort': 'desc',
'page': st.session_state.number or 0
}
headers = {
'authority': 'www.metacritic.com',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9,tr;q=0.8,vi;q=0.7,de;q=0.6,ru;q=0.5',
'cache-control': 'no-cache',
'cookie': 'mc_s_s=d_1; AMCVS_3C66570E5FE1A4AB0A495FFC%40AdobeOrg=1; s_cc=true; mc_ag=1; ctk=NjM0MTc1OWE1OGU2YWIzMGE3ODQxYzNhMjc4OQ%3D%3D; tmpid=1667017030218164; dw-tag=content_header_wrapper%3Bmain_content; prevPageType=list_page; s_sq=%5B%5BB%5D%5D; OptanonConsent=isIABGlobal=false&datestamp=Sat+Oct+29+2022+07%3A34%3A40+GMT%2B0300+(GMT%2B03%3A00)&version=6.20.0&hosts=&consentId=a09860bf-db3b-4a30-a0a0-6e39ccffa8c8&interactionCount=1&landingPath=NotLandingPage&groups=C0001%3A1%2CC0002%3A1%2CC0003%3A1%2CC0004%3A0&AwaitingReconsent=false; AMCV_3C66570E5FE1A4AB0A495FFC%40AdobeOrg=1585540135%7CMCMID%7C36401659451030133907163307877303367266%7CMCOPTOUT-1667025280s%7CNONE%7CvVersion%7C4.4.0; metapv=71; utag_main=v_id:0183b7b032520019bc6df6a1c0c805075001b06d00bd0$_sn:5$_ss:0$_st:1667019881830$vapi_domain:metacritic.com$_pn:71%3Bexp-session$ses_id:1667016422075%3Bexp-session',
'pragma': 'no-cache',
'referer': 'https://www.metacritic.com/browse/games/score/userscore/all/pc/filtered?sort=desc&page=5',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36',
}
res = requests.get(url, params=query, headers=headers)
soup = BeautifulSoup(res.content, 'lxml')
games = soup.select('table.clamp-list tr')
selected = []
for game in games:
if game.has_attr('class'):
continue
date_text = game.select('div.clamp-details span')[-1].text
date = datetime.strptime(date_text, '%B %d, %Y')
if date < datetime.now() - timedelta(days=6 * 365):
continue
row = {
'image': game.select_one('td.clamp-image-wrap img').get('src'),
'date': date.strftime('%B %d, %Y'),
'name': game.select_one('a.title').text,
'user_score': game.select_one('a.metascore_anchor div').text,
'url': 'https://www.metacritic.com' + game.select_one('a.title').get('href'),
}
selected.append(row)
df = pandas.DataFrame(selected)
html = df.to_html(escape=False, formatters=dict(image=image))
st.markdown(html, unsafe_allow_html=True)
if __name__ == '__main__':
main()
load()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment