源码

函数源码
import requests,json

def get_epic_free_games(show=True):

    api_url = "https://store-site-backend-static.ak.epicgames.com/freeGamesPromotions?locale=en-US&country=US&allowCountries=US,CN"
    epic_free_games = {
        "promotionalOffers":{"remarks":"已推出优惠","data":[]},
        "upcomingPromotionalOffers":{"remarks":"即将推出优惠","data":[]}
    }
    r = requests.get(api_url)
    if r.status_code == 200:
        if show:
            print("EPIC免费游戏列表:\n")
        data = r.json()
        for key in data['data']['Catalog']['searchStore']['elements']:
            if key['promotions']:
                if key['promotions']["promotionalOffers"] != []:
                    # print("已推出优惠:")
                    if key['promotions']["promotionalOffers"][0]['promotionalOffers'][0]['discountSetting']['discountPercentage'] == 0:
                        # print("{}\n{} -> {}\n".format(key["title"],key['promotions']["promotionalOffers"][0]['promotionalOffers'][0]['startDate'],key['promotions']["promotionalOffers"][0]['promotionalOffers'][0]['endDate']))
                        epic_free_games["promotionalOffers"]["data"].append({
                            "title":key["title"],
                            "description":key['description'],
                            "keyImages":key['keyImages'],
                            "url":"https://store.epicgames.com/p/"+key['catalogNs']['mappings'][0]['pageSlug'],
                            "startDate":key['promotions']["promotionalOffers"][0]['promotionalOffers'][0]['startDate'],
                            "endDate":key['promotions']["promotionalOffers"][0]['promotionalOffers'][0]['endDate']
                            })
                        # print(key["title"])
                        # print(key['promotions']["promotionalOffers"][0]['promotionalOffers'][0]['startDate'])
                        # print(key['promotions']["promotionalOffers"][0]['promotionalOffers'][0]['endDate'])
                else:
                    # print("即将推出优惠:")
                    if key['promotions']["upcomingPromotionalOffers"][0]['promotionalOffers'][0]['discountSetting']['discountPercentage'] == 0:
                        # print("{}\n{} -> {}\n".format(key["title"],key['promotions']["upcomingPromotionalOffers"][0]['promotionalOffers'][0]['startDate'],key['promotions']["upcomingPromotionalOffers"][0]['promotionalOffers'][0]['endDate']))
                        epic_free_games['upcomingPromotionalOffers']["data"].append({
                            "title":key["title"],
                            "description":key['description'],
                            "keyImages":key['keyImages'],
                            "url":"https://store.epicgames.com/p/"+key['catalogNs']['mappings'][0]['pageSlug'],
                            "startDate":key['promotions']["upcomingPromotionalOffers"][0]['promotionalOffers'][0]['startDate'],
                            "endDate":key['promotions']["upcomingPromotionalOffers"][0]['promotionalOffers'][0]['endDate']
                            })
                        # print(key["title"])
                        # print(key['promotions']["upcomingPromotionalOffers"][0]['promotionalOffers'][0]['startDate'])
                        # print(key['promotions']["upcomingPromotionalOffers"][0]['promotionalOffers'][0]['endDate'])
        if show:
            print("已推出优惠:")
            for promotionalOffers in epic_free_games['promotionalOffers']["data"]:
                print("{}\n{} -> {}\n{}\n".format(promotionalOffers["title"],promotionalOffers['startDate'],promotionalOffers['endDate'],promotionalOffers['url']))
            print("即将推出优惠:")
            for upcomingPromotionalOffers in epic_free_games['upcomingPromotionalOffers']["data"]:
                print("{}\n{} -> {}\n{}\n".format(upcomingPromotionalOffers["title"],upcomingPromotionalOffers['startDate'],upcomingPromotionalOffers['endDate'],upcomingPromotionalOffers['url']))
    else:
        if show:
            print("EPIC免费游戏列表获取失败")
    
    return epic_free_games

get_epic_free_games()
最后修改:2024 年 05 月 22 日
如果觉得我的文章对你有用,请随意赞赏