Skip to content

Commit

Permalink
add server semaphore code
Browse files Browse the repository at this point in the history
  • Loading branch information
petrjasek committed Jan 18, 2024
1 parent 2c01e72 commit b5339a9
Show file tree
Hide file tree
Showing 9 changed files with 606 additions and 34 deletions.
Empty file added server/cp/ai/__init__.py
Empty file.
425 changes: 425 additions & 0 deletions server/cp/ai/semaphore.py

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions server/cp/output/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from .formatter.jimi import JimiFormatter # noqa
from .formatter.newsmlg2 import CPNewsMLG2Formatter # noqa
from .formatter.cp_ninjs_formatter import CPNINJSFormatter # noqa
from .formatter.semaphore import SemaphoreFormatter # noqa

from superdesk.publish.publish_service import PublishService, set_publish_service

Expand Down
91 changes: 91 additions & 0 deletions server/cp/output/formatter/cp_ninjs_formatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from superdesk.publish.formatters.ninjs_formatter import (
NINJSFormatter,
filter_empty_vals,
get_locale_name,
)


def format_cv_item(item, language):
"""Format item from controlled vocabulary for output."""
if item.get("scheme") == "subject":

return filter_empty_vals(
{
"code": item.get("qcode"),
"name": get_locale_name(item, language),
"scheme": "http://cv.iptc.org/newscodes/mediatopic/",
}
)
else:

return filter_empty_vals(
{
"code": item.get("qcode"),
"name": get_locale_name(item, language),
"scheme": item.get("scheme"),
}
)


class CPNINJSFormatter(NINJSFormatter):
type = "cpninjs"
name = "CP NINJS"

def _transform_to_ninjs(self, article, subscriber, recursive=True):
ninjs = super()._transform_to_ninjs(article, subscriber, recursive=recursive)

if (
article.get("subject")
or article.get("organisation")
or article.get("place")
or article.get("event")
or article.get("person")
):
combined_subjects = (
self._get_subject(article)
+ self._get_organisation(article)
+ self._get_place(article)
+ self._get_event(article)
+ self._get_person(article)
)
ninjs["subject"] = combined_subjects

return ninjs

def _get_subject(self, article):
"""Get subject list for article."""
return [
format_cv_item(item, article.get("language", ""))
for item in article.get("subject", [])
]

# Updated Code here to fetch Organisations from Article
def _get_organisation(self, article):
return [
format_cv_item(item, article.get("language", ""))
for item in article.get("organisation", [])
]

# Updated Code here to fetch Places from Article
def _get_place(self, article):
"""Get place list for article."""
return [
format_cv_item(item, article.get("language", ""))
for item in article.get("place", [])
]

# Updated Code here to fetch Events from Article
def _get_event(self, article):
"""Get event list for article."""
return [
format_cv_item(item, article.get("language", ""))
for item in article.get("event", [])
]

# Updated Code here to fetch Person from Article
def _get_person(self, article):
"""Get person list for article."""
return [
format_cv_item(item, article.get("language", ""))
for item in article.get("person", [])
]
27 changes: 27 additions & 0 deletions server/cp/output/formatter/semaphore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import logging
from superdesk.text_utils import get_text
from .cp_ninjs_formatter import CPNINJSFormatter
from cp.ai.semaphore import Semaphore # Import the Semaphore integration class

logger = logging.getLogger(__name__)


class SemaphoreFormatter(CPNINJSFormatter):
def can_format(self, format_type, article):
return format_type.lower() == "semaphore" and article.get("type") == "text"

def _transform_to_ninjs(self, article, subscriber, recursive=True):
semaphore = Semaphore() # Initialize the Semaphore integration
formatted_data = {} # Define how you want to format the data for Semaphore

try:
# Example: format the data
formatted_data["uuid"] = article["guid"]
formatted_data["headline"] = get_text(article["headline"])
# Add more formatting logic here

