Skip to content

Instantly share code, notes, and snippets.

@rakinishraq
Last active December 15, 2023 16:00
Show Gist options
  • Save rakinishraq/38c588e7c59e63f3c824560b24d94bb6 to your computer and use it in GitHub Desktop.
Save rakinishraq/38c588e7c59e63f3c824560b24d94bb6 to your computer and use it in GitHub Desktop.
Update all your Gists and generate a "Gist Directory.md"
[settings]
# Get your token here: https://github.com/settings/tokens
token = ghX_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
dry_run = true
# This is an optional section for compiling [show] gists into one file
# I use this generated file to pin on my Github profile
# NOTE: Multiple files and obfuscation doesn't work in [dir]
[dir]
b6c655692ca4324e85f5c88754291541 = Gist Directory.md
# How To Obfuscation:
# first char is 1 and last char is -1, penultimate is -2, etc.
# :3x11 hides 11th char of 3rd line
# :3x13~ hides 13th char to end of 3rd line
# :20x85~92 hides chars [85-92] inclusive of 20th line
# using ranges for lines (like 1~3:1~)
[show]
38c588e7c59e63f3c824560b24d94bb6 = Sync Gists.py, config.ini:3x11:3x13~:20x85~92
ca6cb6d850295229582caa5c5ef82c86 = Stash.py:29x41~-1
5ce3b34e8e99d9c4b9e269229b6e5f34 = My Portfolio.txt
5e1d1768ea46a9e9aae6157c226ad99d = Sort YT Subs.py
96dc2c9fef19d82f6101dd4005c636b9 = Minimal Miro.user.js
7f7d77d85aa57d2391c31d98bec6ab81 = Startpage Bangs.user.js
[hide]
5815b4749a6279dd30be24b20a683da8 = C:\Users\user\AppData\Roaming\librewolf\Profiles\XXXXXXXX.default-default\chrome\userChrome.css
# Work in progress, use at own risk
import configparser, requests, os
from datetime import datetime
info = """
0. Fix set_gist 404 error
1. Compare contents before syncing and skip if identical (factor in obfus)
2. Add obfuscation of config file contents (robust enough for 1x~)
3. Get_gists only works on public gists?
"""
config = configparser.ConfigParser()
config.read('config.ini')
token = config['settings']['token']
dry_run = config.getboolean('settings', 'dry_run')
def get_gist(gist, file):
response = requests.get(f"https://api.github.com/gists/{gist}")
response.raise_for_status()
data = response.json()
gist_date = data['updated_at']
if file not in data['files']:
file = list(data['files'].keys())[0]
content = data['files'][file]['content']
return gist_date, file, content
def set_gist(gist, file, content):
print(f"set_gist({gist}, {file}, content, {token})")
if dry_run:
return
headers = {'Authorization': f'token {token}'}
data = {
"files": {
file: {
"content": content
}
}
}
response = requests.patch(f"https://api.github.com/gists/{gist}", headers=headers, json=data)
response.raise_for_status() # Raise exception if invalid response
return response.json()
def newer(local_file, gist_date):
# Parse gist_date into datetime
gist_date = datetime.strptime(gist_date, r"%Y-%m-%dT%H:%M:%SZ")
# Get local_file's last modified date as datetime
local_modified_date = datetime.fromtimestamp(os.path.getmtime(local_file))
# Return different values based on comparison
if gist_date > local_modified_date:
return 1 # gist_date is newer
elif gist_date < local_modified_date:
return -1 # local_modified_date is newer
else:
return 0 # both dates are the same
def update(gist_id, file, dir=None):
(gist_date, gist_name, gist_content), n = get_gist(gist_id, file), 0
# Gists Directory.md -> upload
if dir:
url = f"https://gist.github.com/{gist_id}"
local_content = '\n'.join(dir)
print(local_content)
# local missing/remote newer -> download
elif not os.path.exists(file) or (n := newer(file, gist_date)) == 1:
if dry_run:
print("downloading: "+file, "remote newer" if n == 1 else "file not exist")
return
with open(file, "w") as f:
f.write(gist_content)
print("Downloading/updating: "+file)
return
# local gist is newer -> upload
elif n == -1:
local_content = open(file).read()
# upload
set_gist(gist_id, gist_name, local_content)
def main():
dir, export_gist = [], None
for section in config.sections():
for gist, file in config[section].items():
if section == "settings": continue
elif section == "dir":
export_gist, export_file = gist, file
break
elif section == "show":
dir.append(f"{gist}, {file}")
elif section == "censor":
pass
update(gist, file)
if dir and export_gist:
update(export_gist, export_file, dir)
if __name__ == "__main__":
print(info+'\n')
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment