<commit_before>import re
from rest_framework import serializers


from .models import Source, Video


class SourceSerializer(serializers.ModelSerializer):

    class Meta:
        model = Source
        fields = ('id', 'url', 'title', 'typ', 'youtube_id')
        read_only_fields = ('id', 'title', 'youtube_id', 'typ')

    def validate(self, data):
        '''
        - Apply all default validations
        - make sure provided URL is valid
        - parse the youtube_id and type
        return data or raise validation error
        '''
        data = super().validate(data)
        channel_pattern = re.compile(r'.*youtube.com/(channel|user)/(?P<id>[ A-Za-z0-9_-]*)')
        playlist_pattern = re.compile(r'.*list=(?P<id>[ A-Za-z0-9_-]*)')

        if channel_pattern.match(data['url']):
            data['youtube_id'] = channel_pattern.match(data['url']).groupdict()['id']
            data['typ'] = Source.CHANNEL
        elif playlist_pattern.match(data['url']):
            data['youtube_id'] = playlist_pattern.match(data['url']).groupdict()['id']
            data['typ'] = Source.PLAYLIST
        else:
            raise serializers.ValidationError("Source URL couldn't be validated")
        print('data')
        return data


class VideoSerializer(serializers.ModelSerializer):

    class Meta:
        model = Video
        fields = ('url', 'title', 'duration', 'views_count', 'thmubnail', 'image')<commit_msg>Removed print line<commit_after>import re
from rest_framework import serializers


from .models import Source, Video


class SourceSerializer(serializers.ModelSerializer):

    class Meta:
        model = Source
        fields = ('id', 'url', 'title', 'typ', 'youtube_id')
        read_only_fields = ('id', 'title', 'youtube_id', 'typ')

    def validate(self, data):
        '''
        - Apply all default validations
        - make sure provided URL is valid
        - parse the youtube_id and type
        return data or raise validation error
        '''
        data = super().validate(data)
        channel_pattern = re.compile(r'.*youtube.com/(channel|user)/(?P<id>[ A-Za-z0-9_-]*)')
        playlist_pattern = re.compile(r'.*list=(?P<id>[ A-Za-z0-9_-]*)')

        if channel_pattern.match(data['url']):
            data['youtube_id'] = channel_pattern.match(data['url']).groupdict()['id']
            data['typ'] = Source.CHANNEL
        elif playlist_pattern.match(data['url']):
            data['youtube_id'] = playlist_pattern.match(data['url']).groupdict()['id']
            data['typ'] = Source.PLAYLIST
        else:
            raise serializers.ValidationError("Source URL couldn't be validated")
        return data


class VideoSerializer(serializers.ModelSerializer):

    class Meta:
        model = Video
        fields = ('url', 'title', 'duration', 'views_count', 'thmubnail', 'image')
{'fraction_non_alphanumeric': 0.1473387381036306, 'fraction_numerical': 0.0028198801550934085, 'mean_word_length': 2.804289544235925, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '945225', 'n_tokens_mistral': 838, 'n_tokens_neox': 805, 'n_words': 190}