GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

metadata()   C
last analyzed

Complexity

Conditions 7

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 37
rs 5.5
cc 7
1
from mutagen import easyid3, flac, mp3
2
from PyQt5.QtCore import QByteArray
3
4
from mosaic import utilities
5
6
7
def identify_filetype(file):
8
    """Identify the given file as either MP3 or FLAC and return a Mutagen object."""
9
    if file.endswith('.mp3'):
10
        audio_file = mp3.MP3(file, ID3=easyid3.EasyID3)
11
12
    elif file.endswith('.flac'):
13
        audio_file = flac.FLAC(file)
14
15
    return audio_file
16
17
18
def extract_metadata(file):
19
    """Extract all of the metadata embedded within the audio file.
20
21
    Creates a dictionary with the tag and data pairs so that the MP3 and FLAC
22
    tags are in the same dictionary.
23
    """
24
    audio_file = identify_filetype(file)
25
26
    # Mutagen returns None if there is no metadata embedded within a file
27
    if audio_file.tags is None:
28
        return {}
29
30
    tags_dictionary = dict(audio_file.tags)
31
    metadata_dictionary = dict((k, "".join(v)) for k, v in tags_dictionary.items())
32
    return metadata_dictionary
33
34
35
def metadata(file):
36
    """Create a list of all the media file's extracted metadata."""
37
    audio_file = identify_filetype(file)
38
    file_metadata = extract_metadata(file)
39
40
    album = file_metadata.get('album', '??')
41
    artist = file_metadata.get('artist', '??')
42
    title = file_metadata.get('title', '??')
43
    track_number = file_metadata.get('tracknumber', '??')
44
    date = file_metadata.get('date', '')
45
    genre = file_metadata.get('genre', '')
46
    description = file_metadata.get('description', '')
47
    sample_rate = "{} Hz" .format(audio_file.info.sample_rate)
48
    artwork = utilities.resource_filename('mosaic.images', 'nocover.png')
49
50
    try:  # Bitrate only applies to mp3 files
51
        bitrate = "{} kb/s" .format(audio_file.info.bitrate // 1000)
52
        bitrate_mode = "{}" .format(audio_file.info.bitrate_mode)
53
    except AttributeError:
54
        bitrate = ''
55
        bitrate_mode = ''
56
    try:  # Bits per sample only applies to flac files
57
        bits_per_sample = "{}" .format(audio_file.info.bits_per_sample)
58
    except AttributeError:
59
        bits_per_sample = ''
60
61
    try:  # Searches for cover art in flac files
62
        artwork = QByteArray().append(audio_file.pictures[0].data)
63
    except (IndexError, flac.FLACNoHeaderError):
64
        artwork = utilities.resource_filename('mosaic.images', 'nocover.png')
65
    except AttributeError:  # Searches for cover art in mp3 files
66
        for tag in mp3.MP3(file):
67
            if 'APIC' in tag:
68
                artwork = QByteArray().append(mp3.MP3(file)[tag].data)
69
70
    return [album, artist, title, track_number, date, genre, description, sample_rate,
71
            bitrate, bitrate_mode, bits_per_sample, artwork]
72