Skip to content

Commit

Permalink
Add CONTENT_ITEM relation
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed May 30, 2023
1 parent 45822d3 commit 4ab2c6f
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/tribler/core/components/knowledge/db/knowledge_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class ResourceType(IntEnum):
# this is a section for extra types
TAG = 101
TORRENT = 102
CONTENT_ITEM = 103


@dataclass
Expand Down
24 changes: 13 additions & 11 deletions src/tribler/core/components/knowledge/db/tests/test_knowledge_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,24 +539,26 @@ def test_get_subjects(self):
self.db,
{
'infohash1': [
Resource(predicate=ResourceType.TITLE, name='ubuntu', auto_generated=True),
Resource(predicate=ResourceType.CONTENT_ITEM, name='ubuntu', auto_generated=True),
Resource(predicate=ResourceType.TAG, name='linux', auto_generated=True),
],
'infohash2': [
Resource(predicate=ResourceType.TITLE, name='ubuntu', auto_generated=True),
Resource(predicate=ResourceType.CONTENT_ITEM, name='ubuntu', auto_generated=True),
Resource(predicate=ResourceType.TAG, name='linux', auto_generated=True),
],
'infohash3': [
Resource(predicate=ResourceType.TITLE, name='debian', auto_generated=True),
Resource(predicate=ResourceType.CONTENT_ITEM, name='debian', auto_generated=True),
Resource(predicate=ResourceType.TAG, name='linux', auto_generated=True),
],
}
)

actual = self.db.get_subjects(subject_type=ResourceType.TORRENT, predicate=ResourceType.TITLE, obj='missed')
actual = self.db.get_subjects(subject_type=ResourceType.TORRENT, predicate=ResourceType.CONTENT_ITEM,
obj='missed')
assert actual == []

actual = self.db.get_subjects(subject_type=ResourceType.TORRENT, predicate=ResourceType.TITLE, obj='ubuntu')
actual = self.db.get_subjects(subject_type=ResourceType.TORRENT, predicate=ResourceType.CONTENT_ITEM,
obj='ubuntu')
assert actual == ['infohash1', 'infohash2']

actual = self.db.get_subjects(subject_type=ResourceType.TORRENT, predicate=ResourceType.TAG, obj='linux')
Expand All @@ -568,11 +570,11 @@ def test_get_statements(self):
self.db,
{
'infohash1': [
Resource(predicate=ResourceType.TITLE, name='ubuntu', auto_generated=True),
Resource(predicate=ResourceType.CONTENT_ITEM, name='ubuntu', auto_generated=True),
Resource(predicate=ResourceType.TYPE, name='linux', auto_generated=True),
],
'infohash2': [
Resource(predicate=ResourceType.TITLE, name='debian', auto_generated=True),
Resource(predicate=ResourceType.CONTENT_ITEM, name='debian', auto_generated=True),
Resource(predicate=ResourceType.TYPE, name='linux', auto_generated=True),
],
'INFOHASH1': [
Expand All @@ -582,7 +584,7 @@ def test_get_statements(self):
)

expected = [
SimpleStatement(subject_type=ResourceType.TORRENT, subject='infohash1', predicate=ResourceType.TITLE,
SimpleStatement(subject_type=ResourceType.TORRENT, subject='infohash1', predicate=ResourceType.CONTENT_ITEM,
object='ubuntu'),
SimpleStatement(subject_type=ResourceType.TORRENT, subject='infohash1', predicate=ResourceType.TYPE,
object='linux')
Expand All @@ -600,11 +602,11 @@ def test_various_queries(self):
self.db,
{
'infohash1': [
Resource(predicate=ResourceType.TITLE, name='ubuntu'),
Resource(predicate=ResourceType.CONTENT_ITEM, name='ubuntu'),
Resource(predicate=ResourceType.TYPE, name='linux'),
],
'infohash2': [
Resource(predicate=ResourceType.TITLE, name='debian'),
Resource(predicate=ResourceType.CONTENT_ITEM, name='debian'),
Resource(predicate=ResourceType.TYPE, name='linux'),
],
'infohash3': [
Expand Down Expand Up @@ -632,7 +634,7 @@ def _subjects(subject_type=None, obj='', predicate=None):

assert _subjects(obj='linux') == {'infohash1', 'infohash2', 'infohash3'}
assert _subjects(predicate=ResourceType.TAG, obj='linux') == {'infohash3'}
assert _subjects(predicate=ResourceType.TITLE) == {'infohash1', 'infohash2'}
assert _subjects(predicate=ResourceType.CONTENT_ITEM) == {'infohash1', 'infohash2'}

@db_session
def test_non_existent_misc(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging
import queue
import time
from asyncio import Event
from dataclasses import dataclass
from typing import Optional, Set

Expand Down Expand Up @@ -187,8 +186,8 @@ async def process_torrent_title(self, infohash: Optional[bytes] = None, title: O
objects=tags)

if content_items := set(extract_only_valid_tags(title, rules=content_items_rules)):
self.save_statements(subject_type=ResourceType.TORRENT, subject=infohash_str, predicate=ResourceType.TITLE,
objects=content_items)
self.save_statements(subject_type=ResourceType.TORRENT, subject=infohash_str,
predicate=ResourceType.CONTENT_ITEM, objects=content_items)

return len(tags) + len(content_items)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def build_snippets(self, search_results: List[Dict]) -> List[Dict]:
with db_session:
content_items: List[str] = self.knowledge_db.get_objects(subject_type=ResourceType.TORRENT,
subject=search_result["infohash"],
predicate=ResourceType.TITLE)
predicate=ResourceType.CONTENT_ITEM)
if content_items:
for content_id in content_items:
content_to_torrents[content_id].append(search_result)
Expand Down
8 changes: 7 additions & 1 deletion src/tribler/gui/dialogs/editmetadatadialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from PyQt5.QtCore import QModelIndex, QPoint, Qt, pyqtSignal
from PyQt5.QtWidgets import QComboBox, QSizePolicy, QWidget

from tribler.core.components.knowledge.community.knowledge_validator import is_valid_resource
from tribler.core.components.knowledge.db.knowledge_db import ResourceType
from tribler.core.components.knowledge.knowledge_constants import MAX_RESOURCE_LENGTH, MIN_RESOURCE_LENGTH
from tribler.core.utilities.path_util import Path
Expand All @@ -14,7 +15,12 @@
from tribler.gui.utilities import connect, get_objects_with_predicate, get_ui_file_path, tr
from tribler.gui.widgets.tagbutton import TagButton

METADATA_TABLE_PREDICATES = [ResourceType.TITLE, ResourceType.DESCRIPTION, ResourceType.DATE, ResourceType.LANGUAGE]
METADATA_TABLE_PREDICATES = [
ResourceType.CONTENT_ITEM,
ResourceType.DESCRIPTION,
ResourceType.DATE,
ResourceType.LANGUAGE
]


class EditMetadataDialog(DialogContainer):
Expand Down
2 changes: 1 addition & 1 deletion src/tribler/gui/qt_resources/edit_metadata_dialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ margin: 0px;
</column>
<item>
<property name="text">
<string>Title</string>
<string>Content Item</string>
</property>
</item>
<item>
Expand Down

0 comments on commit 4ab2c6f

Please sign in to comment.