def construct_gist_from_bissue_attachments(bissue, bexport): issue_id = bissue["id"] battachments = bexport.get_issue_attachments(issue_id) if not battachments: return None gist_description = construct_gist_description_for_issue_attachments(bissue, bexport) gist_files = {"# README.md": InputFileContent(gist_description)} for name in battachments.keys(): content = bexport.get_issue_attachment_content(issue_id, name) if len(content) == 0: print("Warning: file '{}' of bitbucket issue {}/#{} is empty.".format( name, bexport.get_repo_full_name(), issue_id )) content = "(empty)" elif len(content) > 500 * 1000: print("Error: file '{}' of bitbucket issue {}/#{} is too big and cannot be uploaded as a gist file. This has to be done manually.".format( name, bexport.get_repo_full_name(), issue_id )) content = "(too big)" gist_files[name] = InputFileContent(content) return { "description": gist_description, "files": gist_files }
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 edit_gist(gist_id, content): gist = g.get_gist(gist_id) gist_filename = next(iter(gist.files.values())).filename return gist.edit( files = { gist_filename: InputFileContent(content=content) } )
def create_gist(github, logline): """Create a private gist with log data for the specified log line.""" context_loglines = logline.get_context_loglines() if context_loglines: content = '\n'.join([str(ll) for ll in context_loglines]) return github.get_user().create_gist( False, {'application.log': InputFileContent(content)})
def main(): token = os.getenv('GIT_HUB_GIST_EXERCICSE_TOKEN') gh = Github(token) me = gh.get_user() print( me.create_gist(True, {"repos_stats.py": InputFileContent(code)}, "Get Github users repos and stats"))
def main(): status = get_repo_stats(pb) token = os.environ['GH_GIST_CREATE_TOKEN'] gh = Github(token) me = gh.get_user() code = ''' from collections import namedtuple Repo = namedtuple('Repo', 'name stars forks') def get_repo_stats(user, n=5): """Takes a Github user object and returns the top n most popular repos by star count, skips forks.""" repos = [] for repo in user.get_repos(): if repo.fork: continue repos.append(Repo(name=repo.name, stars=repo.stargazers_count, forks=repo.forks_count)) return sorted(repos, key=lambda x: x.stars, reverse=True)[:n] ''' me.create_gist(True, {"repo_stats.py": InputFileContent(code)}, "Get GH user's most popular repos")
def create_gist(github, logline): """Create a private gist with log data for the specified log line.""" log.debug('Creating gist for error: {0}', logline) context_loglines = logline.get_context_loglines() content = '\n'.join([text_type(ll) for ll in context_loglines]) if not content: return None return github.get_user().create_gist(False, {'application.log': InputFileContent(content)})
def read_files(): ''' Read history and setting file ''' history_path = '{}/{}'.format(Path.home(), config.parser['Shell']['path']) files = {} with open(history_path, errors='ignore', mode='r') as history_file: files[os.path.basename(history_file.name)] = InputFileContent( history_file.read()) with open(constants.CONFIG_PATH, mode='r') as config_file: # Remove token key on uplaod lines = config_file.readlines() lines.pop(1) files[os.path.basename(config_file.name)] = InputFileContent(''.join( map(str, lines))) return files
def update(filename: str, content=None, description: str = "", default=None): if not isinstance(filename, str): raise ValueError("filename has to be specified and be str") if not content: raise ValueError("No content") raw = json.dumps(content, indent=2, default=default) gist.edit( description=description, files={filename: InputFileContent(raw)})
def prepare_payload(): """Prepare history and config file for upload""" files = {} history_path = config.get_history_path() config_path = constants.CONFIG_PATH with open(history_path, errors="ignore", mode="r") as history_file: files[os.path.basename(history_file.name)] = InputFileContent( history_file.read()) with open(config_path, mode="r") as config_file: # Remove token key on uplaod lines = config_file.readlines() lines.pop(1) files[os.path.basename(config_file.name)] = InputFileContent("".join( map(str, lines))) return files
def generate_seed(self, permalink, spoiler): seed_name = "".join(random.choice(string.digits) for _ in range(18)) file_name = "".join(random.choice(string.digits) for _ in range(18)) os.system(f"python sslib/randoscript.py --dry-run --noui --seed={seed_name} --permalink={permalink}") try: spoiler_log_file_name = f"SS Random {seed_name} - Spoiler Log.txt" spoiler_log_file = open(spoiler_log_file_name, "r") except FileNotFoundError: spoiler_log_file_name = f"SS Random {seed_name} - Anti Spoiler Log.txt" spoiler_log_file = open(spoiler_log_file_name, "r") bare_log = spoiler_log_file.read() log = bare_log.split('\n') version = log[0] permalink = log[1].split(' ')[1] hash_re = re.compile('Hash : (.*)') rando_hash = hash_re.findall(log[3])[0] spoiler_log_file.close() if spoiler: timestamp = datetime.now().strftime("%Y-%m-%d-%H:%M:%S") gh = Github(self.github_token) gh_auth_user = gh.get_user() gist = gh_auth_user.create_gist( public=False, files={f"spoiler_log_{timestamp}.txt": InputFileContent(bare_log)}, description="Skyward Sword Randomizer Spoiler Log" ) spoiler_log_url = gist.html_url return { "permalink": permalink, "file_name": file_name, "seed": seed_name, "hash": rando_hash, "spoiler_log_url": spoiler_log_url, "version": version } else: return { "permalink": permalink, "file_name": file_name, "seed": seed_name, "hash": rando_hash, "version": version }
def write_commands(commands): file = get_commands_file() user = get_api().get_user() files = { FILENAME: InputFileContent(content=json.dumps(commands, indent=2)) } if not file: user.create_gist(public=True, files=files, description=COMMENT_IDENTIFIER) else: gist = get_commands_gist() gist.edit(description=COMMENT_IDENTIFIER, files=files)
def post_error(logfile): # Contact github token = os.getenv('GH_TOKEN') g = Github(token) user = g.get_user() with open(logfile) as fh: gist = user.create_gist(True, {'logfile.txt': InputFileContent(fh.read())}, 'Output of the failing CI job.') repo = g.get_repo("ferchault/multiQM") body = "Log to be found here: %s." % gist.html_url repo.create_issue(title="Local CI build failed", body=body)
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})
def create_gist(github, logline): """Create a private gist with log data for the specified logline. :param github: :type github: Github :param logline: :type logline: medusa.logger.LogLine :return: :rtype: github.Gist.Gist """ context_loglines = logline.get_context_loglines() if context_loglines: content = '\n'.join([str(ll) for ll in context_loglines]) return github.get_user().create_gist(False, {'application.log': InputFileContent(content)})
def create_gist( gh: Github, name: str, filepaths: List[PosixPath], public: bool = False, description: Union[str, _NotSetType] = NotSet, ): files = {f.name: InputFileContent(f.read_text()) for f in filepaths} u = gh.get_user() gist = u.create_gist(public, files, description) clone_gist(gist, name) dest = PosixPath(name) print("gist created and cloned") print(dest.absolute())
def gh_comment_issue(dest, issue, comment): # upload attachement, if there is one if 'attachment_name' in comment: filename = comment['attachment_name'] if attachment_export: issuenumber = issue.number if dest is not None else 0 dirname = os.path.join(attachment_export_dir, 'ticket' + str(issuenumber)) if not os.path.isdir(dirname): os.makedirs(dirname) # write attachment data to binary file open(os.path.join(dirname, filename), 'wb').write(comment['attachment']) note = 'Attachment [%s](%s) by %s created at %s' % ( filename, attachment_export_url + 'ticket' + str(issuenumber) + '/' + filename, comment['author'], comment['created_at']) else: if dest is None: return assert gh_user is not None gistname = dest.name + ' issue ' + str( issue.number) + ' attachment ' + filename filecontent = InputFileContent(comment['attachment']) try: gist = gh_user.create_gist( False, {gistname: filecontent}, 'Attachment %s to Ipopt issue #%d created by %s at %s' % (filename, issue.number, comment['author'], comment['created_at'])) note = 'Attachment [%s](%s) by %s created at %s' % ( filename, gist.files[gistname].raw_url, comment['author'], comment['created_at']) except UnicodeDecodeError: note = 'Binary attachment %s by %s created at %s lost by Trac to GitHub conversion.' % ( filename, comment['author'], comment['created_at']) print ' LOOSING ATTACHMENT', filename, 'in issue', issue.number sleep(sleep_after_attachment) if 'note' in comment and comment['note'] != '': note += '\n\n' + comment['note'] else: note = 'Comment by %s created at %s' % (comment['author'], comment['created_at']) if 'note' in comment and comment['note'] != '': note += '\n\n' + comment['note'] if dest is None: return issue.create_comment(note) sleep(sleep_after_request)
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!")
def cli(ctx): """Register a GitHub Token to use GitHub Gists as a backup.""" dir_path = os.path.join(os.path.expanduser('~'), '.keep', '.credentials') existing_token = None if os.path.exists(dir_path): with open(dir_path) as f: existing_token = json.load(f) if click.confirm( '[CRITICAL] Reset token saved in ~/.keep/.credentials ?', abort=True): os.remove(dir_path) token = click.prompt( 'Create a GITHUB TOKEN with permission to Gists and paste here ') gist_id = existing_token.get('gist') if existing_token else None click.echo( "\nAwesome! Your GitHub token has been registered. Now, we need a GIST ID to store the commands in keep." ) prompt_str = 'If you already have a GitHub Gist created by keep, paste the GIST ID here, or else press Enter (we will create a Gist for you). ' new_gist_id = click.prompt(prompt_str, default='', show_default=False) if new_gist_id: gist_id = new_gist_id if not gist_id: g = Github(token) gist = g.get_user().create_gist( False, files={'commands.json': InputFileContent('{}')}, description='Backup for keep - https://github.com/OrkoHunter/keep') gist_id = gist.id with open(dir_path, 'w') as f: json.dump({'token': token, 'gist': gist_id}, f) click.echo("Done! Gist URL - https://gist.github.com/" + gist_id) click.echo( "Your token and gist ID has been stored inside ~/.keep/.credentials") click.echo("You can now use `keep push` or `keep pull`")
def UpdateGist(self: Any, source: Dict[str, Any]) -> None: """ Update a Gist for the authenticated GitHub user using the provided data source. """ filename: str = source["filename"] content: str = source["new"]["raw"] url: str = source["url"] gist: Gist = source["old"]["gist"] data: Dict[str, InputFileContent] = { filename: InputFileContent(content) } try: gist.edit(url, data) logger.success(f"Updated Gist {filename} ({url})") except Exception as e: logger.error(f"Failed to update Gist {filename} ({url}), {e}") logger.trace(content)
def generate_seed(self, permalink, generate_spoiler_log): seed_name = uuid.uuid4() file_name = "".join(random.choice(string.digits) for _ in range(6)) os.system( f"python sslib/ssrando.py -seed={seed_name} -permalink={permalink}" ) permalink_file_name = f"permalink_{seed_name}.txt" permalink_file = open(permalink_file_name, "r") permalink = permalink_file.read() permalink_file.close() if generate_spoiler_log: spoiler_log_file_name = f"SS Random {seed_name} - Spoiler Log.txt" spoiler_log_file = open(spoiler_log_file_name, "r") spoiler_log = spoiler_log_file.read() spoiler_log_file.close() timestamp = datetime.now().strftime("%Y-%m-%d-%H:%M:%S") gh = Github(self.github_token) gh_auth_user = gh.get_user() gist = gh_auth_user.create_gist( public=False, files={ f"spoiler_log_{timestamp}.txt": InputFileContent(spoiler_log) }, description="Skyward Sword Randomizer Spoiler Log") spoiler_log_url = gist.html_url else: spoiler_log_url = None return { "permalink": permalink, "file_name": file_name, "spoiler_log_url": spoiler_log_url }
def CreateGist(self: Any, source: Dict[str, Any]) -> None: """ Create a Gist for the authenticated GitHub user using the provided data source. """ filename: str = source["filename"] content: str = source["new"]["raw"] url: str = source["url"] public: bool = self.config["github"].get("public", False) try: data: Dict[str, InputFileContent] = { filename: InputFileContent(content) } self.git.get_user().create_gist(public, data, url) logger.success(f"Created Gist {filename} ({url})") except Exception as e: logger.error(f"Failed to create Gist {filename} ({url}), {e}") logger.trace(content)
async def create_gist(e): if not github: await e.edit("Github information has not been set up", delete_in=3) return filename = e.pattern_match.group(1) match = e.pattern_match.group(2) reply_message = await e.get_reply_message() if match: message = match.strip() elif reply_message: message = reply_message.message.strip() else: await e.edit("There's nothing to paste.") return await e.edit("`Sending paste to Github...`") user = github.get_user() file = InputFileContent(message) gist = user.create_gist(True, {filename: file}) await e.edit(f"Gist created. You can find it here {gist.html_url}.")
def submit_errors(self): # pylint: disable=too-many-branches,too-many-locals submitter_result = '' issue_id = None gh_credentials = (sickbeard.GIT_AUTH_TYPE == 0 and sickbeard.GIT_USERNAME and sickbeard.GIT_PASSWORD) \ or (sickbeard.GIT_AUTH_TYPE == 1 and sickbeard.GIT_TOKEN) if not all((gh_credentials, sickbeard.DEBUG, sickbeard.gh, classes.ErrorViewer.errors)): submitter_result = 'Please set your GitHub token or username and password in the config and enable debug. Unable to submit issue ticket to GitHub!' return submitter_result, issue_id try: from sickbeard.versionChecker import CheckVersion checkversion = CheckVersion() checkversion.check_for_new_version() commits_behind = checkversion.updater.get_num_commits_behind() except Exception: # pylint: disable=broad-except submitter_result = 'Could not check if your SickRage is updated, unable to submit issue ticket to GitHub!' return submitter_result, issue_id if commits_behind is None or commits_behind > 0: submitter_result = 'Please update SickRage, unable to submit issue ticket to GitHub with an outdated version!' return submitter_result, issue_id if self.submitter_running: submitter_result = 'Issue submitter is running, please wait for it to complete' return submitter_result, issue_id self.submitter_running = True try: # read log file __log_data = None if ek(os.path.isfile, self.log_file): with io.open(self.log_file, encoding='utf-8') as log_f: __log_data = log_f.readlines() for i in range(1, int(sickbeard.LOG_NR)): f_name = '{0}.{1:d}'.format(self.log_file, i) if ek(os.path.isfile, f_name) and (len(__log_data) <= 500): with io.open(f_name, encoding='utf-8') as log_f: __log_data += log_f.readlines() __log_data = list(reversed(__log_data)) # parse and submit errors to issue tracker for cur_error in sorted(classes.ErrorViewer.errors, key=lambda error: error.time, reverse=True)[:500]: try: title_error = ss(str(cur_error.title)) if not title_error or title_error == 'None': title_error = re.match( r'^[A-Z0-9\-\[\] :]+::\s*(.*)(?: \[[\w]{7}\])$', ss(cur_error.message)).group(1) if len(title_error) > 1000: title_error = title_error[0:1000] except Exception as err_msg: # pylint: disable=broad-except self.log( 'Unable to get error title : {0}'.format(ex(err_msg)), ERROR) title_error = 'UNKNOWN' gist = None regex = r'^({0})\s+([A-Z]+)\s+([0-9A-Z\-]+)\s*(.*)(?: \[[\w]{{7}}\])$'.format( cur_error.time) for i, data in enumerate(__log_data): match = re.match(regex, data) if match: level = match.group(2) if LOGGING_LEVELS[level] == ERROR: paste_data = ''.join(__log_data[i:i + 50]) if paste_data: gist = sickbeard.gh.get_user().create_gist( False, { 'sickrage.log': InputFileContent(paste_data) }) break else: gist = 'No ERROR found' try: locale_name = locale.getdefaultlocale()[1] except Exception: # pylint: disable=broad-except locale_name = 'unknown' if gist and gist != 'No ERROR found': log_link = 'Link to Log: {0}'.format(gist.html_url) else: log_link = 'No Log available with ERRORS:' msg = [ '### INFO', 'Python Version: **{0}**'.format(sys.version[:120].replace( '\n', '')), 'Operating System: **{0}**'.format(platform.platform()), 'Locale: {0}'.format(locale_name), 'Branch: **{0}**'.format(sickbeard.BRANCH), 'Commit: SickRage/SickRage@{0}'.format( sickbeard.CUR_COMMIT_HASH), log_link, '### ERROR', '```', cur_error.message, '```', '---', '_STAFF NOTIFIED_: @SickRage/owners @SickRage/moderators', ] message = '\n'.join(msg) title_error = '[APP SUBMITTED]: {0}'.format(title_error) repo = sickbeard.gh.get_organization( sickbeard.GIT_ORG).get_repo(sickbeard.GIT_REPO) reports = repo.get_issues(state='all') def is_ascii_error(title): # [APP SUBMITTED]: 'ascii' codec can't encode characters in position 00-00: ordinal not in range(128) # [APP SUBMITTED]: 'charmap' codec can't decode byte 0x00 in position 00: character maps to <undefined> return re.search( r'.* codec can\'t .*code .* in position .*:', title) is not None def is_malformed_error(title): # [APP SUBMITTED]: not well-formed (invalid token): line 0, column 0 return re.search( r'.* not well-formed \(invalid token\): line .* column .*', title) is not None ascii_error = is_ascii_error(title_error) malformed_error = is_malformed_error(title_error) issue_found = False for report in reports: if title_error.rsplit(' :: ')[-1] in report.title or \ (malformed_error and is_malformed_error(report.title)) or \ (ascii_error and is_ascii_error(report.title)): issue_id = report.number if not report.raw_data['locked']: if report.create_comment(message): submitter_result = 'Commented on existing issue #{0} successfully!'.format( issue_id) else: submitter_result = 'Failed to comment on found issue #{0}!'.format( issue_id) else: submitter_result = 'Issue #{0} is locked, check GitHub to find info about the error.'.format( issue_id) issue_found = True break if not issue_found: issue = repo.create_issue(title_error, message) if issue: issue_id = issue.number submitter_result = 'Your issue ticket #{0} was submitted successfully!'.format( issue_id) else: submitter_result = 'Failed to create a new issue!' if issue_id and cur_error in classes.ErrorViewer.errors: # clear error from error list classes.ErrorViewer.errors.remove(cur_error) except Exception: # pylint: disable=broad-except self.log(traceback.format_exc(), ERROR) submitter_result = 'Exception generated in issue submitter, please check the log' issue_id = None finally: self.submitter_running = False return submitter_result, issue_id
def submit_errors(self): if not (sickbeard.GIT_USERNAME and sickbeard.GIT_PASSWORD and sickbeard.DEBUG and len(classes.ErrorViewer.errors) > 0): self.log('Please set your GitHub username and password in the config and enable debug. Unable to submit issue ticket to GitHub!') return try: from versionChecker import CheckVersion checkversion = CheckVersion() needs_update = checkversion.check_for_new_version() commits_behind = checkversion.updater.get_num_commits_behind() except: self.log('Could not check if your SickRage is updated, unable to submit issue ticket to GitHub!') return if commits_behind is None or commits_behind > 0: self.log('Please update SickRage, unable to submit issue ticket to GitHub with an outdated version!') return if self.submitter_running: return 'RUNNING' self.submitter_running = True gh_org = sickbeard.GIT_ORG or 'SiCKRAGETV' gh_repo = 'sickrage-issues' gh = Github(login_or_token=sickbeard.GIT_USERNAME, password=sickbeard.GIT_PASSWORD, user_agent="SiCKRAGE") try: # read log file log_data = None if os.path.isfile(self.logFile): with ek.ek(codecs.open, *[self.logFile, 'r', 'utf-8']) as f: log_data = f.readlines() for i in range (1 , int(sickbeard.LOG_NR)): if os.path.isfile(self.logFile + "." + str(i)) and (len(log_data) <= 500): with ek.ek(codecs.open, *[self.logFile + "." + str(i), 'r', 'utf-8']) as f: log_data += f.readlines() log_data = [line for line in reversed(log_data)] # parse and submit errors to issue tracker for curError in sorted(classes.ErrorViewer.errors, key=lambda error: error.time, reverse=True)[:500]: try: title_Error = str(curError.title) if not len(title_Error) or title_Error == 'None': title_Error = re.match("^[A-Z0-9\-\[\] :]+::\s*(.*)$", ek.ss(str(curError.message))).group(1) # if len(title_Error) > (1024 - len(u"[APP SUBMITTED]: ")): # 1000 just looks better than 1007 and adds some buffer if len(title_Error) > 1000: title_Error = title_Error[0:1000] except Exception as e: self.log("Unable to get error title : " + sickbeard.exceptions.ex(e), ERROR) gist = None regex = "^(%s)\s+([A-Z]+)\s+([0-9A-Z\-]+)\s*(.*)$" % curError.time for i, x in enumerate(log_data): x = ek.ss(x) match = re.match(regex, x) if match: level = match.group(2) if reverseNames[level] == ERROR: paste_data = "".join(log_data[i:i+50]) if paste_data: gist = gh.get_user().create_gist(True, {"sickrage.log": InputFileContent(paste_data)}) break else: gist = 'No ERROR found' message = u"### INFO\n" message += u"Python Version: **" + sys.version[:120].replace('\n','') + "**\n" message += u"Operating System: **" + platform.platform() + "**\n" if not 'Windows' in platform.platform(): try: message += u"Locale: " + locale.getdefaultlocale()[1] + "\n" except: message += u"Locale: unknown" + "\n" message += u"Branch: **" + sickbeard.BRANCH + "**\n" message += u"Commit: SiCKRAGETV/SickRage@" + sickbeard.CUR_COMMIT_HASH + "\n" if gist and gist != 'No ERROR found': message += u"Link to Log: " + gist.html_url + "\n" else: message += u"No Log available with ERRORS: " + "\n" message += u"### ERROR\n" message += u"```\n" message += curError.message + "\n" message += u"```\n" message += u"---\n" message += u"_STAFF NOTIFIED_: @SiCKRAGETV/owners @SiCKRAGETV/moderators" title_Error = u"[APP SUBMITTED]: " + title_Error reports = gh.get_organization(gh_org).get_repo(gh_repo).get_issues(state="all") issue_found = False issue_id = 0 for report in reports: if title_Error == report.title: comment = report.create_comment(message) if comment: issue_id = report.number self.log('Commented on existing issue #%s successfully!' % issue_id ) issue_found = True break if not issue_found: issue = gh.get_organization(gh_org).get_repo(gh_repo).create_issue(title_Error, message) if issue: issue_id = issue.number self.log('Your issue ticket #%s was submitted successfully!' % issue_id ) # clear error from error list classes.ErrorViewer.errors.remove(curError) self.submitter_running = False return issue_id except Exception as e: self.log(sickbeard.exceptions.ex(e), ERROR) self.submitter_running = False
def program(): """1. Go to your Github Settings and create Personal access token 2. Define a name and choose the type of access you want to grant. 3. Copy the token and store it somewhere safe, as best practice I stored it as a env variable called GH_GIST_CREATE_TOKEN """ token = github_credentials.GH_GIST_CREATE_TOKEN gh = Github(token) print(gh.rate_limiting) me = gh.get_user() print(me) code = ''' from collections import namedtuple Repo = namedtuple('Repo', 'name stars forks') def get_repo_stats(user, n=5): """Takes a Github user object and returns the top n most popular repos by star count, skips forks.""" repos = [] for repo in user.get_repos(): if repo.fork: continue repos.append(Repo(name=repo.name, stars=repo.stargazers_count, forks=repo.forks_count)) return sorted(repos, key=lambda x: x.stars, reverse=True)[:n] ''' code2 = ''' import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from_addr = '*****@*****.**' to_addr = '*****@*****.**' bcc = ['*****@*****.**', '*****@*****.**', '*****@*****.**'] msg = MIMEMultipart() msg['From'] = from_addr msg['To'] = to_addr msg['Subject'] = 'New Releases' body = """ New Releases and Sales! Click the links below to check them out! """ msg.attach(MIMEText(body, 'plain')) smtp_server = smtplib.SMTP('smtp.gmail.com', 587) smtp_server.ehlo() smtp_server.starttls() smtp_server.login(' [email protected] ', ' <application id> ') text = msg.as_string() smtp_server.sendmail(from_addr, [to_addr] + bcc, text) smtp_server.quit() print('Email sent successfully') ''' code3 = ''' #python tip: zip() with star-arguments is great for transposing 2-D data: m = [(1, 2, 3), (4, 5, 6)] list(zip(*m)) [(1, 4), (2, 5), (3, 6)] def transpose_list_of_tuples(data): if isinstance(data, dict): data = data.items() transposed = list(zip(*data)) return transposed ''' # me.create_gist(True, # {"repo_stats.py": InputFileContent(code)}, # "Get GH user's most popular repos") me.create_gist( True, {"transpose.py": InputFileContent(code3)}, 'zip() with star-arguments is great for transposing 2-D data')
def create_gist(filename, content, description): return g_auth_user.create_gist( public = False, files = { filename: InputFileContent(content=content) }, description = description )
def backup_to_gist(user, filename): with open(os.path.abspath(filename)) as fp: content = fp.read() gist = user.create_gist(public=False, files={filename: InputFileContent(content)}) logger.warn("backup link: {}".format(gist.html_url))
def main(): '''Runs the main function. usage: pre_push.py [-h] [-n] [-u] [-a] Check Reddit's API public endpoints for changes ''' parser = argparse.ArgumentParser( description='Check dev/api endpoints for changes') parser.add_argument('-o', '--output', action='store', help='File to write endpoints to.', default='endpoints.json') parser.add_argument('-c', '--check', action='store_true', help='Check if there are any changes,', default=True) parser.add_argument('-e', '--existing', action='store', help='File to existing endpoints') parser.add_argument('-p', '--print', action='store_true', help='Print endpoints not in PRAW', default=False) parser.add_argument('-d', '--changes-dir', action='store', help='Dir to store changes', default='changes') args = parser.parse_args() existingEndpoints = args.existing outputFilename = args.output check = args.check printDiff = args.print changesDir = args.changes_dir changes = False file = None try: if existingEndpoints and isfile(existingEndpoints): with open(existingEndpoints) as f: old_api = f.read() existing = loads(old_api) else: old_api = gist.files['API.json'].content existing = loads(old_api) parsed = parseEndpoints() current_api = dumps(parsed, indent=4) if check: if existing != parsed: changes = True if not isdir(changesDir): mkdir(changesDir) currentChangesDir = datetime.now().strftime( '%Y-%m-%d_%H-%M-%S') revisions = listdir(changesDir) revisions.sort() mkdir(join(changesDir, currentChangesDir)) if len(revisions) > 1: previous_revision = revisions[-1] with open( join(changesDir, currentChangesDir, 'oldEndpoints.json'), 'w') as f: dump(existing, f, indent=4) with open( join(changesDir, currentChangesDir, 'oldEndpoints.json'), 'r') as f: old = f.readlines() with open( join(changesDir, currentChangesDir, 'newEndpoints.json'), 'w') as f: f.write(current_api) with open( join(changesDir, currentChangesDir, 'newEndpoints.json'), 'r') as f: new = f.readlines() log.info('Changes were detected!') diff_string = ''.join( list( unified_diff(old, new, fromfile=f'API_{previous_revision}.json', tofile=f'API_{currentChangesDir}.json'))) diff_file = io.BytesIO(diff_string.encode()) diff_file.seek(0) gist.edit( files={ 'API.json': InputFileContent(current_api), 'changes.diff': InputFileContent(diff_string) }) file = File(diff_file, f"API_{currentChangesDir}.diff") if outputFilename: with open(outputFilename, 'w') as f: f.write(current_api) if printDiff: printEndpointsNotInPRAW(parsed) except Exception as error: log.exception(error) return changes, file
def submit_errors(self): if not (sickbeard.GIT_USERNAME and sickbeard.GIT_PASSWORD and len(classes.ErrorViewer.errors) > 0): return gh_org = sickbeard.GIT_ORG or 'SiCKRAGETV' gh_repo = 'sickrage-issues' gh = Github(login_or_token=sickbeard.GIT_USERNAME, password=sickbeard.GIT_PASSWORD, user_agent="SiCKRAGE") try: # read log file log_data = None if self.logFile and os.path.isfile(self.logFile): with ek.ek(open, self.logFile) as f: log_data = f.readlines() log_data = [line for line in reversed(log_data)] # parse and submit errors to issue tracker for curError in sorted(classes.ErrorViewer.errors, key=lambda error: error.time, reverse=True)[:500]: if not curError.title: continue gist = None regex = "^(%s)\s*([A-Z]+)\s*(.+?)\s*\:\:\s*(.*)$" % curError.time for i, x in enumerate(log_data): x = ek.ss(x) match = re.match(regex, x) if match: level = match.group(2) if reverseNames[level] == ERROR: paste_data = "".join(log_data[i:i + 50]) if paste_data: gist = gh.get_user().create_gist( True, { "sickrage.log": InputFileContent(paste_data) }) break message = u"### INFO\n" message += u"Python Version: **" + sys.version[:120] + "**\n" message += u"Operating System: **" + platform.platform( ) + "**\n" message += u"Branch: **" + sickbeard.BRANCH + "**\n" message += u"Commit: SiCKRAGETV/SickRage@" + sickbeard.CUR_COMMIT_HASH + "\n" if gist: message += u"Link to Log: " + gist.html_url + "\n" message += u"### ERROR\n" message += u"```\n" message += curError.message + "\n" message += u"```\n" message += u"---\n" message += u"_STAFF NOTIFIED_: @SiCKRAGETV/owners @SiCKRAGETV/moderators" issue = gh.get_organization(gh_org).get_repo( gh_repo).create_issue( "[APP SUBMITTED]: " + str(curError.title), message) if issue: self.log( 'Your issue ticket #%s was submitted successfully!' % issue.number) # clear error from error list classes.ErrorViewer.errors.remove(curError) return issue except Exception as e: self.log(sickbeard.exceptions.ex(e), ERROR)