isbn2bibtex.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/python
  2. # Copyright FlintFam Systems Management, 2013.
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. import sys
  17. import urllib
  18. import urllib2
  19. import json
  20. import random
  21. import re
  22. accesskey = "XXXXXXXX"
  23. jsonreq = "http://isbndb.com/api/v2/json/" + accesskey + "/books/?i=combined&q="
  24. qstring = ""
  25. for arg in sys.argv:
  26. qstring = qstring + " " + arg
  27. query = urllib.quote_plus(qstring)
  28. queryURL = jsonreq + query
  29. jsondata = urllib2.urlopen(queryURL).read()
  30. data = json.loads(jsondata)
  31. for book in data['data']:
  32. if book['author_data']:
  33. authorid = book['author_data'][0]['id']
  34. authorlast = re.split('_', authorid)
  35. rand = random.randint(0, 3000)
  36. bibtexkey = authorlast[0] + str(rand)
  37. authoritem = ""
  38. first = 1
  39. for author in book['author_data']:
  40. authorname = author['name']
  41. if first == 0:
  42. authoritem = authoritem + " and " + authorname
  43. else:
  44. authoritem = authorname
  45. first = 0
  46. titleitem = book['title_latin']
  47. publisher = book['publisher_name']
  48. entry = "@Book{" + bibtexkey + ",\nauthor = \"{" + authoritem + "}\",\ntitle = \"{" + titleitem + "}\",\npublisher = \"{" + publisher + "}\"\n}\n\n"
  49. print(entry)