Skip to content

Instantly share code, notes, and snippets.

@tomlankhorst
Last active April 29, 2020 07:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomlankhorst/fb27f014a82519011eb9dcfc39e80a40 to your computer and use it in GitHub Desktop.
Save tomlankhorst/fb27f014a82519011eb9dcfc39e80a40 to your computer and use it in GitHub Desktop.
⭐ Thanks! Star the current GitHub repository.
#!/usr/bin/env python3
import os
import re
import requests
from subprocess import check_output
github_origin_re = r"^(git@github\.com:|https:\/\/github.com\/)([\w\-_]+)\/([\w\-_]+)\.git$"
try:
url = check_output(['git', 'remote', 'get-url', 'origin']).decode("utf-8")
except:
print("No repository with a remote named 'origin'")
exit(1)
m = re.search(github_origin_re, url)
if (m == None):
print("Not a GitHub repository")
exit(1)
repository = '%s/%s' % (m.group(2), m.group(3))
token_config_file = os.path.expanduser('~/.config/thanks/token')
os.makedirs(os.path.dirname(token_config_file), exist_ok=True)
if not os.path.isfile(token_config_file):
print('Acquire a Token with the public_repo scope here: https://github.com/settings/tokens')
print('Your token will be saved in %s' % token_config_file)
token = input('GitHub API token: ')
with open(token_config_file, 'w') as f:
f.write(token)
else:
with open(token_config_file, 'r') as f:
token = f.readline()
url = "https://api.github.com/user/starred/%s" % repository
response = requests.request("PUT", url, headers={
'Content-Length': '0',
'Authorization': 'Bearer %s' % token,
})
if response.status_code == 204:
print("⭐ %s" % repository)
exit(0)
if response.status_code == 401:
print("Unauthorized. Token cleared, check whether the public_repo scope is active.")
os.remove(token_config_file)
exit(1)
print("Oof...")
print(response)
exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment