__init__.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. PLUGIN_NAME = "Ami (COV)"
  2. PLUGIN_AUTHOR = "SuperSaltyGamer"
  3. PLUGIN_DESCRIPTION = "Search covers from MH Covers by files or albums."
  4. PLUGIN_VERSION = "1.0.0"
  5. PLUGIN_API_VERSIONS = ["2.10"]
  6. PLUGIN_LICENSE = "AGPL-3.0"
  7. from typing import List, Union
  8. from picard.album import Album
  9. from picard.config import TextOption, config
  10. from picard.file import File
  11. from picard.track import Track
  12. from picard.coverart.image import CoverArtImage
  13. from picard.ui.itemviews import BaseAction, register_file_action, register_album_action
  14. from picard.ui.options import OptionsPage, register_options_page
  15. from PyQt5 import QtCore, QtWidgets
  16. from .client import Client
  17. from .utils import get_available_browsers
  18. OPTION_HOST = "cov_host"
  19. OPTION_QUERY = "cov_query"
  20. OPTION_BROWSER = "cov_browser"
  21. class SearchCoverAction(BaseAction):
  22. NAME = "Search COV for &covers…"
  23. def callback(self, items: List[Union[File, Track, Album]]):
  24. item = items[-1] if items else None
  25. if isinstance(item, File) or isinstance(item, Track) or isinstance(item, Album):
  26. port = Client.find_available_port(10000, 12000)
  27. client = Client(config.setting[OPTION_HOST], port, item, self.pick_cover)
  28. client.start(config.setting[OPTION_BROWSER], config.setting[OPTION_QUERY])
  29. def pick_cover(self, item: Union[File, Track, Album], cover: CoverArtImage):
  30. items = [item]
  31. if isinstance(item, Track):
  32. items.extend(item.files)
  33. if isinstance(item, Album):
  34. items.extend(item.tracks)
  35. for track in item.tracks:
  36. items.extend(track.files)
  37. for item in items:
  38. for image in item.metadata.images:
  39. if "front" in image.types:
  40. item.metadata.images.remove(image)
  41. item.metadata.images.append(cover)
  42. item.update()
  43. item.metadata_images_changed.emit()
  44. class PluginOptionsPage(OptionsPage):
  45. NAME = "ami_cov"
  46. TITLE = "Ami (COV)"
  47. PARENT = "plugins"
  48. options = [
  49. TextOption("setting", OPTION_HOST, "https://covers.musichoarders.xyz"),
  50. TextOption("setting", OPTION_QUERY, ""),
  51. TextOption("setting", OPTION_BROWSER, "Default")
  52. ]
  53. def __init__(self, parent=None):
  54. super(PluginOptionsPage, self).__init__(parent)
  55. self.ui = Ui_PluginOptionsPage(self)
  56. def load(self):
  57. self.ui.host_edit.setText(self.config.setting[OPTION_HOST])
  58. self.ui.query_edit.setText(self.config.setting[OPTION_QUERY])
  59. browsers = get_available_browsers().keys()
  60. for browser in browsers:
  61. self.ui.browser_combo.addItem(browser)
  62. browser = self.config.setting[OPTION_BROWSER]
  63. browser = browser if browser in browsers else "Default"
  64. self.ui.browser_combo.setCurrentText(browser)
  65. def save(self):
  66. self.config.setting[OPTION_HOST] = self.ui.host_edit.text()
  67. self.config.setting[OPTION_QUERY] = self.ui.query_edit.text()
  68. self.config.setting[OPTION_BROWSER] = self.ui.browser_combo.currentText()
  69. class Ui_PluginOptionsPage(object):
  70. def __init__(self, page: PluginOptionsPage):
  71. page.setObjectName("plugin_options_page")
  72. self.vertical_layout = QtWidgets.QVBoxLayout(page)
  73. self.vertical_layout.setObjectName("vertical_layout")
  74. self.host_label = QtWidgets.QLabel(page)
  75. self.host_label.setObjectName("host_label")
  76. self.host_label.setText("Website address:")
  77. self.vertical_layout.addWidget(self.host_label)
  78. self.host_edit = QtWidgets.QLineEdit(page)
  79. self.host_edit.setObjectName("host_edit")
  80. self.vertical_layout.addWidget(self.host_edit)
  81. self.query_label = QtWidgets.QLabel(page)
  82. self.query_label.setObjectName("query_label")
  83. self.query_label.setText("Query override:")
  84. self.vertical_layout.addWidget(self.query_label)
  85. self.query_edit = QtWidgets.QLineEdit(page)
  86. self.query_edit.setObjectName("query_edit")
  87. self.vertical_layout.addWidget(self.query_edit)
  88. self.browser_label = QtWidgets.QLabel(page)
  89. self.browser_label.setObjectName("browser_label")
  90. self.browser_label.setText("Browser:")
  91. self.vertical_layout.addWidget(self.browser_label)
  92. self.browser_combo = QtWidgets.QComboBox(page)
  93. self.browser_combo.setObjectName("browser_combo")
  94. self.vertical_layout.addWidget(self.browser_combo)
  95. self.spacer = QtWidgets.QSpacerItem(1, 1, QtWidgets.QSizePolicy.Policy.Minimum, QtWidgets.QSizePolicy.Policy.Expanding)
  96. self.vertical_layout.addItem(self.spacer)
  97. QtCore.QMetaObject.connectSlotsByName(page)
  98. register_file_action(SearchCoverAction())
  99. register_album_action(SearchCoverAction())
  100. register_options_page(PluginOptionsPage)