<commit_before> #!/usr/bin/env python
import urllib
import urllib2
import json
import re
import os.path
import readline, glob
# import subprocess
import sys
import pprint
from mutagen.mp3 import MP3
from mutagen.id3 import ID3, APIC, TIT2, ID3, TALB, TPE1, TPE2, TRCK, TCON, TDRC, error

class tcolors:
    HEADER = '\033[95m'
    BLUE = '\033[94m'
    GREEN = '\033[92m'
    YELLOW = '\033[93m'
    RED = '\033[91m'
    END = '\033[0m'
    BOLD = '\033[1m'

def validate_url(url):
    try:
        urllib2.urlopen(url)
        return True
    except:
        return False

def change_song_details(audio_path, title, artist, art_path, album_name, track_number, year, genre):
    mp3 = MP3(audio_path, ID3=ID3)
    set_tags(mp3)
    set_album_art(mp3, art_path, 'image/jpg')
    mp3.save()

    id3 = ID3(audio_path)
    set_album_name(id3, album_name)
    set_title(id3, title)
    set_artist(id3, artist)
    set_track_number(id3, track_number)
    set_year(id3, year)
    set_genre(id3, genre)
    id3.save()

    print 'Track: %s' % id3['TIT2'].text[0]
    print 'Artist: %s' % id3['TPE2'].text[0]
    print 'Album: %s' % id3['TALB'].text[0]
    print 'Year: %s' % id3['TDRC'].text[0]
    print 'Number: %s' % id3['TRCK'].text[0]

def set_tags(mp3):
    try:
        mp3.add_tags()
    except error as e:
        print e
        pass

def set_album_art(mp3, art_path, image_type):
    try:
        mp3.tags.add(
            APIC(
                encoding = 3, # 3 is for utf-8
                mime = image_type, # image/jpeg or image/png
                type = 3, # 3 is for the cover image
                desc = u'Cover',
                data = open(art_path).read()
            )
        )
    except error as e:
        print e
        pass

def set_album_name(id3, album_name):
    try:
        id3.add(TALB(encoding = 3, text = album_name))
    except error as e:
        print e
        pass


def set_title(id3, title):
    try:
        id3.add(TIT2(encoding = 3, text = title))
    except error as e:
        print e
        pass

def set_artist(id3, artist):
    try:
        id3.add(TPE2(encoding = 3, text = artist))
    except error as e:
        print e
        pass

def set_track_number(id3, track_number):
    try:
        id3.add(TRCK(encoding = 3, text = track_number))
    except error as e:
        print e
        pass

def set_year(id3, year):
    try:
        id3.add(TDRC(encoding = 3, text = year))
    except error as e:
        print e
        pass

def set_genre(id3, genre):
    try:
        id3.add(TCON(encoding = 3, text = genre))
    except error as e:
        print e
        pass

def download_file(url, path, title):
    if not os.path.exists(os.path.expanduser(path)):
        try:
            os.makedirs(os.path.expanduser(path))
        except error as e:
            print e
            pass

    download_url = urllib2.urlopen(url)
    file = open(os.path.expanduser(path + title), 'wb')
    meta = download_url.info()
    file_size = int(meta.getheaders('Content-Length')[0])

    print '\nDownloading: %s Bytes: %s' % (tcolors.BOLD + title + tcolors.END, tcolors.BOLD + str(round(file_size / float(1000000), 2)) + "MB" + tcolors.END)

    file_size_dl = 0
    block_sz = 8192

    while True:
        buffer = download_url.read(block_sz)

        if not buffer:
            break

        file_size_dl += len(buffer)
        file.write(buffer)
        status = r'%10d  [%3.2f%%]' % (file_size_dl, file_size_dl * 100. / file_size)
        status = status + chr(8)*(len(status)+1)

        print status,

    file.close()

def download_album(album_json):
    for t in album_json['trackinfo']:
        download_file(t['file']['mp3-128'], '~/Downloads/bandcamper-test/', create_file_name(t['track_num'], t['title']))

    return True

def create_file_name(track_number, title):
    return ('0' if track_number < 10 else '') + str(track_number) + ' ' + title + '.mp3'

def find_album_json(garbage):
    album_json = None
    artist_regex = re.compile('artist:\s\"')
    album_regex = re.compile('album_title:\s\"')
    track_regex = re.compile('trackinfo\s\:\s\[')

    artist_name = re.sub('"', '', re.sub(artist_regex, '', find_json(garbage, artist_regex, ',', '"')))
    album_name = re.sub('"', '', re.sub(album_regex, '', find_json(garbage, album_regex, ',', '"')))
    track_json = re.sub('\}\]', '}]}', re.sub(track_regex, '{\"trackinfo\":[', find_json(garbage, track_regex, ',', ']')))

    if artist_name and album_name and track_json:
        #todo: add artist name and album name to the json
        album_json = json.loads(track_json)

    return album_json

def find_json(garbage, regex, nail, coffin):
    for m in regex.finditer(garbage):
        start = m.start()
<commit_msg>Now grabbing album art url<commit_after> #!/usr/bin/env python
import urllib
import urllib2
import json
import re
import os.path
import readline, glob
# import subprocess
import sys
import pprint
from mutagen.mp3 import MP3
from mutagen.id3 import ID3, APIC, TIT2, ID3, TALB, TPE1, TPE2, TRCK, TCON, TDRC, error

