Skip to content

Instantly share code, notes, and snippets.

@Granitosaurus
Created May 5, 2017 11:33
Show Gist options
  • Save Granitosaurus/60923cd431c30af99635fd82a31fa715 to your computer and use it in GitHub Desktop.
Save Granitosaurus/60923cd431c30af99635fd82a31fa715 to your computer and use it in GitHub Desktop.
Python script for checking whether a video game on steam supports VR devices.
#!/usr/bin/env python3
from urllib.parse import quote, unquote
# requires click, parsel, requests_futures from pip
# requires python3.6
import click
from requests_futures.sessions import FuturesSession
from parsel import Selector
"""
This is a simple concurrent crawler that checks whether a video game on steam supports VR.
The CLI takes in a file as a first argument which should contain 1 video game name per line, e.g.:
Video Game 1
Video Game 2
It will print out something like:
checking 227 urls
Waddle Home -> HTC Vive
DiRT Rally -> Oculus Rift
Project CARS -> HTC Vive | Oculus Rift
Subnautica -> HTC Vive | Oculus Rift
It will skip things that it can't find silently.
"""
class Crawler:
search_url = 'http://store.steampowered.com/search/?term={}'.format
def check_search(self, session, response):
sel = Selector(response.text)
games = sel.css('div#search_result_container div a')
if not games:
return
vrs = games[0].xpath(".//span[re:test(@title, 'vive|oculus','i')]/@title").extract()
if vrs:
vrs = ' | '.join(vrs)
name = unquote(response.url.split('?term=')[-1])
print(f'{name} -> {vrs}')
def check_names(self, names):
names = list(set([n.strip() for n in names]))
urls = [self.search_url(quote(name)) for name in names]
print(f'checking {len(urls)} urls')
with FuturesSession(max_workers=5) as session:
futures = [session.get(url, timeout=5, background_callback=self.check_search) for url in urls]
for f in futures:
f.result()
@click.command()
@click.argument('file', type=click.File('r'))
def cli(file):
"""
Checks whether games support VR\n
Usage: vr_check <file_of_newline_separated_game_names>
"""
crawler = Crawler()
crawler.check_names(file)
if __name__ == '__main__':
cli()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment