Hey Hops just completely for fun, i just made a small program to get the JSON from a project url and save it into a file. it’s a little pointless at the moment but was very interesting trying to work out how to try and do it, along with looking things up along the way
I would love to share the code, just at the moment it is very messy and makes no sense but if Hops are interested, would very very much love to try explain
import urllib.request as urllib_request
import os
import json
from lxml import html
def read_url(url_string):
'''
Takes a Hopscotch project URL (may need to have .html at the end, but unconfirmed) and writes its data into a .json file
'''
url_obj = urllib_request.urlopen(url_string).read()
page = html.fromstring(url_obj)
for div in page.xpath('//div'):
# Get the JSON from the 'data' attribute of the <div> tag
# This is made on the assumption that there is only one <div> tag that has a 'data' attribute where Hopscotch stores the JSON data
div_data = div.get('data')
if div_data:
print('Project JSON was found.')
# Convert the string containing the data from the <div> tag into a Python object
project_data = json.loads(div_data)
write_json_file(project_data)
def write_json_file(data):
'''
Takes a Python object and writes it into a .json file
'''
project_id = data['uuid']
file_path_data = os.path.join('project_data', str(project_id) + '.json')
with open(file_path_data, 'w') as project_file:
json.dump(data, project_file, ensure_ascii=False, indent=4, sort_keys=True)
print('Writing the JSON to a file.')
read_url('https://c.gethopscotch.com/p/ykcmajup8.html')
Please feel very free to ask about anything! I can imagine a first question might be:
What is JSON? (JavaScript Object Notation)
It is just a format for storing data in general, not a great first explanation but I can elaborate. There are mentions around the forum too about it if you can’t wait for me (searching ‘Hopscotch json’ brings up most stuff)
Can I try running the code?
Most certainly! If you are asking this question, of course I can show you :) and looks like I can go with making a tutorial now :P for the time being:
-
you can go to the link and copy and paste the code into a text editor (if you are not sure about this, IDLE is where you can start)
-
save it as a Python file somewhere (just add .py to the end of the file name)
-
in the folder where you saved it, make another folder and call it
project_data
-
open the Python file again and go to the last line, where it says
read_url('https://c.gethopscotch.com/p/ykcmajup8.html')
but replace that link with any project link you like (you may have to add.html
at the end there after you paste the project link in)
(these are not very accessible instructions so I would like to update if anyone wants to try) -
run the project (if you are using IDLE, you can press F5)
-
after a little wait, you can go to the
project_data
folder and a.json
file will appear. this is the project JSON file. If you haven’t seen it before, hopefully later on maybe I can try point out a few things -
Oh dear I just remembered you might have to download some python libraries which is a bit extra, I will have to address that later. This is just an initial thing
so sorry if this doesn’t work :(
I would like to thank all previous Hops and discussions on forum referring to the JSON files for projects it encouraged me to explore! I will update this if anyone is interested
P.S. If this looks a little bit like ‘ahhhh!’ please don’t feel like that(!) one of the reasons of me sharing this is so that you can get to see and have a chance to try understand a few more things too! And plus I am sharing this very quickly at the moment and not explaining much at all hence I’ve just made it even more confusing >_<
Also this is basically my first time peeking around, so if this isn’t a good idea (e.g. for any security purposes or anything, I did hear something about that), Hopscotch Team, please let me know