Programming Journey Entry 4: Meeting JSON

I’ve started this series of posts about general programming in whatever language I happen to feel like. I decided to make it a series of posts separate from from my game development dev log series of posts. This one is focusing on some ideas I have for a couple of different utilities in Python specifically.

Entry 3 made some significant progress with some different functions coming together and some good sample data.

You can find this script in its own own repository on my GitHub (along with random notes and ramblings in the comments) if you’d like to see the “big picture”.

All of these Programming Journey posts can be found in the associated category of this blog.


Forming up a little more

Continuing from the third entry, I’ll finish out my last function with the most obvious last step of the function: opening the file and reading it in, assigning the contents to a variable:

def WriteOrOpenDatafile():
    if CheckFileExists() == False:
        DataFile = RetrieveSteamData()
        with open(SAVED_DATA_FILE_NAME, "w+") as file:
            file.write(DataFile)
            file.close()
            print("file written")
        return DataFile
    else:
        with open(SAVED_DATA_FILE_NAME, "r+") as file:
            FileContents = file.read()
            file.close() # always close when done!
        pyperclip.copy(FileContents) # for me easier than print()
        return FileContents

I actually tried to use the JSON module to load the file in as JSON directly but got an exception error. Apparently the data is not yet in the proper format. Or I missed a step.

This might be a good time to discuss the state of the data at this point. Pasting in the entire JSON string is impractical because even with only eight entries and inserting line breaks manually the string would go on and on for pages (the text is on my GitHub if you were interested though).

So instead I’ll put in the start and end of the string and snip out the middle.

{"appid":1536770,"name":"Learn Programming: Python - Retro","app_type":1,"logo":"https:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/1536770\/capsule_184x69.jpg","friendlyURL":false,"availStatLinks":{"achievements":false,"global_achievements":false,"stats":false,"gcpd":false,"leaderboards":false,"global_leaderboards":false}}

Again, if it’s not clear, this is one long string with no line breaks.

Since this is a string in JavaScript and embedded in an HTML document there are escape characters for the slashes. The \/ you see there is actually how you make one single slash ‘\‘.

That’s going to be part of how I deal with taking this string and somehow turning it into a form of JSON the JSON module is going to be happy with.

And also begs the more general question: what do I do with the image logos for the games?

I mean I guess I could just use the provided URLs that point to caching servers (otherwise known as CDNs) and load them that way. Or I could give the user the option of downloading them all locally. Or I could strip out those entries entirely so it doesn’t come up.

All those options feel like a solution needed for a later stage of development. For no I could probably just do a find/replace on \/ to just a single /. Maybe that is all that’s needed to make the JSON library happy.

As a test I actually did an experiment, and this worked for taking out all those escaped slashes.

BringInString = WriteOrOpenDatafile() # returns game list json string
# below, the -1 means "replace them all"
TakeOutEscSlash = BringInString.replace('''\/''','''/''',-1)

It seems this is all it took. I’m going to end this entry here even though I could go on a lot longer. I’ll start fresh with a new entry and try and summarize what I’ve learned.


Reference links:


I've made a "clearing house" repo for random python programming projects on GitHub:
https://github.com/tildesarecool/ReallyHadtoPython

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s