class tcolors:
    HEADER = '\033[95m'
    BLUE = '\033[94m'
    GREEN = '\033[92m'
    YELLOW = '\033[93m'
    RED = '\033[91m'
    END = '\033[0m'
    BOLD = '\033[1m'

def validate_url(url):
    try:
        urllib2.urlopen(url)
        return True
    except:
        return False

def change_song_details(audio_path, title, artist, art_path, album_name, track_number, year, genre):
    mp3 = MP3(audio_path, ID3=ID3)
    set_tags(mp3)
    set_album_art(mp3, art_path, 'image/jpg')
    mp3.save()

    id3 = ID3(audio_path)
    set_album_name(id3, album_name)
    set_title(id3, title)
    set_artist(id3, artist)
    set_track_number(id3, track_number)
    set_year(id3, year)
    set_genre(id3, genre)
    id3.save()

    print 'Track: %s' % id3['TIT2'].text[0]
    print 'Artist: %s' % id3['TPE2'].text[0]
    print 'Album: %s' % id3['TALB'].text[0]
    print 'Year: %s' % id3['TDRC'].text[0]
    print 'Number: %s' % id3['TRCK'].text[0]

def set_tags(mp3):
    try:
        mp3.add_tags()
    except error as e:
        print e
        pass

def set_album_art(mp3, art_path, image_type):
    try:
        mp3.tags.add(
            APIC(
                encoding = 3, # 3 is for utf-8
                mime = image_type, # image/jpeg or image/png
                type = 3, # 3 is for the cover image
                desc = u'Cover',
                data = open(art_path).read()
            )
        )
    except error as e:
        print e
        pass

def set_album_name(id3, album_name):
    try:
        id3.add(TALB(encoding = 3, text = album_name))
    except error as e:
        print e
        pass


def set_title(id3, title):
    try:
        id3.add(TIT2(encoding = 3, text = title))
    except error as e:
        print e
        pass

def set_artist(id3, artist):
    try:
        id3.add(TPE2(encoding = 3, text = artist))
    except error as e:
        print e
        pass

def set_track_number(id3, track_number):
    try:
        id3.add(TRCK(encoding = 3, text = track_number))
    except error as e:
        print e
        pass

def set_year(id3, year):
    try:
        id3.add(TDRC(encoding = 3, text = year))
    except error as e:
        print e
        pass

def set_genre(id3, genre):
    try:
        id3.add(TCON(encoding = 3, text = genre))
    except error as e:
        print e
        pass

def download_file(url, path, title):
    if not os.path.exists(os.path.expanduser(path)):
        try:
            os.makedirs(os.path.expanduser(path))
        except error as e:
            print e
            pass

    download_url = urllib2.urlopen(url)
    file = open(os.path.expanduser(path + title), 'wb')
    meta = download_url.info()
    file_size = int(meta.getheaders('Content-Length')[0])

    print '\nDownloading: %s Size: %s' % (tcolors.BOLD + title + tcolors.END, tcolors.BOLD + str(round(file_size / float(1000000), 2)) + "MB" + tcolors.END)

    file_size_dl = 0
    block_sz = 8192

    while True:
        buffer = download_url.read(block_sz)

        if not buffer:
            break

        file_size_dl += len(buffer)
        file.write(buffer)
        status = r'%10d  [%3.2f%%]' % (file_size_dl, file_size_dl * 100. / file_size)
        status = status + chr(8)*(len(status)+1)

        print status,

    file.close()

def download_album(album_json):
    for t in album_json['trackinfo']:
        download_file(t['file']['mp3-128'], '~/Downloads/bandcamper-test/', create_file_name(t['track_num'], t['title']))

    return True

def create_file_name(track_number, title):
    return ('0' if track_number < 10 else '') + str(track_number) + ' ' + title + '.mp3'

def find_album_json(garbage):
    album_json = None
    track_regex = re.compile('trackinfo\s\:\s\[')

    artist_name = sanitise_variable(re.compile('artist:\s\"'), garbage)
    album_name = sanitise_variable(re.compile('album_title:\s\"'), garbage)
    album_art_url = sanitise_variable(re.compile('artFullsizeUrl:\s\"'), garbage)
    track_json = re.sub('\}\]', '}]}', re.sub(track_regex, '{\"trackinfo\":[', find_json(garbage, track_regex, ',', ']')))

    if artist_name and album_name and track_json:
        #todo: add artist name and album name to the json
        album_json = json.loads(track_json)

    return album_json

def sanitise_variable(regex, garbage):
    return re.sub('"', '', re.sub(regex, '', find_json(garbage, regex, ',', '"')))

def find_json(garbage, regex, nail, coffin):
    for m in regex.finditer(garbage):
        start = m.start()

{'fraction_non_alphanumeric': 0.13601090604026847, 'fraction_numerical': 0.027684563758389263, 'mean_word_length': 2.5178900774621913, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 38, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1591698', 'n_tokens_mistral': 3586, 'n_tokens_neox': 3421, 'n_words': 815}