except Exception as e:
logger.error(f"Error formatting data for Semaphore: {str(e)}")
formatted_data = {} # Return an empty dictionary in case of an error

return formatted_data
19 changes: 19 additions & 0 deletions server/cp/output/transmitters/semaphore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from flask import current_app, json

from superdesk.publish import register_transmitter
from superdesk.publish.publish_service import PublishService
from superdesk.text_checkers.ai.semaphore import (
Semaphore,
) # Import the Semaphore integration class


class SemaphoreTransmitter(PublishService):
def _transmit(self, queue_item, subscriber):
semaphore = Semaphore(current_app) # Initialize the Semaphore integration
item = json.loads(queue_item["formatted_item"])
# Modify this part to transmit the item using the Semaphore integration
semaphore.transmit(item)


# Register the Semaphore transmitter
register_transmitter("semaphore", SemaphoreTransmitter(), [])
4 changes: 2 additions & 2 deletions server/requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ python3-saml>=1.9,<1.10
python-xmp-toolkit>=2.0.1,<2.1
num2words==0.5.10

git+https://github.com/superdesk/superdesk-core.git@develop#egg=superdesk-core
git+https://github.com/superdesk/superdesk-planning.git@develop#egg=superdesk-planning
git+https://github.com/superdesk/superdesk-core.git@v2.6.7#egg=superdesk-core
git+https://github.com/superdesk/superdesk-planning.git@v2.6.2#egg=superdesk-planning
git+https://github.com/superdesk/[email protected]#egg=superdesk-analytics
71 changes: 39 additions & 32 deletions server/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# pip-compile requirements.in
#
amqp==5.1.1
amqp==5.2.0
# via kombu
arrow==0.13.0
# via
Expand All @@ -14,7 +14,7 @@ async-timeout==4.0.3
# via redis
authlib==0.14.3
# via superdesk-core
babel==2.12.1
babel==2.14.0
# via flask-babel
bcrypt==3.1.7
# via superdesk-core
Expand All @@ -26,32 +26,34 @@ blinker==1.4
# flask-mail
# raven
# superdesk-core
boto3==1.28.45
boto3==1.34.21
# via superdesk-core
botocore==1.31.45
botocore==1.34.21
# via
# boto3
# s3transfer
cachetools==5.3.1
cachetools==5.3.2
# via flask-oidc-ex
celery[redis]==5.2.7
# via superdesk-core
# via
# celery
# superdesk-core
cerberus==1.3.5
# via
# eve
# superdesk-core
certifi==2023.7.22
certifi==2023.11.17
# via
# elastic-apm
# elasticsearch
# requests
cffi==1.15.1
cffi==1.16.0
# via
# bcrypt
# cryptography
chardet==3.0.4
# via superdesk-core
charset-normalizer==3.2.0
charset-normalizer==3.3.2
# via requests
ciso8601==1.0.8
# via eve-elastic
Expand All @@ -62,7 +64,6 @@ click==8.1.7
# click-plugins
# click-repl
# flask
# superdesk-core
click-didyoumean==0.3.0
# via celery
click-plugins==1.1.1
Expand All @@ -71,11 +72,11 @@ click-repl==0.3.0
# via celery
croniter==0.3.37
# via superdesk-core
cryptography==41.0.3
cryptography==41.0.7
# via
# authlib
# jwcrypto
deepdiff==6.5.0
deepdiff==6.7.1
# via superdesk-planning
defusedxml==0.7.1
# via python3-saml
Expand All @@ -84,11 +85,15 @@ deprecated==1.2.14
docopt==0.6.2
# via num2words
draftjs-exporter[lxml]==2.1.7
# via superdesk-core
# via
# draftjs-exporter
# superdesk-core
ecs-logging==2.1.0
# via elastic-apm
elastic-apm[flask]==6.18.0
# via superdesk-core
elastic-apm[flask]==6.20.0
# via
# elastic-apm
# superdesk-core
elasticsearch==7.13.4
# via eve-elastic
eve==1.1.2
Expand All @@ -97,7 +102,7 @@ eve-elastic==7.3.2
# via superdesk-core
events==0.3
# via eve
feedparser==6.0.10
feedparser==6.0.11
# via superdesk-core
flask==1.1.2
# via
Expand Down Expand Up @@ -130,7 +135,7 @@ httplib2==0.22.0
# via oauth2client
icalendar==4.0.9
# via superdesk-planning
idna==3.4
idna==3.6
# via requests
isodate==0.6.1
# via python3-saml
Expand All @@ -148,7 +153,7 @@ jmespath==1.0.1
# via
# boto3
# botocore
jwcrypto==1.5.0
jwcrypto==1.5.1
# via
# flask-oidc-ex
# python-jwt
Expand Down Expand Up @@ -179,13 +184,13 @@ oauthlib==3.2.2
# via requests-oauthlib
ordered-set==4.1.0
# via deepdiff
packaging==23.1
packaging==23.2
# via gunicorn
pillow==9.2.0
# via superdesk-core
prompt-toolkit==3.0.39
prompt-toolkit==3.0.43
# via click-repl
pyasn1==0.5.0
pyasn1==0.5.1
# via
# ldap3
# oauth2client
Expand All @@ -209,13 +214,13 @@ python-dateutil==2.7.5
# croniter
# icalendar
# superdesk-core
python-jwt==4.0.0
python-jwt==4.1.0
# via flask-oidc-ex
python-magic==0.4.27
# via superdesk-core
python-twitter==3.5
# via superdesk-core
python-xmp-toolkit==2.0.1
python-xmp-toolkit==2.0.2
# via -r requirements.in
python3-saml==1.9.0
# via -r requirements.in
Expand All @@ -231,7 +236,9 @@ pytz==2023.3.post1
pyyaml==6.0.1
# via superdesk-core
raven[flask]==6.10.0
# via superdesk-core
# via
# raven
# superdesk-core
redis==4.5.5
# via
# celery
Expand All @@ -247,11 +254,11 @@ requests-oauthlib==1.3.1
# via python-twitter
rsa==4.9
# via oauth2client
s3transfer==0.6.2
s3transfer==0.10.0
# via boto3
sgmllib3k==1.0.0
# via feedparser
simplejson==3.19.1
simplejson==3.19.2
# via eve
six==1.16.0
# via
Expand All @@ -262,11 +269,11 @@ six==1.16.0
# python-dateutil
superdesk-analytics @ git+https://github.com/superdesk/[email protected]
# via -r requirements.in
superdesk-core @ git+https://github.com/superdesk/superdesk-core.git@develop
superdesk-core @ git+https://github.com/superdesk/superdesk-core.git@v2.6.7
# via -r requirements.in
superdesk-planning @ git+https://github.com/superdesk/superdesk-planning.git@develop
superdesk-planning @ git+https://github.com/superdesk/superdesk-planning.git@v2.6.2
# via -r requirements.in
typing-extensions==4.7.1
typing-extensions==4.9.0
# via superdesk-core
tzlocal==2.1
# via superdesk-core
Expand All @@ -279,20 +286,20 @@ urllib3==1.25.11
# elasticsearch
# requests
# superdesk-core
vine==5.0.0
vine==5.1.0
# via
# amqp
# celery
# kombu
wcwidth==0.2.6
wcwidth==0.2.13
# via prompt-toolkit
websockets==10.3
# via superdesk-core
werkzeug==1.0.1
# via
# flask
# superdesk-core
wrapt==1.15.0
wrapt==1.14.1
# via
# deprecated
# elastic-apm
Expand Down
1 change: 1 addition & 0 deletions server/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"cp.ultrad",
"cp.planning_exports",
"cp.set_province_on_publish",
"cp.ai.semaphore",
]

MACROS_MODULE = "cp.macros"
Expand Down

0 comments on commit b5339a9

Please sign in to comment.