Skip to content

Commit

Permalink
sanitizing markdown and getting social media names (#63)
Browse files Browse the repository at this point in the history
* sanitizing markdown and getting social media names

Haven't gotten around to test this but I think it should work. Would solve #61 and #62

* Making flake happy

* Minor optimization by adding a break

* Rewrote as a single function

* A (poor) attempt to make flake happy

* Trying to make flake happy again also formatting

* Fixed unclosed parenthesis

* Properly labelling a function

* Add test for formatted network provider

* Update README

I think with the github outage it didn't save my latest commit so adding
this commit here to make sure the previous commit is added

Co-authored-by: Martijn Boers <martijn@plebian.nl>
  • Loading branch information
TcMaX and martijnboers authored Apr 2, 2020
1 parent 1703981 commit a7a6e04
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
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 @@ -43,8 +43,8 @@ def get_reply(self, parsed_submission: ParsedSubmission) -> str:
album_release_date = '' if album_release_date is False else '({})'.format(album_release_date)
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 @@ -60,3 +60,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)

0 comments on commit a7a6e04

Please sign in to comment.