├── PKGBUILD ├── README └── pygist.py /PKGBUILD: -------------------------------------------------------------------------------- 1 | # Contributor: Matt Kemp 2 | 3 | pkgname=pygist-git 4 | pkgver=20110305 5 | pkgrel=1 6 | pkgdesc="Python command line interface with gist.github.com" 7 | url="http://github.com/mattikus/pygist/tree/master" 8 | arch=('any') 9 | license=('MIT') 10 | depends=('python') 11 | optdepends=('git: utilizes git-config to gather user information for github' 12 | 'xclip: will yank pastes to clipboard automagically') 13 | makedepends=('git') 14 | conflicts=('pygist') 15 | replaces=('pygist') 16 | 17 | _gitroot="git://github.com/mattikus/pygist.git" 18 | _gitname="pygist" 19 | 20 | build() { 21 | cd "$srcdir" 22 | msg "Connecting to GIT server...." 23 | 24 | if [ -d $_gitname ]; then 25 | cd $_gitname && git pull origin 26 | msg "The local files are updated." 27 | else 28 | git clone $_gitroot $_gitname 29 | fi 30 | 31 | msg "GIT checkout done or server timeout" 32 | } 33 | 34 | package() { 35 | cd "$srcdir/$_gitname" 36 | 37 | install -m755 pygist.py -D "$pkgdir/usr/bin/pygist" 38 | } 39 | 40 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Python command line client for gist.github.com 2 | 3 | Based on Chris Wanstrath's ruby gist client: 4 | http://github.com/defunkt/gist/tree/master 5 | 6 | Install: 7 | wget http://github.com/mattikus/pygist/raw/master/pygist.py -O ~/bin/pygist 8 | 9 | Usage: 10 | cat file.txt | pygist 11 | pygist file1 file2 file3 file4 12 | pygist -p file1 13 | echo 'hello world' | pygist -a -d 'Hello World' 14 | pygist -g 1234 > something.txt 15 | 16 | Bugs: 17 | No known bugs, feel free to email me at matt@mattikus.com if you find any. 18 | -------------------------------------------------------------------------------- /pygist.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """ 3 | Python command line client for gist.github.com 4 | 5 | Based on Chris Wanstrath's ruby gist client: 6 | http://github.com/defunkt/gist/tree/master 7 | 8 | Basic usage: 9 | cat file.txt | pygist 10 | pygist file1 file2 file3 file4 11 | pygist -p file1 12 | echo 'hello world' | pygist -a 13 | pygist -g 1234 > something.txt 14 | 15 | """ 16 | __author__ = 'Matt Kemp ' 17 | __version__ = 'pygist 2.0 3/5/11' 18 | __license__ = """ 19 | Copyright (c) 2011 Matt Kemp 20 | 21 | Permission is hereby granted, free of charge, to any person obtaining a copy 22 | of this software and associated documentation files (the "Software"), to deal 23 | in the Software without restriction, including without limitation the rights 24 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 25 | copies of the Software, and to permit persons to whom the Software is 26 | furnished to do so, subject to the following conditions: 27 | 28 | The above copyright notice and this permission notice shall be included in 29 | all copies or substantial portions of the Software. 30 | 31 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 32 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 33 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 34 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 35 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 36 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 37 | THE SOFTWARE. 38 | """ 39 | 40 | import os 41 | import sys 42 | import subprocess 43 | 44 | from argparse import ArgumentParser, RawDescriptionHelpFormatter 45 | from urllib.request import urlopen 46 | from urllib.parse import urlencode 47 | 48 | site = 'https://gist.github.com/gists' 49 | 50 | def get_gh_login(): 51 | cmd = subprocess.getoutput('which git') 52 | if not cmd: 53 | return 54 | 55 | user = subprocess.getoutput('{0} config --global github.user'.format(cmd)) 56 | token = subprocess.getoutput('{0} config --global github.token'.format(cmd)) 57 | 58 | return user, token 59 | 60 | def gen_request(files, private, anon, description): 61 | i = 0 62 | data = {} 63 | 64 | if not anon: 65 | user, token = get_gh_login() 66 | if all((user, token)): 67 | data['login'] = user 68 | data['token'] = token 69 | 70 | if private: 71 | data['private'] = 'on' 72 | 73 | if description: 74 | data['description'] = description 75 | 76 | for filename in files: 77 | i += 1 78 | if filename is sys.stdin: 79 | filename = '' 80 | extension = '' 81 | contents = sys.stdin.read() 82 | else: 83 | if not os.path.isfile(filename): 84 | print("'{0}' does not exist or is not a regular file".format(filename)) 85 | sys.exit() 86 | extension = os.path.splitext(filename)[1] 87 | with open(filename) as file: 88 | contents = file.read() 89 | 90 | data['file_ext[gistfile{0:d}]'.format(i)] = extension 91 | data['file_name[gistfile{0:d}]'.format(i)] = os.path.basename(filename) 92 | data['file_contents[gistfile{0:d}]'.format(i)] = contents 93 | 94 | 95 | return urlencode(data).encode('utf8') 96 | 97 | def get_gist(id): 98 | url = 'https://gist.github.com/%s.txt' % id 99 | with urlopen(url) as info: 100 | data = info.read() 101 | sys.stdout.write(data.decode('utf8')) 102 | 103 | def copy_paste(url): 104 | cmd = None 105 | if sys.platform == 'darwin': 106 | cmd = cmd or subprocess.getoutput('which pbcopy') 107 | cmd = cmd or subprocess.getoutput('which xclip') 108 | if cmd: 109 | output = subprocess.Popen(cmd, stdin=subprocess.PIPE) 110 | output.stdin.write(url) 111 | output.stdin.close() 112 | return True 113 | else: 114 | return False 115 | 116 | def main(): 117 | parser = ArgumentParser(description=__doc__, 118 | formatter_class=RawDescriptionHelpFormatter) 119 | parser.add_argument('-v', '--version', action='version', version=__version__) 120 | parser.add_argument('-g', dest='gist_id', 121 | help='retreive a paste identified by the gist id') 122 | parser.add_argument('-d', dest='description', 123 | help='description of the gist') 124 | parser.add_argument('-p', dest='private', action='store_true', 125 | help='set for private gist') 126 | parser.add_argument('-a', dest='anon', action='store_true', 127 | help='set for anonymous gist') 128 | parser.add_argument('file', nargs='*', help='file to paste to gist') 129 | args = parser.parse_args() 130 | 131 | if args.gist_id: 132 | get_gist(args.gist_id) 133 | sys.exit() 134 | 135 | if sys.stdin.isatty() and not args.file: 136 | parser.print_help() 137 | sys.exit(1) 138 | 139 | if len(args.file) < 1: 140 | data = gen_request([sys.stdin], args.private, args.anon, args.description) 141 | else: 142 | data = gen_request(args.file, args.private, args.anon, args.description) 143 | 144 | with urlopen(site, data) as info: 145 | url = info.geturl() 146 | 147 | if copy_paste(url.encode('utf8')): 148 | print('{0} | copied to clipboard successfully.'.format(url)) 149 | else: 150 | print('{0}'.format(url)) 151 | 152 | if __name__ == '__main__': 153 | main() 154 | --------------------------------------------------------------------------------