Jump to content

So I have some code

def extract(soup, CONTINUE=True, FIRST_PAGE=True):
    tbody = soup.find("tbody")
    rows = tbody.find_all("tr", role="row")
    for row in rows:
        sub_rows = row.find_all("td")
        command = {}
        command["command"] = sub_rows[0].get_text()
        command["message"] = sub_rows[1].get_text()
        command["accessLevel"] = sub_rows[2].get_text()
        commands.append(command)
    next_button_li = driver.find_element_by_id("DataTables_Table_0_next")
    next_button_a = next_button_li.find_elements_by_tag_name("a")
    next_button_a[0].click()
    soup = BeautifulSoup(str(driver.page_source), "html.parser")
    if (soup.find("li", class_="paginate_button next disabled")
            is None and CONTINUE is True):
        extract(soup, FIRST_PAGE=False)
    elif (soup.find("li", class_="paginate_button next disabled")
            is not None and FIRST_PAGE is False and CONTINUE is True):
        extract(soup, CONTINUE=False, FIRST_PAGE=False)

It has some more code but in all it goes & does its thing and returns in a file with 

[
  {
    "message": "hello",
    "accessLevel": "everyone",
    "command": "!hello"
  },
  {
    "message": "hi",
    "accessLevel": "everyone",
    "command": "!hi"
  }
]

Do you know of a way I can add more "stuff": "stuff without pulling anything",

I need it to look like this:

[{
  "command": "!hi",
  "group": "",
  "status": true,
  "runAsBot": false,
  "runAsUserElevated": false,
  "startsWithEx": false,
  "message": "hi",
  "lastUsed": "2016-01-23T02:51:02.4542456-08:00",
  "cooldown": 0,
  "accessLevel": 1,
  "soundFile": "",
  "costEnabled": false,
  "costAll": 0,
  "costVIPViewer": 0,
  "costVIPB": 0,
  "costVIPS": 0,
  "costVIPG": 0,
  "costVIPMod": 0,
  "costVIPStreamer": 0,
  "OnScreenWidgetEnabled": false,
  "OnScreenWidgetName": "",
  "OnScreenWidgetTitle": "",
  "OnScreenWidgetMessage": "",
  "OnScreenWidgetImageLink": "",
  "OnScreenWidgetAnimMode": 0,
  "OBSRemoteEnabled": false,
  "OBSRemoteAction": 0,
  "OBSRemoteSceneName": "",
  "CommandChaningCmdName": "",
  "CommandChaningRunAfter": 0,
  "CommandChaningRunAsAdmin": false,
  "VIPModEnabled": false,
  "VIPModIfViewer": 0,
  "VIPModIfVIPB": 0,
  "VIPModIfVIPS": 0,
  "VIPModIfVIPG": 0,
  "VIPModAddDays": 30,
  "countdown": "2016-01-22T21:59:40.053443-08:00",
  "counter": 8,
  "APITarget": false,
  "hideFromCmdList": true,
  "accessDeniedMsg": "",
  "noPointsMsg": "",
  "disableChanAccess": false,
  "disableWhisperAccess": false,
  "disableDiscordAccess": false,
  "isSoundVolumeSet": true,
  "soundVolume": 100
},
{
  "command": "!hello",
  "group": "",
  "status": true,
  "runAsBot": false,
  "runAsUserElevated": false,
  "startsWithEx": false,
  "message": "hello",
  "lastUsed": "2016-01-23T02:50:56.4220873-08:00",
  "cooldown": 0,
  "accessLevel": 1,
  "soundFile": "",
  "costEnabled": false,
  "costAll": 0,
  "costVIPViewer": 0,
  "costVIPB": 0,
  "costVIPS": 0,
  "costVIPG": 0,
  "costVIPMod": 0,
  "costVIPStreamer": 0,
  "OnScreenWidgetEnabled": false,
  "OnScreenWidgetName": "",
  "OnScreenWidgetTitle": "",
  "OnScreenWidgetMessage": "",
  "OnScreenWidgetImageLink": "",
  "OnScreenWidgetAnimMode": 0,
  "OBSRemoteEnabled": false,
  "OBSRemoteAction": 0,
  "OBSRemoteSceneName": "",
  "CommandChaningCmdName": "!cd5",
  "CommandChaningRunAfter": 1,
  "CommandChaningRunAsAdmin": false,
  "VIPModEnabled": false,
  "VIPModIfViewer": 0,
  "VIPModIfVIPB": 0,
  "VIPModIfVIPS": 0,
  "VIPModIfVIPG": 0,
  "VIPModAddDays": 30,
  "countdown": "2016-01-22T22:01:05.6060647-08:00",
  "counter": 14,
  "APITarget": false,
  "hideFromCmdList": false,
  "accessDeniedMsg": "",
  "noPointsMsg": "",
  "disableChanAccess": false,
  "disableWhisperAccess": false,
  "disableDiscordAccess": false,
  "isSoundVolumeSet": true,
  "soundVolume": 100
}]

