#!/usr/bin/python


'''
- Cmdfoornd v.alpha 2016-11-18 by hexufo.

Get a random command from commandlinefu.com.

Get the latest version here:
https://github.com/hexufo/misc

Project page on PyPI: https://pypi.python.org/pypi/misc-scripts

You can find me in various places on the internet:
hexufo @ {wordpress, twitter, github, gmail}

- This software is distributed under the Unlicense:

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org>
'''


from random import randrange
import urllib2


useragents = [
  'curl/7.10.x (i386-redhat-linux-gnu) libcurl/7.10.x OpenSSL/0.9.7a ipv6 zlib/1.1.4',
  'curl/7.7.x (i386--freebsd4.3) libcurl 7.7.x (SSL 0.9.6) (ipv6 enabled)',
  'curl/7.8 (i686-pc-linux-gnu) libcurl 7.8 (OpenSSL 0.9.6)',
  'curl/7.9.x (win32) libcurl 7.9.x']


try:
  url = "http://www.commandlinefu.com/commands/random/plaintext"
  agentrnd = randrange(0,len(useragents))
  # req = urllib2.Request(url) # 2016-11-18: This used to work fine. Now they require curl ? Why ?
  req = urllib2.Request(url, headers={'User-Agent': useragents[agentrnd]}) # Ok. I am curl...
  response = urllib2.urlopen(req)
  page = response.read()

  print page[:-1]
except Exception, error_message:
  print "[-] Error: " + str(error_message)
  exit(1)


exit(0)


