sharedby.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from terroroftinytown.services.base import BaseService
  2. from terroroftinytown.services.rand import HashRandMixin
  3. from terroroftinytown.client import VERSION
  4. from terroroftinytown.client.errors import UnexpectedNoResult
  5. import re
  6. from terroroftinytown.services.status import URLStatus
  7. class SharedByService(BaseService):
  8. def __init__(self, *args, **kwargs):
  9. BaseService.__init__(self, *args, **kwargs)
  10. self.user_agent = (
  11. 'Mozilla/5.0 (Windows NT 6.1; WOW64) '
  12. 'AppleWebKit/537.36 (KHTML, like Gecko) '
  13. 'Chrome/39.0.2171.95 Safari/537.36 '
  14. 'Nintendu/64 (URLTeam {0})'
  15. ).format(VERSION)
  16. def process_redirect_body(self, response):
  17. try:
  18. return BaseService.process_redirect_body(self, response)
  19. except UnexpectedNoResult:
  20. if 'cakeErr1-context' not in response.text:
  21. raise
  22. match = re.search(
  23. r'"Location: (.*)"</pre>', response.text, re.DOTALL
  24. )
  25. if not match:
  26. raise
  27. link = match.group(1)
  28. # Python's urllib escapes too much. We'll escape the bare minimum
  29. link = link.replace('\n', '%0A')
  30. link = link.replace('\r', '%0D')
  31. return (URLStatus.ok, link, response.encoding)
  32. class SharedBy6Service(HashRandMixin, SharedByService):
  33. def get_shortcode_width(self):
  34. return 6