PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/bibim/ir/types.py

https://github.com/rxuriguera/bibtexIndexMaker
Python | 87 lines | 44 code | 9 blank | 34 comment | 0 complexity | 7a48afb2e488e6f50873c916be79f920 MD5 | raw file
  1. # Copyright 2010 Ramon Xuriguera
  2. #
  3. # This file is part of BibtexIndexMaker IR. The code in this file is
  4. # largely based on Peteris Krumins' python library for google search
  5. # wich is licensed under MIT license.
  6. #
  7. # Peteris Krumins (peter@catonmat.net)
  8. # http://www.catonmat.net -- good coders code, great reuse
  9. # http://www.catonmat.net/blog/python-library-for-google-search/
  10. #
  11. # BibtexIndexMaker IR is free software: you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation, either version 3 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # BibtexIndexMaker IR is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU General Public License
  22. # along with BibtexIndexMaker IR. If not, see <http://www.gnu.org/licenses/>.
  23. class SearchError(Exception):
  24. """
  25. Base class for search exceptions.
  26. """
  27. def __init__(self, error):
  28. self.error = error
  29. class ParseError(SearchError):
  30. """
  31. Parse error in search results.
  32. self.msg attribute contains explanation why parsing failed
  33. self.tag attribute contains BeautifulSoup object with the most relevant tag
  34. that failed to parse
  35. Thrown only in debug mode
  36. """
  37. def __init__(self, msg, tag):
  38. self.msg = msg
  39. self.tag = tag
  40. def __str__(self):
  41. return self.msg
  42. def html(self):
  43. return self.tag.prettify()
  44. class SearchResult(object):
  45. """
  46. """
  47. def __init__(self, title="", url=""):
  48. self.title = title
  49. self.url = url
  50. def __repr__(self):
  51. return ('Search Result(title: "%s..., url: %s...)"' %
  52. (self.title[0:20], self.url[0:20]))
  53. @property
  54. def base_url(self):
  55. if self.url.startswith('http://'):
  56. return 'http://' + self.url.split('/')[2]
  57. else:
  58. return self.url.rsplit('/', 1)[0]
  59. class DescSearchResult(SearchResult):
  60. """
  61. """
  62. def __init__(self, title, url, desc):
  63. super(DescSearchResult, self).__init__(title, url)
  64. self.desc = desc
  65. class ScholarSearchResult(SearchResult):
  66. """
  67. """
  68. def __init__(self, title, url, desc, authors=[], year=None, base=None):
  69. super(ScholarSearchResult, self).__init__(title, url)
  70. self.desc = desc
  71. self.authors = authors
  72. self.year = year