def sync_playlists(self, old):
        print "Migrating playlists ..."
        prog = ProgressBar(0, self.count_table(old, 'playlists'), 77, mode='fixed')

        old.execute('select id, name, played, published, createdBy from playlists')
        for old_playlist in old.fetchall():
            try:
                playlist = Playlist.objects.get(id=old_playlist[0])
            except Playlist.DoesNotExist:
                playlist = Playlist(id=old_playlist[0])

            playlist.name = old_playlist[1]
            playlist.play_counter = old_playlist[2]
            playlist.creation_datetime = old_playlist[3] or datetime.datetime.now()
            playlist.creation_user = self.get_user(old_playlist[4])

            old.execute('''
select 
    pc.categoryId
from playlists_categories pc
where
    pc.playlistId = %s
''' % int(playlist.pk))

            for old_cat in old.fetchall():
                # skipping more dead relations weee
                if not old_cat[0]:
                    continue
                playlist.category = PlaylistCategory.objects.get(pk=old_cat[0])

            playlist.save()

            old.execute('''
select 
    userId 
from users_bookmarkedPlaylists
where
    playlistId = %s
''' % int(playlist.pk))

            for old_fan in old.fetchall():
                # skipping more dead relations weee
                if not old_fan[0]: continue

                fan = self.get_user(old_fan[0])
                playlist.fans.add(fan.playlistprofile)

            playlist.fans_count = playlist.fans.count()

            old.execute('''
select 
    t.name 
from playlists_tags pt
left join tags t on t.id = pt.tagId 
where
pt.playlistId = %s
''' % int(playlist.pk))
            playlist.tags = ','.join([i[0] for i in old.fetchall()])
            playlist.save()


            old.execute(int(playlist.pk) % 'select ps.songId, s.title, a.name from playlists_songs ps left join songs s on ps.songId = s.id left join artists a on a.id = s.artistId where ps.playlistId = %s and s.artistId = 0')
            for old_song in old.fetchall():
                # skipping more dead relations weee
                if not old_song[0]:
                    continue
                
                artist_name = old_song[2]
                if not artist_name:
                    artist_name = 'YouTube'

                try:
                    artist = Artist.objects.get(name__iexact=artist_name)
                except Artist.DoesNotExist:
                    artist = Artist(name=artist_name)
                    artist.save()

                try:
                    track = Track.objects.get(name__iexact=old_song[1], artist=artist)
                except Track.DoesNotExist:
                    track = Track(artist=artist, name=old_song[1])
                    track.save()

                playlist.tracks.add(track)

            playlist.tracks_count = playlist.tracks.count()
            playlist.save()
            
            prog.increment_amount()
            print prog, '\r',
            sys.stdout.flush()
dataset/ETHPy150Open jpic/playlistnow.fm/apps/playlist/management/commands/migrate_pln.py/Command.sync_playlists/swapped operands of `%`