Over a million developers have joined DZone.


This prints a list of songs from the iTunes db based on search terms
that you place inside the program for each run.  Crude, I know...

Works fine, though.

Requires python3.1

#!/usr/bin/env python3.1
# scarf_song_titles.py
# rev 1 (rev 0 lives at http://snippets.dzone.com/user/paul618)
# written just for fun, by xlforum.net/paul618 AKA L3P7ON

# PM me on xlforum.net with bugs or suggestions

# Q:: Isn"t this cheating?
# A:: Not if everybody was using it.

# This runs on python3.1 on any platform you can imagine.
# Tested on a Mac (v. 10.6.3)
# the paths defined in this section might be different on a PC or Gnu/Linux

# To run this on a Mac, Gnu/Linux or PC:
# 0) uncomment the  two  lines below the WARNING.
#    this gives the program PERMISSION to read your db and write a datafile

# 1) download python3.1
# 2) paste this program into a file called scarf_song_titles.py
# 3) fire up IDLE at Applications/Python 3.1/IDLE
# 4) File/Open scarf_song_titles.py
# 5) put your search criteria into two spots indicated
# 5) hit F5 to Run
# 6) click in the IDLE and goto step 5

# USE AT YOUR OWN RISK! The author does not warrent the reliability of this program.

# IFF you will allow this program to read your iTunes_Music_Library.xml
# fix the path and uncomment the next line...
#XMLfile = "/Users/paul/Music/iTunes/*.xml"

# I have to create an ASCII-only version of that file to avoid a program error.

# IFF you will allow this program to create such a file
# fix the path and uncomment the next line...
#datafile = "/Users/paul/song_title_game_data.xml"

# remove this file after you have installed some new tunes
# so that my datafile will be re-created

# --------------------------------------------------------------------------------------------

searchstring0 = "Red"    # PUT YOUR FIRST  SEARCHSTRING HERE BEFORE EACH RUN; hit command-s F5
searchstring1 = "Rain"   # PUT YOUR SECOND SEARCHSTRING HERE BEFORE EACH RUN; hit command-s F5

# --------------------------------------------------------------------------------------------

import re, os, sys, stat, time, struct
import glob

# you can subsitiute your own favs here
Amazons = ("Joni M", "Emmylou", "Rickie ", "Natalie", "Avril L", "Alison", "Gillian", "Cindi L"
           "Bonnie R", "Flora P", "Gillian", "Maura M", "Lucinda", "Sheryl C", "Alanis", "Patti S")

# ---- subrs

def uniqify(items):
    seen = set()
    ret = []
    for i in items:
        if not i in seen:
    return ret

def cleanline(L):
    L = L.replace("&", "and")
    i = L.rindex("")
    return L[i+8:-10]   # trimmed

def report(data):    
    d = uniqify(data)
    for s, t in d:
        # bug may not support multiple ppl doin the same song...
        print(" %s -- %s" % (s, t))
        #print("site:lyrics.filestube.com %s -- %s" % (s, t))

# ---- end subr

# datafile will be the iTunes XML file cleaned of non-ASCII chars
# this is only created once, at the path defined in the preamble

if not os.path.exists(datafile):
    # datafile has not been created yet
    fn = glob.glob(XMLfile)[0]
    if 1: print("fn = >>>%s<<<" % (fn))
    buffersize = os.stat(fn)[stat.ST_SIZE]
    print("Your XML db is %d bytes.\nCreating %s.\nProcessing 160 songs per second...." % (buffersize, datafile))

    buffersize *= 1.1  # do I need this?
    buffersize = int(buffersize)
    # read
    with open(fn, 'rb', buffersize) as fh0: 
        red_data = fh0.read()

    # write
    fh1 = open(datafile, "wb")
    for byte in red_data:
        # delete non-ASCII chars
        if -128 <= byte <= 127:
            c = struct.pack('b', byte)

    time.sleep(.05)  # let it close

# ---- main

dolls = []
guys = []
title = "" # need this binding here
toggle = True

pattern0  = "Name"
pattern1  = "Artist"
q_pattern = "Playlists"

print("Searching %s. This will take a minute..." % (datafile))

file = open(datafile, "r")
for line in file.readlines():
    mo2 = re.search(searchstring0, line)   #, flags=re.IGNORECASE)  you want it to match case
    mo3 = re.search(searchstring1, line)   #, flags=re.IGNORECASE)

    mo0 = re.search(pattern0, line)   # Title
    mo1 = re.search(pattern1, line)   # Artist
    mo4 = re.search(q_pattern, line)  # quit before examining Playlists

    if mo4:
        continue  # lookin at Playlists, which are past the data of interest
    if mo0 and (mo2 or mo3):
        # collect song title
        title = cleanline(line)
        toggle = True  # prepare to print the Artist
    if mo1 and toggle:
        if 0: print("I see an Artist for %s" % (title))
        # collect artist
        artist = cleanline(line)
        if 0: print(" %s --> %s " % (title, artist))
        flag = False
        for gf in Amazons:
            if artist.startswith(gf):
                if 0: print("I see an Amazon: %s -- %s" % (title, artist))
                dolls.append((title, artist))
                if 0: print("I see somebody: %s -- %s" % (title, artist))
        # for/else!!!!!!!!!!!!!!  my first one ever. woohoo!!!!!!!!!!!!!!
            guys.append((title, artist))

        toggle = False

print("---- Amazons:")

print("---- everybody else:")
print("---- done.")

# ---- 
# ----

mail = '''
> How do I write binary data to a file?
> I can open a file with 'wb' as mode but if I do:
> b = open('bin.dat','wb')
> for i in range(50): b.write(i)
> I get an error.

This is an error because the write method can only output a string.
Strings in Python are not strictly 7-bit characters, but 8-bit bytes.
So you can use the struct module to convert Python data into binary
strings, which can then be written to the file.

>>> import struct
>>> binfile = open('bin.dat', 'wb')
>>> for num in range(50):
...   data = struct.pack('i', num)
...   binfile.write(data)
>>> binfile = open('bin.dat', 'rb')
>>> intsize = struct.calcsize('i')
>>> while 1:
>>>   data = binfile.read(intsize)
...   if data == '':
...     break
...   num = struct.unpack('i', data)
...   print num

typical_record = '''
                        Track ID2056
                        ArtistCharlie Parker
                        ComposerParker, C. - Harris, B.
                        AlbumKen Burns Jazz
                        KindAAC audio file
                        Total Time181026
                        Track Number7
                        Track Count16
                        Date Modified2008-06-06T20:07:06Z
                        Date Added2007-03-01T21:59:06Z
                        Bit Rate128
                        Sample Rate44100
                        Play Count3
                        Play Date3350641115
                        Play Date UTC2010-03-05T17:38:35Z
                        Persistent ID4BA3750063A2DDFC
                        Track TypeFile
                        File Type1295270176
                        File Folder Count4
                        Library Folder Count1


Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}