Last active
January 10, 2021 19:40
-
-
Save turnipsoup/daff1583a249c0f979c79e7026f9d0b8 to your computer and use it in GitHub Desktop.
Get current prices for games from pricecharting.com and return updated CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# This script will read my games CSV list and update the current | |
# price of the games in the sheet | |
import pandas as pd | |
import requests | |
import bs4 as bs | |
from tabulate import tabulate | |
# Open CSV file | |
df = pd.read_excel("/Users/jeremy/Documents/spreadsheets/gametrack.xlsx", sheet_name='Games') | |
df = df.iloc[:, :8] | |
# Get game prices function | |
def gameval(game_link, status): | |
currprice = 0 | |
# Get page and build soup object | |
r = requests.get(game_link) | |
soup = bs.BeautifulSoup(r.content, 'lxml') | |
# We need to support multiple game statuses: | |
if status == "Loose": | |
currprice = soup.find("td", {"id": "used_price"}).find("span", {"class": "price"}).text.strip().replace("$","") | |
elif status == "Complete": | |
currprice = soup.find("td", {"id": "complete_price"}).find("span", {"class": "price"}).text.strip().replace("$","") | |
elif status == "New": | |
currprice = soup.find("td", {"id": "new_price"}).find("span", {"class": "price"}).text.strip().replace("$","") | |
return currprice | |
def pricediff(current_price, new_price): | |
diff_price = float(current_price) - float(new_price) | |
if diff_price != 0: | |
return True | |
return False | |
# Iterate over all games in the file | |
def main(): | |
changed_prices = [] | |
# Create our new CSV file | |
with open("./games_new.csv", "w") as f: | |
f.write("Title,Platform,Localization,Condition,Status,PaidPrice,CurrentEvalPrice,PC_Link\n") | |
f.close() | |
for game in range(len(df)): | |
game_data = df.iloc[game] | |
title = game_data['Title'] | |
platform = game_data['Platform'] | |
condition = game_data['Condition'] | |
status = game_data['Status'] | |
localization = str(game_data['Localization']) | |
paidprice = game_data['PaidPrice'] | |
currentevalprice = game_data['CurrentEvalPrice'] | |
pc_link = game_data['PC_Link'] | |
newcurrentevalprice = gameval(pc_link, status) | |
pricedelta = float(newcurrentevalprice) - float(currentevalprice) | |
# Log to screen to keep me happy: | |
print(f"Fetched game price info for {title} from {pc_link} - old price: {currentevalprice}, new price: {newcurrentevalprice}") | |
with open("./games_new.csv", "a") as f: | |
f.write(f"{title},{platform},{localization},{condition},{status},{paidprice},{newcurrentevalprice},{pc_link}\n") | |
f.close() | |
# If the price is different, add to changed_prices list | |
if pricediff(currentevalprice, newcurrentevalprice): | |
changed_prices.append([title, currentevalprice, newcurrentevalprice, pricedelta]) | |
# Lets easily see what games changes prices | |
print() | |
print("These games changes prices since last run:") | |
print(tabulate(changed_prices, headers=["Title", "Old Price", "New Price", "Price Change"])) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment