A command line (CLI) program for monitoring and downloading 8chan threads. Licensed under MIT.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

file_io.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import os
  2. from os.path import expanduser
  3. from lizard import time_methods
  4. def save_file(file_path, file_contents):
  5. """ Writes the given data to a file. The path will be automatically created if necessary, and if the file already
  6. exists, the new one will be saved with the timestamp as a suffix to the filename. """
  7. dir = os.path.dirname(file_path)
  8. if not os.path.exists(dir):
  9. os.makedirs(dir)
  10. with open(file_path, 'wb') as f:
  11. f.write(file_contents)
  12. def normalize_filename(f):
  13. """ f must be a dict containing keys 'hashed_name' and 'original_name' describing an 8ch file. The normalization
  14. inserts first 10 characters of the hashed name into the original name, just before the extension (if any).
  15. This normalized name is a compromise between preserving original filenames and avoiding collisions due to files
  16. with same name. """
  17. normalized = ' {}'.format(f['hashed_name'][:10]).join(os.path.splitext(f['original_name']))
  18. return normalized
  19. def ensure_directory_exists(p):
  20. """ Takes the directory part of the path, and if it doesn't exist, creates it. Otherwise does nothing. """
  21. if os.path.isfile(p):
  22. raise "Was expecting {} to be a directory, but it was a file instead. Please submit stack trace to the issue " \
  23. "tracker. ".format(p)
  24. if not os.path.exists(p):
  25. os.makedirs(p)
  26. def path_to_lizard_data():
  27. d = os.path.join(expanduser("~"), 'lizard_data')
  28. p = os.path.abspath(d)
  29. return p
  30. def path_to_database():
  31. d = path_to_lizard_data()
  32. p = os.path.join(d, 'threads.orm.db')
  33. return p
  34. def path_to_thread_cache(board, thread_no):
  35. p = os.path.join(path_to_lizard_data(), board, thread_no)
  36. return p
  37. def path_to_cached_file(board, thread_no, normalized_filename):
  38. tc = path_to_thread_cache(board, thread_no)
  39. p = os.path.join(tc, 'files', normalized_filename)
  40. return p
  41. def file_exists_in_cache(board, thread_no, normalized_filename):
  42. p = path_to_cached_file(board, thread_no, normalized_filename)
  43. result = os.path.exists(p)
  44. return result
  45. def generate_json_path(board, thread_no):
  46. """ Returns a path like /home/USER/lizard_data/b/123/123456.json. Note that the filename incoprorates a
  47. timestamp, so repeated calls to this method are likely to produce different results. """
  48. d = path_to_thread_cache(board, thread_no)
  49. f = '{}.json'.format(time_methods.timestamp_now())
  50. p = os.path.join(d, f)
  51. return p
  52. def generate_html_path(board, thread_no):
  53. """ Returns a path like /home/USER/lizard_data/b/123/123456.html. Note that the filename incoprorates a
  54. timestamp, so repeated calls to this method are likely to produce different results. """
  55. d = path_to_thread_cache(board, thread_no)
  56. f = '{}.html'.format(time_methods.timestamp_now())
  57. p = os.path.join(d, f)
  58. return p
  59. def timestamped_path_to_exported_threads_file():
  60. """ Returns a path like /home/USER/lizard_data/b/123/123456.html. Note that the filename incoprorates a
  61. timestamp, so repeated calls to this method are likely to produce different results. """
  62. f = 'Exported {}.txt'.format(time_methods.timestamp_now())
  63. p = os.path.join(path_to_lizard_data(), f)
  64. return p