Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sanitizing markdown and getting social media names #63

Merged
merged 10 commits into from
Apr 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ And that's it! If it doesn't work you can create an issue

## How to use?
Please set the `SUBREDDIT` in `conf/config.ini` to select the desired subreddit to moderate. This only works when the Reddit account is moderator of the selected subreddit


## Features
- [x] Using Youtube, Last.fm and Soundcloud for detecting submissions with exceeding listen count
- [x] Report self-promotion posts using username and submission title
- [x] Post artist tags, album information and social media links using Musicbrainz

## Contribute

If you have features or bugfixes don't be shy to create a pull requests or bug report!
Expand Down
18 changes: 16 additions & 2 deletions blottertrax/description_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def get_reply(self, parsed_submission: ParsedSubmission) -> str:

life_span = '' if has_life_span is False else '({} to {})'.format(life_span_begin, life_span_end)
tags = ', '.join(map(lambda t: t['name'], artist['tag-list'][:5])) if has_tags else 'none'
socials = ', '.join(map(lambda u: '[{}]({})'.format(u['type'], u['target']),
artist['url-relation-list'])) if has_socials else 'none'
socials = ', '.join(
map(self.format_network_to_friendly_name, artist['url-relation-list'])) if has_socials else 'none'

return templates.musicbrainz_artist_info.strip().format(
artist['name'],
Expand All @@ -58,3 +58,17 @@ def _get_artist_by_id(self, artist_id: str):
return self.musicbrainz.get_artist_by_id(
id=artist_id, includes=['tags', 'ratings', 'annotation', 'url-rels', 'user-tags']
)['artist']

@staticmethod
def format_network_to_friendly_name(info) -> str:
social_network = ['twitter.com', 'facebook.com', 'instagram.com']
link_type = info['type']
target = info['target']
target = target.replace('(', '\(').replace(')', '\)')

if link_type == 'social network':
for domain in social_network:
if domain in target:
link_type = domain.split('.')[0]
break
return '[{}]({})'.format(link_type, target)
3 changes: 0 additions & 3 deletions blottertrax/services/youtube.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import List
from urllib import parse

from urllib.parse import urlparse

import googleapiclient.discovery
Expand All @@ -11,8 +10,6 @@
from blottertrax.value_objects.parsed_submission import ParsedSubmission
from blottertrax.value_objects.service_result import ThresholdServiceResult

scopes = ["https://www.googleapis.com/auth/youtube.readonly"]


class Youtube(ThresholdService):
config: Config = Config()
Expand Down
22 changes: 22 additions & 0 deletions tests/test_description_provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import unittest

from blottertrax.description_provider import DescriptionProvider


class MyTestCase(unittest.TestCase):
def test_format_network_to_friendly_name(self):
networks = [
('social network', 'https://facebook.com/linkedpage', '[facebook](https://facebook.com/linkedpage)'),
('social network', 'https://twitter.com/linkedpage', '[twitter](https://twitter.com/linkedpage)'),
('social network', 'https://instagram.com/linkedpage', '[instagram](https://instagram.com/linkedpage)'),
('discogs', 'https://discogs.com/linkedpage', '[discogs](https://discogs.com/linkedpage)'),
('some other database', 'https://some-other-database.com/linkedpage', '[some other database](https://some-other-database.com/linkedpage)'),
('wikipedia', 'https://en.wikipedia.org/wiki/Caravan_(band)', '[wikipedia](https://en.wikipedia.org/wiki/Caravan_\(band\))'),
]
for network_type, target, expected in networks:
formatted = DescriptionProvider.format_network_to_friendly_name({
'target': target,
'type': network_type
})

self.assertEqual(expected, formatted)