ijontichy

pastebin

Aug 12th, 2014
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3.  
  4. import argparse
  5. import sys
  6. import os
  7.  
  8. from src import pastebinfuncs, pastebininfo, pastebinrequest
  9.  
  10. USAGEMESSAGE = "usage: {} <file>".format(os.path.basename(sys.argv[0]) )
  11.  
  12. expireChoices = ["10M", "1H", "1D", "1M", "N"]
  13.  
  14. def errorExit(message):
  15. print("ERROR: " + message, file=sys.stderr)
  16. sys.exit(1)
  17.  
  18. def main(args):
  19.  
  20. streamMode = False
  21.  
  22. if args.file is None:
  23. streamMode = True
  24.  
  25. apiKey, userKey = pastebinfuncs.getAPIKeys()
  26.  
  27. if apiKey is None:
  28. print("Adding API key to {}.".format(pastebinfuncs.KEYDIR) )
  29. print("You can get your API key at \"http://pastebin.com/api\".")
  30. while not apiKey:
  31. apiKey = input("API key: ")
  32.  
  33. keyFile = open(pastebinfuncs.replaceHome(pastebinfuncs.KEYDIR), "a")
  34. keyFile.write("apikey = {}\n".format(apiKey))
  35. keyFile.close()
  36.  
  37. if userKey is None:
  38. print("Adding user key to {}.".format(pastebinfuncs.KEYDIR) )
  39. userKey = pastebinfuncs.loginPastebin(apiKey, write=True)
  40.  
  41. # by now, we should have a user and API key
  42.  
  43. if streamMode:
  44. pbBuffer = []
  45. pbString = ""
  46.  
  47. try:
  48. while 1:
  49. pbBuffer.append(input())
  50.  
  51. except KeyboardInterrupt:
  52. print()
  53. return 1
  54.  
  55. except EOFError:
  56. if pbBuffer:
  57. pbString = "\n".join(pbBuffer)
  58. else:
  59. return 0
  60.  
  61. pbInfo = pastebininfo.PastebinInfo(text=pbString, name="<stdin>", key=apiKey, userKey=userKey)
  62.  
  63. else:
  64. pbFile = args.file
  65.  
  66. try:
  67. pbInfo = pastebininfo.PastebinInfo.fromFile(pbFile, key=apiKey, userKey=userKey)
  68.  
  69. except IOError:
  70. errorExit("\"{}\" does not exist".format(pbFile) )
  71. return 1
  72.  
  73.  
  74. if args.name is not None:
  75. pbInfo.name = args.name
  76.  
  77. if args.private:
  78. pbInfo.private = 2
  79. elif args.unlisted:
  80. pbInfo.private = 1
  81. else:
  82. pbInfo.private = 0
  83.  
  84. pbInfo.private = str(pbInfo.private)
  85.  
  86. args.expire = args.expire.upper()
  87.  
  88. if args.expire not in expireChoices:
  89. errorExit("invalid expiration choice \"{}\"".format(args.expire))
  90. else:
  91. pbInfo.expire = args.expire
  92.  
  93. #print(pbInfo)
  94. pbRequ = pastebinrequest.PastebinRequest(pbInfo)
  95.  
  96. try:
  97. url = pbRequ.getPage()
  98.  
  99. except pastebinrequest.PastebinError:
  100. exc = sys.exc_info()
  101. errorExit("{}".format(exc[1]))
  102. return
  103.  
  104. print(url)
  105.  
  106. if __name__ == '__main__':
  107. #print(sys.argv)
  108.  
  109. argp = argparse.ArgumentParser("pastebin")
  110. argp.add_argument("file", nargs="?", type=str, help="file to paste (optional)")
  111. argp.add_argument("-n", "--name", help="name of paste", type=str)
  112. argp.add_argument("-e", "--expire", help="when paste expires ({})".format(", ".join(expireChoices)), type=str, default=expireChoices[-1])
  113. argp.add_argument("-p", "--private", help="determines if paste is private", action='store_true')
  114. argp.add_argument("-u", "--unlisted", help="determines if paste listed", action='store_true')
  115.  
  116. args = argp.parse_args()
  117.  
  118. exit = main(args)
  119.  
  120. if exit and isinstance(exit, int):
  121. sys.exit(exit)
Advertisement
Add Comment
Please, Sign In to add comment