fork download
  1. import os
  2. import sys
  3.  
  4. import django
  5. from django.core.files.storage import default_storage
  6. from django.core.files.base import ContentFile
  7. from django.db.utils import IntegrityError
  8.  
  9. os.environ["DJANGO_SETTINGS_MODULE"] = "WillBeams.settings"
  10. sys.path.append("WillBeams/")
  11.  
  12. # os.environ["DJANGO_SETTINGS_MODULE"] = "valiz.settings"
  13. # sys.path.append("valiz/")
  14.  
  15. django.setup()
  16. from will_beams import models
  17. import scraper
  18.  
  19.  
  20. class DownloadToModel(scraper.Downloader):
  21.  
  22. def __init__(self, *args, **kwargs):
  23. self._webm_obj = None
  24. super().__init__(*args, **kwargs)
  25.  
  26. def _download(self, data):
  27. md5 = data[-1]
  28. try:
  29. models.Webm.increase_rating(md5)
  30. result = None, None, None
  31. except models.Webm.DoesNotExist:
  32. self._webm_obj = models.Webm(md5=md5)
  33. result = super()._download(data)
  34.  
  35. return result
  36.  
  37. def save(self, filename, webm, thumb):
  38. if not filename:
  39. return
  40.  
  41. if webm:
  42. filename += '.webm'
  43. path = os.path.join(
  44. models.get_media_folder(self._webm_obj, filename), filename)
  45. webm_path = default_storage.save(path, ContentFile(webm))
  46. self._webm_obj.video = webm_path
  47.  
  48. if thumb:
  49. filename += '.jpg'
  50. path = os.path.join(
  51. models.get_media_folder(self._webm_obj, filename), filename)
  52. thumb_path = default_storage.save(path, ContentFile(thumb))
  53. self._webm_obj.thumbnail = thumb_path
  54.  
  55. try:
  56. self._webm_obj.save()
  57. except IntegrityError:
  58. models.Webm.increase_rating(self._webm_obj.md5)
  59.  
  60. def work(self, *args, **kwargs):
  61. return super().work(self.save)
  62.  
  63.  
  64. if __name__ == '__main__':
  65. sections = ['vg', 'b', 'a', 'mov']
  66. worker = scraper.MainWorker(sections, downloader=DownloadToModel)
  67. worker.work()
  68.  
Runtime error #stdin #stdout #stderr 0.02s 9984KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 4, in <module>
ImportError: No module named 'django'