#!/usr/bin/python3

import requests
import json
from sqlalchemy import create_engine, ForeignKey
from sqlalchemy import Column, Date, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.exc import IntegrityError

#Globals
engine = create_engine('sqlite:///games.db')
Base = declarative_base()
API_URL = "http://store.steampowered.com/api/appdetails/"
LIMIT = 250

class Game(Base):
    __tablename__ = 'games'
    id = Column(Integer, primary_key=True)
    init_price = Column(Integer)
    final_price = Column(Integer)

    def __repr__(self):
        return "<Game(id='%s', initial_price='%s', final_price='%s')>" % (self.id, self.init_price, self.final_price)

def build_list():
    game_list = dict()
    URL = "http://api.steampowered.com/ISteamApps/GetAppList/v2"
    response = requests.get(URL)
    game_list = json.loads(response.text)
    return game_list

def fetchdump(appids):
    if len(appids) > LIMIT:
        print("Error: Do not submit more than %d items per query!" % LIMIT)
        return
    #TODO: Make sure we were fed a list of strings, not list of ints
    Session = sessionmaker(bind=engine)
    session = Session()
    params = {
        "appids": "," . join(appids),
        "filters": "price_overview"
    }
    response = requests.get(API_URL, params=params)
    data = json.loads(response.text)
    for game in data:
        if data[game]["success"] is True:
            if data[game]["data"]:
                init_price = data[game]["data"]["price_overview"]["initial"]
                final_price = data[game]["data"]["price_overview"]["final"]
                game_obj = Game(id=game, init_price=init_price, final_price=final_price)
                session.merge(game_obj)
        else:
            print("ID %s is false" % game)
    try:
        session.commit()
    except IntegrityError as err:
        print("Error updating DB! %s" % err)

def main():
    master_list = build_list()
    appids = []
    for app in master_list["applist"]["apps"][:LIMIT]:
        appids.append(str(app["appid"]))
    Base.metadata.create_all(engine)
    fetchdump(appids)

if __name__ == "__main__":
    main()

@@ -522,16 +522,42 @@
 y=True)%0A
+    name = Column(String)%0A
     init
@@ -581,16 +581,16 @@
 nteger)%0A
-
     fina
@@ -670,16 +670,27 @@
 id='%25s',
+ name='%25s',
  initial
@@ -733,16 +733,27 @@
 self.id,
+ self.name,
  self.in
@@ -1004,16 +1004,25 @@
 p(appids
+, mapping
 ):%0A    i
@@ -1700,24 +1700,66 @@
 %22%5D%5B%22final%22%5D%0A
+                name = mapping%5Bint(game)%5D%0A
             
@@ -1786,16 +1786,27 @@
 id=game,
+ name=name,
  init_pr
@@ -2116,16 +2116,33 @@
 ds = %5B%5D%0A
+    mapping = %7B%7D%0A
     for 
@@ -2229,16 +2229,148 @@
 pid%22%5D))%0A
+        if app%5B%22name%22%5D:%0A            mapping%5Bapp%5B%22appid%22%5D%5D = app%5B%22name%22%5D%0A        else:%0A            mapping%5Bapp%5B%22appid%22%5D%5D = %22unknown%22%0A
     Base
@@ -2394,24 +2394,24 @@
 all(engine)%0A
-
     fetchdum
@@ -2418,16 +2418,25 @@
 p(appids
+, mapping
 )%0A%0Aif __

Update fetcher to include name