Skip to content

Instantly share code, notes, and snippets.

@ImanCol
Created March 4, 2023 22:09
Show Gist options
  • Save ImanCol/6d743d059d4269398b25503b9ea1e0a7 to your computer and use it in GitHub Desktop.
Save ImanCol/6d743d059d4269398b25503b9ea1e0a7 to your computer and use it in GitHub Desktop.
Only Move your file from thist Script
import sys
import requests
import time
import webbrowser
import os
import re
# Your GitHub Personal Access Token
token = 'INSERT-TOKEN'
# Your Gist description
# Your Gist content
# filepath = 'sitios_accesibles.txt'
filepath = sys.argv[1]
def get_filename(filepath):
filename = os.path.basename(filepath)
return filename
filename = get_filename(filepath)
description = filename
with open(filename, 'r', encoding="utf-8") as f:
content = f.read()
# Create the Gist
url = 'https://api.github.com/gists'
headers = {
'Accept': 'application/vnd.github+json',
'Authorization': 'Bearer ' + token,
'X-GitHub-Api-Version': '2022-11-28'
}
data = {
'description': description,
'private': False,
'files': {
filename: {
'content': content
}
}
}
# Check if the Gist already exists
try:
r = requests.get(url, headers=headers)
gist_list = r.json()
found = False
for gist in gist_list:
if gist['description'] == description:
found = True
# Update the Gist
print("Actualizando..." + filename)
r = requests.patch(url + '/' + gist['id'], headers=headers, json=data)
break
if not found:
# Create the Gist
print("Creando..." + filename)
r = requests.post(url, headers=headers, json=data)
except:
# Create the Gist
r = requests.post(url, headers=headers, json=data)
# Get the HTML URL
html_url = r.json()['files'][filename]['raw_url']
# Get the content from the HTML URL
html_content = requests.get(html_url).text
# Write the content to a file
with open('gist-log.txt', 'w', encoding="utf-8") as f:
f.write(html_content)
def open_url():
answer = input("¿Desea abrir el enlace generado? S/N: ")
if answer.lower() == 's':
url = r.json()['html_url']
webbrowser.open(url)
def wait_for_input():
input("Presione cualquier tecla para continuar...")
# Print the HTML URL
print(r.json()['html_url'])
wait_for_input()
open_url()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment