avatars.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # -*- coding: utf-8 -*-
  2. """
  3. Author: Bernhard Scheirle
  4. """
  5. from __future__ import unicode_literals
  6. import logging
  7. import os
  8. import hashlib
  9. logger = logging.getLogger(__name__)
  10. _log = "pelican_comment_system: avatars: "
  11. try:
  12. from . identicon import identicon
  13. _identiconImported = True
  14. except ImportError as e:
  15. logger.warning(_log + "identicon deactivated: " + str(e))
  16. _identiconImported = False
  17. # Global Variables
  18. _identicon_save_path = None
  19. _identicon_output_path = None
  20. _identicon_data = None
  21. _identicon_size = None
  22. _initialized = False
  23. _authors = None
  24. _missingAvatars = []
  25. def _ready():
  26. if not _initialized:
  27. logger.warning(_log + "Module not initialized. use init")
  28. if not _identicon_data:
  29. logger.debug(_log + "No identicon data set")
  30. return _identiconImported and _initialized and _identicon_data
  31. def init(pelican_output_path, identicon_output_path, identicon_data,
  32. identicon_size, authors):
  33. global _identicon_save_path
  34. global _identicon_output_path
  35. global _identicon_data
  36. global _identicon_size
  37. global _initialized
  38. global _authors
  39. global _missingAvatars
  40. _identicon_save_path = os.path.join(pelican_output_path,
  41. identicon_output_path)
  42. _identicon_output_path = identicon_output_path
  43. _identicon_data = identicon_data
  44. _identicon_size = identicon_size
  45. _authors = authors
  46. _missingAvatars = []
  47. _initialized = True
  48. def _createIdenticonOutputFolder():
  49. if not _ready():
  50. return
  51. if not os.path.exists(_identicon_save_path):
  52. os.makedirs(_identicon_save_path)
  53. def getAvatarPath(comment_id, metadata):
  54. if not _ready():
  55. return ''
  56. md5 = hashlib.md5()
  57. author = tuple()
  58. for data in _identicon_data:
  59. if data in metadata:
  60. string = "{}".format(metadata[data])
  61. md5.update(string.encode('utf-8'))
  62. author += tuple([string])
  63. else:
  64. logger.warning(_log + data +
  65. " is missing in comment: " + comment_id)
  66. if author in _authors:
  67. return _authors[author]
  68. global _missingAvatars
  69. code = md5.hexdigest()
  70. if not code in _missingAvatars:
  71. _missingAvatars.append(code)
  72. return os.path.join(_identicon_output_path, '%s.png' % code)
  73. def generateAndSaveMissingAvatars():
  74. _createIdenticonOutputFolder()
  75. global _missingAvatars
  76. for code in _missingAvatars:
  77. avatar_path = '%s.png' % code
  78. avatar = identicon.render_identicon(int(code, 16), _identicon_size)
  79. avatar_save_path = os.path.join(_identicon_save_path, avatar_path)
  80. avatar.save(avatar_save_path, 'PNG')
  81. _missingAvatars = []