CommandLineFu with Pytho


SUBMITTED BY: alemotta

DATE: March 14, 2017, 4:33 p.m.

FORMAT: Text only

SIZE: 4.9 kB

HITS: 140

  1. CommandLineFu with Python
  2. Overview
  3. One of the best methods to practice Python coding is to study some code and try
  4. them out yourself.
  5. By doing a lot of code exercises, you will get a much better understanding of what
  6. it really does.
  7. In other words, learning by doing.
  8. To help you improve your Python coding skill, I've created a program below which
  9. is using the API from CommandLineFu.com
  10. CommandLineFu API
  11. A common first step when you want to use a Web-based services is to see if they
  12. have an API.
  13. Luckily for me, Commandlinefu.com provides one and can be found here
  14. "
  15. The content of commandlinefu.com is available in a variety of different formats
  16. for you to do what you like with.
  17. Any page which contains a list of commands (such as the listings by tag, function
  18. or user) can be returned in a format of your choice through a simple change to the
  19. request URL."
  20. The example URL that they provide is:
  21. http://www.commandlinefu.com/commands/'command-set'/'format'/
  22. where:
  23. command-set
  24. is the URL component which specifies which set of commands to return.
  25. Possible values are:
  26. browse/sort-by-votes
  27. tagged/163/grep
  28. matching/ssh/c3No
  29. format
  30. is one of the following: plaintext, json and rss.
  31. I prefer using the json format and that is also what I'm using in the program
  32. below.
  33. Creating the "Command Line Search Tool"
  34. I think we have all the API information we need, so let's get to it.
  35. The program is well documented and should be straightforward.
  36. Open up a text editor, copy & paste the code below.
  37. Save the file as: "commandlinefu.py" and exit the editor.
  38. #!/usr/bin/env python27
  39. import urllib2
  40. import base64
  41. import json
  42. import os
  43. import sys
  44. import re
  45. os.system("clear")
  46. print "-" * 80
  47. print "Command Line Search Tool"
  48. print "-" * 80
  49. def Banner(text):
  50. print "=" * 70
  51. print text
  52. print "=" * 70
  53. sys.stdout.flush()
  54. def sortByVotes():
  55. Banner('Sort By Votes')
  56. url = "http://www.commandlinefu.com/commands/browse/sort-by-votes/json"
  57. request = urllib2.Request(url)
  58. response = json.load(urllib2.urlopen(request))
  59. #print json.dumps(response,indent=2)
  60. for c in response:
  61. print "-" * 60
  62. print c['command']
  63. def sortByVotesToday():
  64. Banner('Printing All commands the last day (Sort By Votes) ')
  65. url = "http://www.commandlinefu.com/commands/browse/last-day/sort-by-votes/json"
  66. request = urllib2.Request(url)
  67. response = json.load(urllib2.urlopen(request))
  68. for c in response:
  69. print "-" * 60
  70. print c['command']
  71. def sortByVotesWeek():
  72. Banner('Printing All commands the last week (Sort By Votes) ')
  73. url = "http://www.commandlinefu.com/commands/browse/last-week/sort-by-votes/json"
  74. request = urllib2.Request(url)
  75. response = json.load(urllib2.urlopen(request))
  76. for c in response:
  77. print "-" * 60
  78. print c['command']
  79. def sortByVotesMonth():
  80. Banner('Printing: All commands from the last months (Sorted By Votes) ')
  81. url = "http://www.commandlinefu.com/commands/browse/last-month/sort-by-votes/json"
  82. request = urllib2.Request(url)
  83. response = json.load(urllib2.urlopen(request))
  84. for c in response:
  85. print "-" * 60
  86. print c['command']
  87. def sortByMatch():
  88. #import base64
  89. Banner("Sort By Match")
  90. match = raw_input("Please enter a search command: ")
  91. bestmatch = re.compile(r' ')
  92. search = bestmatch.sub('+', match)
  93. b64_encoded = base64.b64encode(search)
  94. url = "http://www.commandlinefu.com/commands/matching/" + search + "/" + b64_encoded + "/json"
  95. request = urllib2.Request(url)
  96. response = json.load(urllib2.urlopen(request))
  97. for c in response:
  98. print "-" * 60
  99. print c['command']
  100. print """
  101. 1. Sort By Votes (All time)
  102. 2. Sort By Votes (Today)
  103. 3. Sort by Votes (Week)
  104. 4. Sort by Votes (Month)
  105. 5. Search for a command
  106. Press enter to quit
  107. """
  108. while True:
  109. answer = raw_input("What would you like to do? ")
  110. if answer == "":
  111. sys.exit()
  112. elif answer == "1":
  113. sortByVotes()
  114. elif answer == "2":
  115. print sortByVotesToday()
  116. elif answer == "3":
  117. print sortByVotesWeek()
  118. elif answer == "4":
  119. print sortByVotesMonth()
  120. elif answer == "5":
  121. print sortByMatch()
  122. else:
  123. print "Not a valid choice"
  124. When you run the program, you will be presented with a menu in which you can make
  125. your choices.
  126. --------------------------------------------------------------------------------
  127. Command Line Search Tool
  128. --------------------------------------------------------------------------------
  129. 1. Sort By Votes (All time)
  130. 2. Sort By Votes (Today)
  131. 3. Sort by Votes (Week)
  132. 4. Sort by Votes (Month)
  133. 5. Search for a command
  134. Press enter to quit
  135. What would you like to do?
  136. ...
  137. ...

comments powered by Disqus