shorter.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env python
  2. # -*- coding: iso-8859-15 -*-
  3. """
  4. $Id$
  5. This file is part of the anontwi project, http://anontwi.03c8.net
  6. Copyright (c) 2012/2013/2014/2015 by psy <epsylon@riseup.net>
  7. anontwi is free software; you can redistribute it and/or modify it under
  8. the terms of the GNU General Public License as published by the Free
  9. Software Foundation version 3 of the License.
  10. anontwi is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  13. details.
  14. You should have received a copy of the GNU General Public License along
  15. with anontwi; if not, write to the Free Software Foundation, Inc., 51
  16. Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. x90.es - nopcode.org secure short links (allies)
  18. """
  19. import urllib, sys
  20. import pycurl
  21. from cStringIO import StringIO
  22. class ShortURLReservations(object):
  23. def __init__(self, service='x90.es'):
  24. self._service = service
  25. self._parse_shortener()
  26. self._extra = {}
  27. def _parse_shortener(self):
  28. """
  29. List of valid links shorterers
  30. """
  31. if self._service == 'x90.es' or not self._service:
  32. self._url = 'http://x90.es/api.php'
  33. self._par = 'url'
  34. self._method = 'post'
  35. def process_url(self, url, proxy):
  36. dest = urllib.urlencode({self._par: url})
  37. dest = dest + "&action=shorturl" # see x90.es API features
  38. # add some fake user-agent and referer
  39. user_agent = 'Mozilla/5.0 (Linux; U; Android 0.5; en-us)'
  40. referer = ''
  41. out = StringIO()
  42. c = pycurl.Curl()
  43. if self._method == 'post':
  44. c.setopt(c.POST, 1)
  45. c.setopt(c.POSTFIELDS, dest)
  46. #c.setopt(c.VERBOSE, True)
  47. target = self._url
  48. c.setopt(c.URL, target)
  49. c.setopt(c.FOLLOWLOCATION, 1)
  50. c.setopt(c.REFERER, referer)
  51. c.setopt(c.USERAGENT, user_agent)
  52. # try connection with proxy
  53. if proxy is None:
  54. pass
  55. else:
  56. c.setopt(c.PROXY, proxy)
  57. c.setopt(c.WRITEFUNCTION, out.write)
  58. try:
  59. c.perform()
  60. except Exception as e:
  61. if proxy is None:
  62. print "\n[Error] Something wrong connecting to short url service provider:", self._service, '[', e[1], '].' , "Aborting!...\n"
  63. else:
  64. print "\n[Error] Something wrong connecting to short url service provider:", self._service, "[ Couldn't connect to proxy ]." , "Aborting!...\n"
  65. sys.exit(2)
  66. shorturl = out.getvalue()
  67. return shorturl
  68. c.close()