2
\$\begingroup\$

so I am playing with Rawg.io 's API. U can sort their game list by their release date, however there are just too many games. In their website, there's an option called "Popular only", which only shows games that are being followed by at least 1 people.

I implemented this feature by sorting the elements I get and creating a sorted list. I wanted to make sure that I would also acces their information in a systematically way ([game's number], [name or other topic]).

(the ##delete## parts are parts that are just for console visuals, I am planning to move this project to a Kivy app, so won't need them later)

I really want to know what I could have done differently. What's obnoxious, stupid, ... could be better?

import requests

######Delete#########
import os

os.system("cls")
###################


#### Functions
def _Url(year1, month1, day1, year2, month2, day2, PageNumber=1):
    if month1 < 10:
        month1 = "0" + str(month1)
    if day1 < 10:
        day1 = "0" + str(day1)
    if month2 < 10:
        month2 = "0" + str(month2)
    if day2 < 10:
        day2 = "0" + str(day2)

    Url = (
        "https://api.rawg.io/api/games?dates="
        + str(year1)
        + "-"
        + str(month1)
        + "-"
        + str(day1)
        + ","
        + str(year2)
        + "-"
        + str(month2)
        + "-"
        + str(day2)
        + "&page="
        + str(PageNumber)
        + "&platforms=4,187,1,18,186&page_size=40&ordering=released"
    )
    return Url


def _GetData_Date(year1=2020, month1=8, day1=4, year2=2020, month2=8, day2=4): #just place holder values

    Url = _Url(year1, month1, day1, year2, month2, day2)
    StaticData = requests.get(Url).json()
    AmountOfGames = StaticData["count"]
    AmountOfPages = AmountOfGames // 40 #1 page contains 40 so it does amount//40 to get pages
    if(AmountOfGames%40 != 0):
        AmountOfPages = AmountOfPages + 1 #does +1 if there's a leftover page
    PageNumber = 1

#############Delete############
    Succes = 0
    Fail = 0
#########################

    while PageNumber <= AmountOfPages:

        for x in range(len(StaticData["results"])):
            if StaticData["results"][x]["added"] > 0:
                try:   #can't start with an empty Game variable
                    AStaticData = (StaticData["results"][x],)  # the ","turns it to tulpe, it keeps adding tulpes to 1 giant one that contains all the filtered information
                    Game = Game + AStaticData
                except:
                    AStaticData = (StaticData["results"][x],)  # the ","turns it to tulpe
                    Game = AStaticData
############################Delete#################
                os.system("cls")
                Succes = Succes + 1
                print("succes: ", (Succes))
                print("fail:", Fail)
                print("Total:", AmountOfGames, Succes + Fail)
            else:
                os.system("cls")
                Fail = Fail + 1
                print("succes: ", (Succes))
                print("fail:", Fail)
                print("Total:", AmountOfGames,"-", Succes + Fail)
##################################################
        # goes trough pages
        PageNumber = PageNumber + 1
        if PageNumber <= AmountOfPages:
            Url = _Url(year1, month1, day1, year2, month2, day2, PageNumber)
            StaticData = requests.get(Url).json()

    return Game


#### Save it to local variables #####
StaticData =_GetData_Date() 

### Main program ###################
for x in range(len(StaticData)):
    print(StaticData[x]["name"])
\$\endgroup\$
2
  • \$\begingroup\$ Why not use the API wrapper? pypi.org/project/rawgpy \$\endgroup\$
    – hjpotter92
    Commented Aug 12, 2020 at 9:26
  • \$\begingroup\$ I have more control by doing everything myself, I think. Doing it myself felt easier than understanding the full documentation of the wrapper. \$\endgroup\$
    – user182461
    Commented Aug 12, 2020 at 9:30

1 Answer 1

2
\$\begingroup\$

f-strings

I recommend to use f-strings (Python version >= 3.6) for string formatting. See this link for more details: https://www.python.org/dev/peps/pep-0498/

When you use them you can replace

if month1 < 10:
    month1 = "0" + str(month1)

with an expression like this:

month1 = f'{month1:02}'

This adds a leading zero if month1 only has one digit.

In addition, an expression like:

 Url = (
        "https://api.rawg.io/api/games?dates="
        + str(year1)
        + "-"
        + str(month1)
        + "-"
        + str(day1)
        + ","
        + str(year2)
        + "-"
        + str(month2)
        + "-"
        + str(day2)
        + "&page="
        + str(PageNumber)
        + "&platforms=4,187,1,18,186&page_size=40&ordering=released"
    )

can be written as

url_part1 = "https://api.rawg.io/api/games?dates="
url_part2 = "&platforms=4,187,1,18,186&page_size=40&ordering=released"

 Url = f"{url_part1}{year1}-{month1}-{day1},{year2}-{month2}-{day2}&page={PageNumber}{url_part2}"

using f-strings. This is shorter and less error prone (less quotes you can forget), opinions might vary regarding the readability, though. Type conversions (str(.)) are not needed anymore.

Avoid hardcoding

It is good that you added a comment to explain where the number 40 comes from:

AmountOfPages = AmountOfGames // 40 #1 page contains 40 so it does amount//40 to get pages

However, it would be even better if you assigned this number to a variable with a telling name. For example

games_per_page = 40 # Preferably, put this on the top of your script or in a config file
# [...]
AmountOfPages = AmountOfGames // games_per_page

This is more flexible. Imagine the number of games per page changes.

Leftover page: ceiling division

In this part you first do a floor division (//), then you add 1 for leftover pages

AmountOfPages = AmountOfGames // games_per_page#1 page contains 40 so it does amount//40 to get pages
if(AmountOfGames%games_per_page != 0):
    AmountOfPages = AmountOfPages + 1 #does +1 if there's a leftover page

This can be simplified with a ceiling division

import math

AmountOfPages = math.ceil(AmountOfGames / games_per_page)
\$\endgroup\$
2
  • \$\begingroup\$ Thank you so much, for taking your time to check my code! You gave me different topics to learn about, which I will 100% do. :) \$\endgroup\$
    – user182461
    Commented Aug 12, 2020 at 18:28
  • \$\begingroup\$ You are welcome. I forgot to mention that I did not do a complete review of your entire code, that is, I did not check every single line. I just skimmed through the code and wrote down a few things I noticed. \$\endgroup\$
    – Flursch
    Commented Aug 12, 2020 at 18:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.