Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
retribute.me
api
Commits
76e08567
Verified
Commit
76e08567
authored
May 25, 2019
by
Eliot Berriot
Browse files
Added url extraction from activitypub profiles
parent
cb55f756
Changes
3
Hide whitespace changes
Inline
Side-by-side
retribute_api/search/activitypub.py
0 → 100644
View file @
76e08567
import
lxml.html
from
rest_framework
import
serializers
def
get_urls
(
content
):
links
=
[]
dom
=
lxml
.
html
.
fromstring
(
content
)
for
link
in
dom
.
xpath
(
"//a/@href"
):
links
.
append
(
link
)
return
links
def
extract_urls_from_attachments
(
attachments
):
data
=
[]
for
attachment
in
attachments
:
if
attachment
[
"type"
]
!=
"PropertyValue"
:
continue
urls
=
get_urls
(
attachment
[
"value"
])
if
not
urls
:
continue
row
=
{
"summary"
:
attachment
[
"name"
],
"url"
:
urls
[
0
]}
data
.
append
(
row
)
return
data
class
TagSerializer
(
serializers
.
Serializer
):
name
=
serializers
.
CharField
()
type
=
serializers
.
CharField
()
class
AttachmentSerializer
(
serializers
.
Serializer
):
type
=
serializers
.
CharField
()
name
=
serializers
.
CharField
()
value
=
serializers
.
CharField
()
class
ActorSerializer
(
serializers
.
Serializer
):
attachment
=
serializers
.
ListField
(
child
=
AttachmentSerializer
(),
min_length
=
1
)
tag
=
serializers
.
ListField
(
child
=
TagSerializer
(),
min_length
=
0
)
setup.cfg
View file @
76e08567
...
...
@@ -31,6 +31,8 @@ install_requires =
django-redis
djangorestframework
psycopg2-binary
lxml
[options.entry_points]
...
...
tests/search/test_activitypub.py
0 → 100644
View file @
76e08567
from
retribute_api.search
import
activitypub
def
test_profile_serializer
():
payload
=
{
"tag"
:
[{
"type"
:
"Hashtag"
,
"name"
:
"#nobot"
}],
"attachment"
:
[
{
"type"
:
"PropertyValue"
,
"name"
:
"patreon"
,
"value"
:
'<a href="https://patreon.com/username" rel="me nofollow noopener">Test</a>'
,
}
],
}
serializer
=
activitypub
.
ActorSerializer
(
data
=
payload
)
assert
serializer
.
is_valid
(
raise_exception
=
True
)
is
True
assert
serializer
.
validated_data
==
payload
def
test_extract_urls_from_attachments
():
attachments
=
[
{
"type"
:
"PropertyValue"
,
"name"
:
"Support me on Patreon"
,
"value"
:
'<a href="https://patreon.com/username" rel="me nofollow noopener" target="_blank">This is my Patreon</a>'
,
},
{
"type"
:
"PropertyValue"
,
"name"
:
"Support me on Ko-Fi"
,
"value"
:
'<a href="https://ko-fi.com/username" rel="me nofollow noopener" target="_blank">This is my Ko-Fi</a>'
,
},
{
"type"
:
"PropertyValue"
,
"name"
:
"Irrelevant text"
,
"value"
:
"No link"
},
]
expected
=
[
{
"summary"
:
"Support me on Patreon"
,
"url"
:
"https://patreon.com/username"
},
{
"summary"
:
"Support me on Ko-Fi"
,
"url"
:
"https://ko-fi.com/username"
},
]
assert
activitypub
.
extract_urls_from_attachments
(
attachments
)
==
expected
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment