Advertisement
ijontichy

main.py

Nov 26th, 2011
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import sys, os
  5. from src import pastebinfuncs, pastebininfo, pastebinrequest
  6.  
  7. USAGEMESSAGE = "usage: {} <file>".format(os.path.basename(sys.argv[0]) )
  8.  
  9. def usage():
  10.     print(USAGEMESSAGE, file=sys.stderr)
  11.  
  12. def usageExit():
  13.     usage()
  14.     sys.exit()
  15.  
  16. def errorExit(message):
  17.     print("ERROR: " + message, file=sys.stderr)
  18.     usageExit()
  19.  
  20. def main(args):
  21.  
  22.     if len(args) <= 1:
  23.         errorExit("no file specified")
  24.         return  # just in case
  25.  
  26.     pbFile = args[1]
  27.  
  28.     apiKey, userKey = pastebinfuncs.getAPIKeys()
  29.  
  30.     if apiKey is None:
  31.         print("Adding API key to {}.".format(pastebinfuncs.KEYDIR) )
  32.         print("You can get your API key at \"http://http://pastebin.com/api\".")
  33.         while not apiKey:
  34.             apiKey = input("API key: ")
  35.  
  36.         keyFile = open(pastebinfuncs.replaceHome(pastebinfuncs.KEYDIR), "a")
  37.         keyFile.write("apikey = {}\n".format(apiKey))
  38.         keyFile.close()
  39.  
  40.     if userKey is None:
  41.         print("Adding user key to {}.".format(pastebinfuncs.KEYDIR) )
  42.         userKey = pastebinfuncs.loginPastebin(apiKey, write=True)
  43.  
  44.     # by now, we should have a user and API key
  45.  
  46.     try:
  47.         pbInfo = pastebininfo.PastebinInfo.fromFile(pbFile, key=apiKey, userKey=userKey)
  48.  
  49.     except IOError:
  50.         errorExit("\"{}\" does not exist".format(pbFile) )
  51.         return
  52.  
  53.     pbRequ = pastebinrequest.PastebinRequest(pbInfo)
  54.  
  55.     try:
  56.         url = pbRequ.getPage()
  57.  
  58.     except pastebinrequest.PastebinError:
  59.         exc = sys.exc_info()
  60.         errorExit("{}".format(exc[1]))
  61.         return
  62.  
  63.     print(url)
  64.  
  65. if __name__ == '__main__':
  66.     main(sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement