helpers.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import os
  2. import requests
  3. import re
  4. from PyQt5.QtCore import QObject, pyqtSignal
  5. from PyQt5 import QtGui
  6. def relative_path(path):
  7. this_dir = os.path.dirname(__file__)
  8. return os.path.join(this_dir, path)
  9. def fix_markdown(text):
  10. # most people use a single newline, not two
  11. text = text.replace('\n', '\n\n')
  12. # replace '@name' with markdown link '[@name](lbry://@name)'
  13. text = re.sub(r'(\s)(@[^\s]*)', r'\1[\2](lbry://\2)', text)
  14. return text
  15. class FeedUpdater(QObject):
  16. finished = pyqtSignal(list)
  17. progress = pyqtSignal(object)
  18. def set_feed(self, feed):
  19. self.feed = feed
  20. def run(self):
  21. items = []
  22. for i in range(20):
  23. try:
  24. next_item = next(self.feed)
  25. except StopIteration:
  26. break
  27. except KeyError:
  28. continue
  29. except ConnectionError:
  30. window.connecting_screen()
  31. break
  32. if next_item:
  33. self.progress.emit(next_item)
  34. items.append(next_item)
  35. self.finished.emit(items)
  36. IMAGE_DIR = relative_path('./images/')
  37. thumb_path = '/tmp/lyberry_thumbs/'
  38. if not os.path.isdir(thumb_path):
  39. os.mkdir(thumb_path)
  40. def download_file(url):
  41. local_filename = thumb_path+str(hash(url))
  42. if os.path.isfile(local_filename):
  43. return local_filename
  44. with requests.get(url, stream=True) as r:
  45. r.raise_for_status()
  46. with open(local_filename, 'wb') as f:
  47. for chunk in r.iter_content(chunk_size=8192):
  48. f.write(chunk)
  49. return local_filename
  50. class ImageLoader(QObject):
  51. finished = pyqtSignal(QtGui.QPixmap)
  52. no = 0
  53. def set_url(self, url):
  54. self.url = url
  55. def run(self):
  56. pixmap = QtGui.QPixmap()
  57. try:
  58. file = download_file(self.url)
  59. pixmap.load(file)
  60. del file
  61. self.finished.emit(pixmap)
  62. return
  63. except Exception as err:
  64. print(f'Error loading image: {err}')
  65. self.default()
  66. def default(self):
  67. pixmap = QtGui.QPixmap()
  68. pixmap.load(IMAGE_DIR+'NotFound.png')
  69. self.finished.emit(pixmap)
  70. def make_chapter_file(pub):
  71. with open('/tmp/lyberry_chapters', 'w') as chapters_file:
  72. chapters_file.write(desc_to_ffmetadata(pub.description))
  73. def desc_to_ffmetadata(desc: str, end: int = 0) -> str:
  74. lines = desc.split('\n')
  75. matches = []
  76. for line in lines:
  77. match = re.match(r'\s*(\d+:\d+(:\d+)?)\s(.*)', line)
  78. if match:
  79. matches.append(match)
  80. stamps = []
  81. for match in matches:
  82. raw_time = match.group(1)
  83. times = raw_time.split(':')
  84. if len(times) == 2:
  85. [minutes, seconds] = times
  86. hours = 0
  87. elif len(times) == 3:
  88. [hours, minutes, seconds] = times
  89. time = int(hours)*3600 + int(minutes)*60 + int(seconds)
  90. title = match.group(3)
  91. stamps.append([time, title])
  92. out = ';FFMETADATA1'
  93. for i, stamp in enumerate(stamps):
  94. time = stamp[0]
  95. title = stamp[1]
  96. out += f'''
  97. [CHAPTER]
  98. TIMEBASE=1/1
  99. START={time}
  100. '''
  101. if i+1 < len(stamps):
  102. next_time = stamps[i+1][0]
  103. out += f'END={next_time}\n'
  104. elif end != 0:
  105. out += f'END={end}\n'
  106. else:
  107. out += f'END={next_time + 100}\n'
  108. out += f'title={title}\n'
  109. return out
  110. class Loader(QObject):
  111. finished = pyqtSignal()
  112. def set_func(self, func):
  113. self.func = func
  114. def run(self):
  115. self.func()
  116. self.finished.emit()