This forum is in archive mode. You will not be able to post new content.

Author Topic: [Python] Gister: Upload your code to gist(github)  (Read 429 times)

0 Members and 1 Guest are viewing this topic.

Offline kenjoe41

  • Symphorophiliac Programmer
  • Administrator
  • Baron
  • *
  • Posts: 990
  • Cookies: 224
    • View Profile
[Python] Gister: Upload your code to gist(github)
« on: December 25, 2014, 07:42:23 PM »
Well, i am used to sitting around forums and enjoying all the gifts for xmas people provide. But i got too disappointed this time that no one actually had a cool interesting project to give back to the community except laze about the release of "The Interview" and other stuff.

Anyway, In the wake of waking  torrented version of "The Interview", i decided to put this together. I must admit the the script lacks alot of improvement both codewise, efficientwise and feature wise but atleast we have something for christmas from the community.
Things to add later is support for two-factor authentication and improve the code [Thanks @Psycho_coder for testing, cleaning and pointing afew things out].

Anyway, here is the code, please do with it whatever you must.
The script depends on Github API v3 python wrapper: Github3
so you will need to:
Code: (text) [Select]
pip install github3.pyNot much testing has been done. If you find any bugs, hit me up.

Change log, v0.3:
Added:
  • Two-factor authentication
  • Changed to using argparse
  • Portability between Python 2 and 3
  • Fixed afew errors.
Features to add:
   
  • Support for token use.

Code: (python) [Select]
from __future__ import print_function
from io import open

from github3 import login, create_gist
import argparse
import sys
import os

try:
    # Python 2
    prompt = raw_input
except NameError:
    # Python 3
    prompt = input

def two_factor_function():
    code = ''
    while not code:
        # The user could accidentally press Enter before being ready,
        # let's protect them from doing that.
      code = prompt('Enter 2FA code: ')
    return code

def usage(argv0):
    print ("\tusage: %s [-h] -u <username> -p <password> -f <input filename> -d <description>\n"%argv0, file=sys.stdout)
    print ("\tusage: %s -f <input filename> -d <description>\n"%argv0, file=sys.stdout)

def is_valid_file(parser,arg):
    if not os.path.exists(arg):
        parser.error("File %s not found"%arg)
    else:
        return arg

def read_file(infilename):
    try:
        infile = open(infilename, 'r')
        content = infile.read()
        infile.close()
        return content
    except IOError as err:
print("Error reading file: %s"%str(err), file=sys.stderr)


def gist_up(args):
    basename = os.path.basename(args.filename)
    files = {
        basename: {
            'content': read_file(args.filename)
        }
    }

    if args.user and args.passwd:
        gh = login(args.user, args.passwd)
        gist = gh.create_gist(args.description+"--uploaded with Gister.py", files, public=args.private)
        return gist.html_url
    elif args.user and args.passwd and args.two-factor:
gh = login(args.user, args.passwd, two_factor_callback=two_factor_function)
        gist = gh.create_gist(args.description+"--uploaded with Gister.py", files, public=args.private)
        return gist.html_url

    else:  # Anonymous gist, public by default.
        gist = create_gist(args.description+"--uploaded with Gister.py", files)
        return gist.html_url


def main(argc, argv):
    parser = argparse.ArgumentParser(description='Upload code/text files to Gist on Github.')
    parser.add_argument("-i", "--input", dest="filename", required=True,
    help="Input code/text file to upload to Gist",
    metavar="FILENAME",type=lambda x:is_valid_file(parser,x))
    parser.add_argument("-u", "--user", required=False, help="Username for Github for non-anonymous uploads.")
    parser.add_argument("-p", "--passwd", required=False, help="Password for Github for non-anonymous uploads.")
    parser.add_argument("-d", "--description", required=True, help="Description of the gist.")
    parser.add_argument("--two-factor", required=False, action='store_true', help="Enable two-factor authentication.")
    parser.add_argument("--private", required=False, action='store_true',
default=False, help="Create a private gist, default if public")
    args = parser.parse_args()

    print("Uploading file %s to gist..."%args.filename, end='\n', file=sys.stdout)
    gist_url = gist_up(args)
    print("File Gisted and Available at : %s" %
          gist_url, end='\n', file=sys.stdout)


if __name__ == "__main__":
    sys.exit(main(len(sys.argv), sys.argv))
« Last Edit: December 30, 2014, 05:07:42 PM by kenjoe41 »
If you can't explain it to a 6 year old, you don't understand it yourself.
http://upload.alpha.evilzone.org/index.php?page=img&img=GwkGGneGR7Pl222zVGmNTjerkhkYNGtBuiYXkpyNv4ScOAWQu0-Y8[<NgGw/hsq]>EvbQrOrousk[/img]

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.