def render_gist(gist_id): g = Github() gist = g.get_gist(gist_id) embed_url = "<script src=\"https://gist.github.com/%s/%s.js\"></script>" % (gist.user.login, gist_id) output = "<p>%s</p> %s" % (gist.description, embed_url) return output
def get(self, id): token = app.config['API_TOKEN'] g = Github(token) # authenticate to avoid rate limits content = json.loads([ value for key, value in g.get_gist(id).files.items() if key.endswith('.kbd.json') ][0].content) data = kle_render.deserialise(content) img = kle_render.render_keyboard(data) return serve_pil_image(img)
def execute(update, context): """Execute the content of the gist.""" g = Github() gist = g.get_gist(GIST_ID) gist_content = get_gist_content(gist, "commands.txt") with open("script.sh", "w") as f: for line in gist_content.split("\n"): if line.startswith("#") and line.endswith(";"): f.write(line[1:-1] + "\n") result = subprocess.run(["sh", "script.sh"], stdout=subprocess.PIPE) update.message.reply_text(result.stdout.decode("utf-8"))
class GithubManager(object): """ Wrapper for Github operations, specifically retrieving scripts from gists """ def __init__(self, username, password): self.__github = Github(username, password) self._gists = {} @classmethod def from_environment_vars(cls): # Construct a Github wrapper using environment variable settings username = os.environ['GITHUB_USER'] password = os.environ['GITHUB_PASSWORD'] return cls(username, password) def execute_gist(self, scriptManager, id, filename, input={}, continue_on_error=True): """ Run the Python code contained in the given gist file in a safe context. Errors will be ignored (after stopping execution) if continue_on_error is true. An exception will be raised if continue_on_error is false. """ log.open_context('Script ' + filename + ' from gist ' + id) # retrieve the gist, but don't make redundant API calls gist = None if id in self._gists: gist = self._gists[id] else: gist = self.__github.get_gist(id) self._gists[id] = gist # security check if gist.public: raise SecurityException('Error: attempted to run public code') # extract the script script = gist.files[filename].content output = scriptManager.execute(script, input, continue_on_error) log.close_context() return output
def cli(ctx): """Pull commands from saved GitHub gist.""" commands_file_path = os.path.join(utils.dir_path, 'commands.json') token = utils.get_github_token() if not token: return hub = Github(token['token']) gist = hub.get_gist(token['gist']) gist_url = f"https://gist.github.com/{token['gist']}" prompt_str = f"[CRITICAL] Replace local commands with GitHub gist\nGist URL : {gist_url} ?" if click.confirm(prompt_str, abort=True): pass """Using `w+` so it create the file if doesn't exist (Issue #64)""" with open(commands_file_path, 'w+') as commands_file: commands_file.write(gist.files['commands.json'].content) click.echo("Done!")
def gist(id, filename=None, version=None, lines=None): g = Github() gist = g.get_gist(id) if version is not None: for h in gist.history: if h.version[:len(version)] == version: gist = h break f = gist.files[filename or gist.files.keys()[0]] if lines: content = [] split = f.content.splitlines() for start, stop in lines: content.append('\n'.join(split[start - 1:stop])) return "\n\n".join(content) return f.content
def cli(ctx): """Push commands to a secret GitHub gist.""" token = utils.get_github_token() if not token: return gist_url = f"https://gist.github.com/{token['gist']}" prompt_str = f"[CRITICAL] Overwrite upstream gist with local commands?\nGist URL : {gist_url}" click.confirm(prompt_str, abort=True) commands = utils.read_commands() if not commands: click.echo("No commands to push. Add one by 'keep new'. ") else: hub = Github(token['token']) gist = hub.get_gist(token['gist']) gist.edit(files={'commands.json': InputFileContent(json.dumps(commands))}) click.echo("Done!")
class Connector: def __init__(self): self._git = Github() self._authorised = None def isauthorised(self, login: str, pword: str) -> bool: self._git = Github(login, pword) try: self._authorised = self._git.get_user().login except BadCredentialsException: self._git = Github() return False else: return True def logout(self): self._authorised = None self._git = Github() def authorised(self) -> str: return self._authorised def user(self, login=None) -> NamedUser: if self._authorised and (not login or login.lower() == self._authorised.lower()): user = self._git.get_user() elif login: user = self._git.get_user(login) else: raise NotAutorisedUserException() user.login return user def repo(self, id) -> Repository: repo = self._git.get_repo(id) repo.owner return repo def gist(self, id) -> Gist: gist = self._git.get_gist(id) gist.owner return gist
class Command(BaseCommand): help = 'Update the gist with sample data files' def handle(self, *args, **options): self.gh = Github(os.getenv('GITHUB_TOKEN')) self.org = self.gh.get_organization("california-civic-data-coalition") self.repo = self.org.get_repo("django-calaccess-raw-data") self.gist = self.gh.get_gist('66bed097ddca855c36506da4b7c0d349') sample_data_dir = self.repo.get_dir_contents('/example/test-data/tsv/') files = {} for file in sample_data_dir: lines = file.decoded_content.splitlines() # only modify files with records in them if len(lines) > 0: # we want the header + the first five lines without illegal chars top_lines = [] for line in lines: if '"' not in line: top_lines.append(line) if len(top_lines) == 6: break # recombine the split lines into a single string joined_lines = '\r\n'.join(top_lines) # add to dict files[file.name] = InputFileContent(content=joined_lines) else: # add null file files[file.name] = None # now save self.gist.edit( description='Updating sample files', files=files, )
class Command(BaseCommand): help = 'Update the gist with sample data files' def handle(self, *args, **options): self.gh = Github(os.getenv('GITHUB_TOKEN')) self.org = self.gh.get_organization("california-civic-data-coalition") self.repo = self.org.get_repo("django-calaccess-raw-data") self.gist = self.gh.get_gist('66bed097ddca855c36506da4b7c0d349') sample_data_dir = self.repo.get_dir_contents('/example/test-data/tsv/') files = {} for file in sample_data_dir: lines = file.decoded_content.splitlines() # can't add empty files to gist, so skip if len(lines) > 0: # we want the header + the first five lines without illegal chars top_lines = [] for line in lines: if '"' not in line: top_lines.append(line) if len(top_lines) == 6: break # recombine the split lines into a single string joined_lines = '\r\n'.join(top_lines) files[file.name] = InputFileContent(content=joined_lines) # now save self.gist.edit( description='Updating sample files', files=files, )
def get_repo_data_from_github_api(provider, repo_url, access_token=None): if provider not in REPO_PROVIDERS: raise Exception(f"unknown provider: {provider}") if provider in ["GitHub", "Gist"]: repo_data = {"remote_id": None, "fork": None} try: g = Github(access_token) if provider == "GitHub": # github repo url is in this form: "https://github.com/{self.user}/{self.repo}" full_name = repo_url.split("github.com/")[-1] repo = g.get_repo(f"{full_name}") else: repo = g.get_gist(f'{repo_url.split("/")[-1]}') # we need remote_id to detect renamed repos/users repo_data["remote_id"] = repo.id except GithubException as e: if e.status == 404: # repo doesnt exists anymore repo_data["fork"] = 404 return repo_data elif e.status == 403: reset_seconds = g.rate_limiting_resettime - time.time() # round expiry up to nearest 5 minutes (as it is done in bhub) minutes_until_reset = 5 * (1 + (reset_seconds // 60 // 5)) e.data["minutes_until_reset"] = minutes_until_reset raise e else: raise e if getattr(repo, "fork", None) or getattr(repo, "fork_of", None): # GitHub object has fork attribute, but Gist object has fork_of repo_data["fork"] = 1 else: repo_data["fork"] = 0 return repo_data else: return None
class Logger: _FILL_CHAR = '.' def __init__(self, token): self.g = Github(token) log.info('init GitHub api for user @{}'.format(self.g.get_user().login)) self.gist = None self.name = None def create_gist(self, name): self.gist = self.g.get_user().create_gist(True, {name: InputFileContent(Logger._FILL_CHAR)}) self.name = name log.info('use gist {}@{}'.format(self.name, self.gist.id)) def use_gist(self, id): self.gist = self.g.get_gist(id) self.name = list(self.gist.files.keys())[0] log.info('use gist {}@{}'.format(self.name, self.gist.id)) def log(self, log): content = self._get_gist_content() new_content = InputFileContent(content + '\n' + log) self._update_gist_content(new_content) def clear(self): self._update_gist_content(InputFileContent(Logger._FILL_CHAR)) def _get_gist_content(self): content = list(self.gist.files.values())[0].content # since github gists require at least one alphanumeric character within gist content to be present while # creating, we remove it, replacing it with the fist line of "real" content if content == Logger._FILL_CHAR: return '' return content def _update_gist_content(self, new_content): self.gist.edit(files={self.name: new_content})
# -*- coding: utf-8 -*- import subprocess from github import Github from github.InputFileContent import InputFileContent from credentials import CREDS from driver import main from driver import getlongterm subprocess.call(["python", "driver.py"]) g = Github(CREDS['TOKEN']) spotify_gist = g.get_gist(CREDS['GIST_ID']) #spotify_gist_long_term = g.get_gist('20d9ea0342b543a1460fd13be64a7c60') f = InputFileContent(main()) eggs = InputFileContent(getlongterm()) spotify_gist.edit('🎧 My music activity', {'🎧 My music activity over the last 4 weeks': f}) #spotify_gist_long_term.edit('🎧 My music activity over the last 6 months', # {'🎧 My music activity over the last 6 months': eggs}) spotify_gist.edit('🎧 My music activity', {'🎧 My music activity over the last 6 months': eggs})
if __name__ == '__main__': init_db() # pip install pygithub from github import Github gh = Github(TOKEN) # # # # OR: # # But: "github.GithubException.RateLimitExceededException: 403 {'message': "API rate limit exceeded for \ # # 79.000.10.000. (But here's the good news: Authenticated requests get a higher rate limit. Check out the # # documentation for more details.)", 'documentation_url': 'https://developer.github.com/v3/#rate-limiting'}" # gh = Github() gist = gh.get_gist('2f80a34fb601cd685353') print(gist) print('History ({}):'.format(len(gist.history))) with create_connect() as connect: try: for history in reversed(gist.history): print(' committed_at: {}, version: {}, files: {}'.format( history.committed_at, history.version, history.files)) if 'gistfile1.txt' not in history.files: print(' Not found file "gistfile1.txt"!') continue file = history.files['gistfile1.txt'] # print(' url: {}'.format(file.raw_url))
import discord import tabulate from BotUtils import BotServices from bs4 import BeautifulSoup, NavigableString, Tag from praw.endpoints import API_PATH from github import Github, InputFileContent from config import github_token, webhook import requests from discord import Webhook, RequestsWebhookAdapter, File webhook = Webhook.partial(*webhook, adapter=RequestsWebhookAdapter()) g = Github(github_token) gist = g.get_gist('28f68ce32bca9a8f6aa1da1c52e1e72a') services = BotServices('RedditAPIChecker') reddit = services.reddit('Lil_SpazBot') log = services.logger() def parseEndpoints(): url = 'https://www.reddit.com/dev/api' html_content = requests.get( url, headers={ 'user-agent': 'Reddit API diff checker by u/Lil_SpazJoekp' }).text soup = BeautifulSoup(html_content, 'lxml') endpoints = soup.findAll(attrs={'class': 'endpoint'})
import argparse import os import ast from github import Github parser = argparse.ArgumentParser( description="This is a gister demo for youtube!") parser.add_argument('--gistid', metavar="GISTID", type=str, help="This is the ID of the gist!") arguments = parser.parse_args() if arguments.gistid: GTHB = Github(os.getenv('GUSER'), os.getenv('GPASS')) gister = GTHB.get_gist(arguments.gistid) for key in gister.files.keys(): print(f'Executing the file: {key} from gists of github!') exec( compile(ast.parse(gister.files[key].content), filename=key, mode='exec')) else: parser.print_help()
def main(): g = Github(GITHUB_TOKEN) output = '_Last updated on {}_\n\n'.format(datetime.now()) # Go through the given repositories that use milestones for repo_name in REPOS_M: repo = g.get_repo(repo_name) # Print repo header output += '# [{}]({})\n\n'.format(repo.name, repo.html_url) # Only check open milestones open_milestones = repo.get_milestones(state='open') found_milestone_without_due_date = False # Go through all open milestones for milestone in open_milestones: # Skip milestones without issues if (milestone.open_issues + milestone.closed_issues) == 0: continue # Milestones don't have .html_url, so creating it manually: milestone_html_url = '{}/milestone/{}'.format( repo.html_url, milestone.number) # skip milestones without due date if milestone.due_on is None: print( 'Skipped {} (No due date set)'.format(milestone_html_url)) found_milestone_without_due_date = True continue # Print milestone header including progress and due date. output += '### [{}]({}) {}/{} issues ({:.0f}%) - Due on {}\n\n'.format( milestone.title, milestone_html_url, milestone.closed_issues, milestone.open_issues + milestone.closed_issues, 100 * milestone.closed_issues / (milestone.open_issues + milestone.closed_issues), milestone.due_on.date() if milestone.due_on else '???') # Go through all issues. First the closed ones, then the open ones output += process_issues(repo, 'closed', milestone=milestone) output += process_issues(repo, 'open', milestone=milestone) if open_milestones.totalCount == 0 or found_milestone_without_due_date: output += 'No milestones open or no milestone with due date set.\n\n' # Go through the given repositories that don't use milestones. for repo_name, name, label_name in REPOS_P: repo = g.get_repo(repo_name) # Print repo header output_name = repo.name if name is None else name output += '# [{}]({})\n\n'.format(output_name, repo.html_url) # Get closed issues that were closed within the last week. last_week = datetime.today() - timedelta(days=7) output_closed_issues = process_issues(repo, 'closed', since=last_week, label_names=[label_name]) # Now get all open issues. output_open_issues = process_issues(repo, 'open', label_names=[label_name]) # Check if there was actually something to be printed. if (len(output_closed_issues) + len(output_open_issues)) > 0: # Print info output += '_Not based on milestones._\n_✅ -> Closed within the last 7 days._\n\n\n' output += output_closed_issues output += output_open_issues else: output += 'No issues to show.\n\n' # Write gist. gist = g.get_gist(GIST_ID) gist.edit( description=GIST_DESCRIPTION, files={GIST_FILENAME: InputFileContent(content=output)}, ) # Print success and gist url for easy access. print('View output at {}'.format(gist.html_url))
class GitHubGist: def __init__(self, gistToken=""): self.logger = logging.getLogger('uba') self.enablePublicGist = True if not self.logger.hasHandlers(): logHandler = logging.StreamHandler() logHandler.setLevel(logging.DEBUG) self.logger.addHandler(logHandler) self.logger.setLevel(logging.INFO) if gistToken == "": self.gistToken = config.gistToken else: self.gistToken = gistToken self.status = "Not configured" self.connected = False self.user = None self.gist = None self.description = None try: if len(self.gistToken) < 40: self.status = "Gist token is has valid" raise Exception(self.status) self.gh = Github(self.gistToken) self.user = self.gh.get_user() self.status = "Gist user: "******"Could not connect" self.logger.error(str(error)) def openGistBookNote(self, book): self.description = GitHubGist.bToBookName(book) self.openGistByDescription(self.description) def openGistChapterNote(self, book, chapter): self.description = GitHubGist.bcToChapterName(book, chapter) self.openGistByDescription(self.description) def openGistVerseNote(self, book, chapter, verse): self.description = GitHubGist.bcvToVerseName(book, chapter, verse) self.openGistByDescription(self.description) def openGistByDescription(self, description): if self.connected and self.user is not None: self.gist = None gists = self.user.get_gists() for g in gists: if description == g.description: self.gist = g self.description = description self.logger.debug("Existing Gist:{0}:{1}".format( description, self.gist.id)) break def openGistById(self, id): self.gist = None try: if self.gh: self.gist = self.gh.get_gist(id) except: return None if not self.description == self.gist.description: return None def getAllNoteGists(self): if self.connected and self.user is not None: self.gist = None self.description = None gists = self.user.get_gists() notes = [] for g in gists: if g.description.startswith("UBA-Note-"): notes.append(g) return notes def id(self): if self.gist: return self.gist.id else: return "" def updateContent(self, content, updated): if self.connected: if self.gist is not None: self.gist = self.user.create_gist( self.enablePublicGist, { self.description: InputFileContent(content), "updated": InputFileContent(str(updated)) }, self.description) else: self.gist.edit( files={ self.description: InputFileContent(content), "updated": InputFileContent(str(updated)) }) def getFile(self): if self.connected and self.gist is not None: files = self.gist.files file = files[self.description] return file else: return None def getContent(self): file = self.getFile() if file is not None: return file.content else: return "" def getUpdated(self): if self.connected: if self.gist: files = self.gist.files file = files["updated"] if file and file.content is not None: return int(file.content) return 0 else: return 0 def deleteAllNotes(self): if self.connected and self.user: self.gist = None self.description = None gists = self.user.get_gists() count = 0 for g in gists: if g.description.startswith("UBA-Note-"): count += 1 g.delete() return count def bToBookName(b): return "UBA-Note-Book-{0}".format(b) def bcToChapterName(b, c): return "UBA-Note-Chapter-{0}-{1}".format(b, c) def bcvToVerseName(b, c, v): return "UBA-Note-Verse-{0}-{1}-{2}".format(b, c, v) def bookNameToB(name): res = re.search(r'UBA-Note-Book-(\d*)', name).groups() return res[0] def chapterNameToBc(name): res = re.search(r'UBA-Note-Chapter-(\d*)-(\d*)', name).groups() return res def verseNameToBcv(name): res = re.search(r'UBA-Note-Verse-(\d*)-(\d*)-(\d*)', name).groups() return res def extractContent(gist): return gist.files[gist.description].content def extractUpdated(gist): files = gist.files file = files["updated"] if file and file.content is not None: return int(file.content) else: return 0
return re.search("<h1>(.*?)</h1>", html).group(1) def get_gist_filename(link): parse_result = urlparse(link).path return "{}.md".format(os.path.split(parse_result)[1]) if len(sys.argv) < 2: raise ValueError link = sys.argv[1] link_title = get_header(link) link_md = get_gist_filename(link) g = Github(AUTH_TOKEN) user = g.get_user() link_gist_blob = InputFileContent(content="* [{}]({})".format(link_title, link)) link_gist = user.create_gist(public=True, description=link_title, files={link_md: link_gist_blob}) gist = g.get_gist(GIST_ID) gist_files = gist.files blob = gist_files[FILENAME].content blob += "\n\n* [{}]({})".format(link_title, link_gist.html_url) blob = InputFileContent(content=blob) gist.edit(files={FILENAME: blob})
from typing import List from chalice import Chalice, Response from github import Github, Gist, Organization, Team, InputFileContent import os app = Chalice(app_name='listRepositories') github_client = Github(os.environ['ACCESS_TOKEN']) organization: Organization.Organization = github_client.get_organization( os.environ['ORGANIZATION_NAME']) team: Team.Team = organization.get_team(int(os.environ['TEAM_ID'])) gist: Gist.Gist = github_client.get_gist(os.environ['GIST_ID']) @app.route('/') def index(): content: List[str] = [ f'[{repo.name}]({repo.clone_url})' for repo in team.get_repos() ] gist.edit( files={ 'repositories.md': InputFileContent(content=' \n'.join(content)), }) return Response(status_code=301, body='', headers={'Location': gist.html_url})