Created
February 6, 2024 02:03
-
-
Save CaptainStabs/dc29e8945e3e50d212542d4a89cd42f4 to your computer and use it in GitHub Desktop.
Qb-inventory to qs-inventory. Convert from qb to quasar inventory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import pandas as pd | |
import csv | |
# Export from database by running `SELECT citizenid, inventory FROM players;` | |
# Make sure to export using an app like TablePlus and use the `Quote if needed` option. This will not work if you export from HeidiSQL | |
# The output should look something like this | |
''' | |
citizenid,inventory | |
AFN86608,"[{""slot"":1,""name"":""weapon_nightstick"",""type"":""weapon"",""info"":{""serie"":""54yiL4at180nbcg"",""quality"":100},""amount"":1} | |
''' | |
df1 = pd.read_csv('players.csv', dtype=str) # load exported data | |
def process_inventory(line): | |
jd = json.loads(line) # Load json from string | |
df = pd.DataFrame(jd) # Load json into datafram | |
if 'info' in list(df.columns): # Prevents crashing due to an empty inventory (singular edge case) | |
df['info'] = df['info'].apply(lambda x: {**x, 'quality': 100} if isinstance(x, dict) else pd.NA if pd.isna(x) else x) # Append quality into info if info has contents | |
df['info'] = df['info'].apply(lambda x: {'quality': 100} if x == [] else x) # Add quality to info if there is no content | |
df['count'] = df['amount'] # count is the same as amount because quasar is stupid | |
df['created'] = 1707157018 # All converted items will have the same creation date | |
df = df[['name', 'type', 'info', 'amount', 'count', 'slot', 'created']] # reorder just for my sanity and because i don't trust lua | |
output = json.dumps(json.loads(df.to_json(orient='records'))).replace(': ', ':').replace(', "', ',"').replace('}, {', '},{') # remove spaces because lua can't be trusted | |
return output | |
df1['inventory'] = df['inventory'].apply(process_inventory) | |
df1.to_csv('test.csv', index=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment