def updateMetascore(request):

    # get items
    items = Items.query().fetch()

    # iterate items
    for item in items:

        # if metacriticPage is valid
        if (item.item_metacriticPage != None and item.item_metacriticPage != ''):

            # fetch page
            url = METACRITIC_BASE_URL + item.item_metacriticPage

            logging.info(url)

            # allow 30 seconds for response
            response = urlfetch.fetch(url, None, 'GET', {}, False, False, 30)

            if response.status_code == 200:

                # parse metacritic page response - return score
                metascore = getMetacriticScore(response.content)

                logging.info('------ METASCORE -------')
                logging.info('-------------------')
                logging.info(metascore)
                logging.info('-------------------')

                # upate item record
                item.item_metascore = metascore
                item.put()

    return HttpResponse('success', mimetype='text/plain', status='200')
def updateSteamPriceDeferred():

    # get items
    items = Items.query().fetch()

    # iterate items
    for item in items:

        # steam price URL found
        if (item.item_steamPriceURL != None and item.item_steamPriceURL.strip() != ''):

            # fetch page
            url = item.item_steamPriceURL

            # headers required for response
            headers = {'Cookie': 'Steam_Language=english; birthtime=-2208959999;', 'User-Agent': 'Mozilla', 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', 'Accept': 'text/html', 'Accept-Encoding': 'gzip'}

            # fetch(url, payload=None, method=GET, headers={}, allow_truncated=False, follow_redirects=True, deadline=None, validate_certificate=None)
            # allow 30 seconds for response
            response = urlfetch.fetch(url, None, 'GET', headers, False, False, 30)

            # decompress
            f = StringIO.StringIO(response.content)
            c = gzip.GzipFile(fileobj=f)
            content = c.read()

            if response.status_code == 200:

                # parse metacritic page response - return score
                steamPrice = getSteamPrice(content)

                if (steamPrice != None):

                    logging.info('------ UPDATE STEAM PRICE v2 -------')
                    logging.info(item.item_name)
                    logging.info(steamPrice)

                    # upate item record
                    item.item_steamPrice = steamPrice
                    item.put()

    return HttpResponse('success', mimetype='text/plain', status='200')
def updateSteamPageDeferred():
    # get items
    items = Items.query().fetch()

    # iterate items
    for item in items:

        # steam price URL not found
        if (item.item_steamPriceURL == None or item.item_steamPriceURL.strip() == ''):

            # search steam
            searchList = Steam.searchSteam(item.item_name)

            # add all names to array and item names to dictionary by name
            nameList = []
            searchDictionary = {}

            for steamItem in searchList:
                nameList.append(steamItem['name'])
                searchDictionary[steamItem['name']] = steamItem

            # get closest matches
            closestMatches = difflib.get_close_matches(item.item_name, nameList, 1)

            # match found
            if (len(closestMatches) != 0):
                firstMatch = closestMatches[0]

                logging.info('------ UPDATE STEAM PAGE v2 -------')
                logging.info(item.item_name)
                logging.info(searchDictionary[firstMatch]['page'])

                # update steam price info
                item.item_steamPriceURL = searchDictionary[firstMatch]['page']
                item.item_steamPrice = searchDictionary[firstMatch]['price']
                item.put()

    return HttpResponse('success', mimetype='text/plain', status='200')
def updateImageSize(request):

    items = Items.query(Items.item_initialProvider == '1').fetch()

    itemDict = []

    if items:

        for item in items:

            arr = item.item_largeImage.split('/')
            arr[0] = 'small'
            smallString = '/'.join(arr)

            item.item_largeImage = smallString

            item.put()

            itemDict.append(item.item_largeImage)

        return HttpResponse(json.dumps({'items': itemDict}), mimetype='application/json')

    return HttpResponse(json.dumps({'status': 'empty'}), mimetype='application/json')