Thanks

Link to comment
https://linustechtips.com/topic/611015-maybe-you-can-help-my-python/
Share on other sites

Link to post
Share on other sites

On 14/06/2016 at 9:31 AM, Mitch619911 said:

command = {}
command["command"] = sub_rows[0].get_text() 
command["message"] = sub_rows[1].get_text() 
command["accessLevel"] = sub_rows[2].get_text() 
commands.append(command)

 

Do you mean that you want the code above to work for each different option there is?

 

For example you want it to do:

command = {} 
command["command"] = sub_rows[0].get_text() 
command["message"] = sub_rows[1].get_text() 
command["accessLevel"] = sub_rows[2].get_text() 
command["group"] = sub_rows[3].get_text()
command["counter"] = sub_rows[4].get_text
command["..."] = sub_row[...].get_text
...
commands.append(command)

So you want to add all the parts in the JSON array into the command array?

Link to post
Share on other sites

  • 3 weeks later...
1 hour ago, Mitch619911 said:

Yeah kinda, I just remade the code to work with a php to add the things I wanted.

Its pretty easy in Python.

Use the json package to read the file and parse it to a python dictionary.

Add the entries you want and then convert the dictionary to json and save it again.

Desktop: Intel i9-10850K (R9 3900X died 😢 )| MSI Z490 Tomahawk | RTX 2080 (borrowed from work) - MSI GTX 1080 | 64GB 3600MHz CL16 memory | Corsair H100i (NF-F12 fans) | Samsung 970 EVO 512GB | Intel 665p 2TB | Samsung 830 256GB| 3TB HDD | Corsair 450D | Corsair RM550x | MG279Q

Laptop: Surface Pro 7 (i5, 16GB RAM, 256GB SSD)

Console: PlayStation 4 Pro

Link to post
Share on other sites

2 minutes ago, mathijs727 said:

Its pretty easy in Python.

Use the json package to read the file and parse it to a python dictionary.

Add the entries you want and then convert the dictionary to json and save it again.

I donno, I just made a php just to convert somethings in it, after a day with no replys I just went and made some php for it :P

Link to post
Share on other sites

4 hours ago, Mitch619911 said:

I donno, I just made a php just to convert somethings in it, after a day with no replys I just went and made some php for it :P

But PHP is about the ugliest language in existence (except for Prolog, god I hate Prolog).

 

Look at how clean this Python implementation is:

import json

if __name__ == "__main__":
  with open("infile.json", "r") as file:
    data = json.loads(file.read())
    for entry in data:
      entry["status"] = True
      entry["runAsBot"] = False
      # Set more values if you want...

    with open("outfile.json", "w+") as file:
      file.write(json.dumps(data))

 

Desktop: Intel i9-10850K (R9 3900X died 😢 )| MSI Z490 Tomahawk | RTX 2080 (borrowed from work) - MSI GTX 1080 | 64GB 3600MHz CL16 memory | Corsair H100i (NF-F12 fans) | Samsung 970 EVO 512GB | Intel 665p 2TB | Samsung 830 256GB| 3TB HDD | Corsair 450D | Corsair RM550x | MG279Q

Laptop: Surface Pro 7 (i5, 16GB RAM, 256GB SSD)

Console: PlayStation 4 Pro

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×