-
-
Save pa-0/e9419e06ff22616230c9a4fe75fb7771 to your computer and use it in GitHub Desktop.
SnippetsLab json export to github gist #gists #snippets
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
### set a tag in snippets lab "exempt" if you do not want snippet to push to github | |
### set a tag in snippets lab "private" if you want the new gist to be secret | |
### export SnippetsLab to json document (named: SnippetsLab.json) | |
### place exported json file in same directory as main.py | |
### set your GITHUB_USERNAME and your API_TOKEN (personal access token) | |
### uncomment lines 130 and 131 to delete all your gists on github | |
### github only allows removing 30 at a time so you may have to run | |
### this multiple times. | |
### run: python main.py | |
import time | |
import requests | |
import json | |
from pathlib import Path | |
from pygments.lexers import get_lexer_by_name | |
GITHUB_USERNAME = 'github_username' | |
API_TOKEN = 'personal_access_token' | |
GET_API_URL = f"https://api.github.com/users/{GITHUB_USERNAME}/gists" | |
POST_API_URL = 'https://api.github.com/gists' | |
PARAMS = {'scope': 'gist'} | |
import_file = 'SnippetsLab.json' # mac | |
class SlToGist(): | |
def __init__(self): | |
self.file = None | |
self.snippets = None | |
self.headers = { | |
'Authorization': 'Bearer ' + API_TOKEN, | |
'Accept': 'application/vnd.github+json', | |
# 'Content-Type': 'application/json', | |
'X-GitHub-Api-Version': '2022-11-28' | |
} | |
self.folders = None | |
self.tags = None | |
self.secret_tag_key = None | |
self.exempt_tag_key = None | |
self.importedJson = self.load_import_file() | |
self.get_folders() | |
self.get_tags() | |
self.get_snippets() | |
def load_import_file(self, file_path = import_file): | |
if Path(file_path).is_file(): | |
with open(Path(file_path)) as impfile: | |
return json.load(impfile) | |
else: | |
print("%s : File not found for importing" % import_file) | |
exit() | |
def get_gists(self): | |
res = requests.get(GET_API_URL) | |
gists = res.json() | |
# print(json.dumps(gists, indent=4)) | |
return gists | |
def push_gist(self, gist = None, isPublic = True): | |
if gist is not None: | |
desc1 = self.folders[gist['folder']] if gist.get('folder') is not None else None | |
desc2 = gist['title'] if gist.get('title') is not None else None | |
description = None | |
if desc1 is not None and desc2 is not None: | |
description = "%s :: #%s" % (desc2, desc1) | |
elif desc1 is not None or desc2 is not None: | |
description = f"add title #{desc1}" if desc1 is not None else desc2 | |
else: | |
description = '' | |
files = {} | |
for idx, f in enumerate(gist['fragments']): | |
lexer = get_lexer_by_name(f['language'].replace("Lexer", '')) | |
if len(lexer.filenames) > 0: | |
lexer_ext = lexer.filenames[0].replace('*', '') | |
else: | |
lexer_ext = f".{f['language'].replace('Lexer', '')}" | |
files = { | |
f"{gist['title'].title().replace(' ', '').replace('/', '')}{idx + 1}{lexer_ext}": { | |
'content': f['content'] | |
} | |
} | |
payload = { | |
'description': description, | |
'public': isPublic, | |
'files': files | |
} | |
res = requests.post(POST_API_URL, headers=self.headers, params=PARAMS, data=json.dumps(payload)) | |
# print(gist['title']) | |
print(res.status_code) | |
print(res.url) | |
print(res.text) | |
def get_tags(self): | |
self.tags = {} | |
for t in self.importedJson['contents']['tags']: | |
self.tags[t['uuid']] = t['title'] | |
if t['title'] == 'private': | |
self.secret_tag_key = t['uuid'] | |
if t['title'] == 'exempt': | |
self.exempt_tag_key = t['uuid'] | |
def get_folders(self): | |
# self.folders = self.importedJson['contents']['folders'] | |
self.folders = {} | |
for f in self.importedJson['contents']['folders']: | |
self.folders[f['uuid']] = f['title'] | |
def get_snippets(self): | |
self.snippets = self.importedJson['contents']['snippets'] | |
def delete_all_gists(self): | |
gists = self.get_gists() | |
for g in gists: | |
res = requests.delete(POST_API_URL + f"/{g['id']}", headers=self.headers) | |
if res.status_code == 204: | |
print(f"Removed: {g['id']}") | |
else: | |
print(f"Error removing: {g['id']}") | |
def jd(json_data): | |
return json.dumps(json_data, indent=2) | |
sltg = SlToGist() | |
### uncomment next two lines to delete all your gists on github | |
### github only allows removing 30 at a time so you may have to run | |
### this multiple times. | |
# sltg.delete_all_gists() | |
# exit() | |
for snip in sltg.snippets: | |
isPublic = True | |
isExempt = False | |
if snip.get('tags') is not None and sltg.secret_tag_key in snip['tags']: | |
isPublic = False | |
else: | |
isPublic = True | |
if snip.get('tags') is not None and sltg.exempt_tag_key in snip['tags']: | |
isExempt = True | |
else: | |
isExempt = False | |
if isExempt is False: | |
print(f"pushing snippet: {snip['title']}") | |
sltg.push_gist(snip, isPublic) | |
time.sleep(2) | |
else: | |
print(f"not pushing exempt snippet: {snip['title']}") | |
# exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment