pub_screen.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from PyQt5 import QtWidgets, uic
  2. from PyQt5.QtCore import QObject, QThread, pyqtSignal
  3. from lyberry_qt import helpers
  4. from lyberry_api import settings
  5. from lyberry_qt.comment_screen import CommentWidget, WriteCommentWidget
  6. from datetime import datetime
  7. import re
  8. def open_external(pub):
  9. file_type = pub.media_type.split('/')[0]
  10. if file_type == 'video' or file_type == 'audio':
  11. helpers.make_chapter_file(pub)
  12. settings.media_player(pub.streaming_url)
  13. elif file_type == 'text':
  14. settings.text_viewer(pub.streaming_url)
  15. class PubScreen(QtWidgets.QDialog):
  16. change_url = pyqtSignal(str)
  17. def __init__(self, window, pub):
  18. super(PubScreen, self).__init__()
  19. uic.loadUi(helpers.relative_path('designer/pub.ui'), self)
  20. self._window = window
  21. self.pub = pub
  22. self._lbry = pub._LBRY_api
  23. self.title_label.setText(self.pub.title)
  24. self.url = pub.url
  25. self.license_label.setText(f"License: {pub.license}")
  26. timestamp = datetime.fromtimestamp(pub.timestamp)
  27. self.date_label.setText(f"Date: {timestamp}")
  28. desc = helpers.fix_markdown(self.pub.description)
  29. self.description_label.setText(desc)
  30. self.description_label.linkActivated.connect(self.change_url.emit)
  31. self.channel_button.setText(self.pub.channel.name)
  32. self.channel_button.clicked.connect(lambda: self.change_url.emit(self.pub.channel.url))
  33. self.amt = 1
  34. self.comments = []
  35. self.comments_button.clicked.connect(self.more_comments)
  36. self.write_comment_button.clicked.connect(self.write_comment)
  37. self.open_button.clicked.connect(self.open)
  38. self.finished.connect(self.pub.refresh_comments_feed)
  39. self.play_thread = QThread()
  40. def write_comment(self):
  41. self.writing_section = WriteCommentWidget(self)
  42. self.write_comment_section.addWidget(self.writing_section)
  43. self.write_comment_button.setEnabled(False)
  44. self.writing_section.finished.connect(lambda:
  45. self.write_comment_button.setEnabled(True))
  46. def open(self):
  47. self.play_thread.exit()
  48. self.opener = helpers.Loader()
  49. self.opener.set_func(lambda: open_external(self.pub))
  50. self.opener.moveToThread(self.play_thread)
  51. self.play_thread.started.connect(self.opener.run)
  52. self.opener.finished.connect(self.opener.deleteLater)
  53. self.play_thread.start()
  54. def show_comment(self, comment):
  55. item = CommentWidget(comment)
  56. item.channel_button.clicked.connect(lambda: self.change_url.emit(comment.channel.url))
  57. item.change_url.connect(self.change_url.emit)
  58. self.comments.append(item)
  59. self.comments_section.addWidget(item, self.amt, 0, 1, 1)
  60. self.amt += 1
  61. def list_comments(self, comments):
  62. for comment in comments:
  63. self.show_comment(comment)
  64. self.fix_comment_button()
  65. def fix_comment_button(self):
  66. self.comments_section.addWidget(self.comments_button)
  67. self.comments_button.setEnabled(True)
  68. def more_comments(self):
  69. self.comments_button.setEnabled(False)
  70. self.comment_worker = helpers.FeedUpdater()
  71. self.comment_worker.set_feed(self.pub.comments_feed)
  72. self.comment_worker.moveToThread(self._window.comment_thread)
  73. self._window.comment_thread.started.connect(self.comment_worker.run)
  74. self.comment_worker.progress.connect(self.show_comment)
  75. self.comment_worker.finished.connect(self.fix_comment_button)
  76. self.comment_worker.finished.connect(self.comment_worker.deleteLater)
  77. self.comment_worker.finished.connect(self._window.comment_thread.quit)
  78. self.scrollArea.ensureWidgetVisible(self.comments_button)
  79. self._window.comment_thread.start()