<s> import sys <EOL> import datetime <EOL> import urllib <EOL> import simplejson <EOL> from django . core . management . base import BaseCommand , CommandError <EOL> from django import db <EOL> from django . contrib . auth . models import User <EOL> from actstream . models import follow <EOL> from django . db . models import signals <EOL> from django . conf import settings <EOL> from jpic . slughifi import slughifi <EOL> from jpic . progressbar import ProgressBar <EOL> from playlist . models import * <EOL> from music . models import * <EOL> from gfc . models import * <EOL> from socialregistration . models import * <EOL> import gfc <EOL> class Command ( BaseCommand ) : <EOL> args = '<STR_LIT>' <EOL> help = '<STR_LIT>' <EOL> def handle ( self , * args , ** options ) : <EOL> backend = db . load_backend ( '<STR_LIT>' ) <EOL> conn = backend . DatabaseWrapper ( { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT:root>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT:localhost>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> old = conn . cursor ( ) <EOL> self . old_root_ids = ( <NUM_LIT> , <NUM_LIT> ) <EOL> signals . pre_save . disconnect ( yourplaylist_bookmarked ) <EOL> signals . m2m_changed . disconnect ( update_counts ) <EOL> signals . post_save . disconnect ( new_follower , sender = Follow ) <EOL> signals . post_save . disconnect ( new_follow , sender = Follow ) <EOL> signals . post_save . disconnect ( playlist_create_activity , sender = Playlist ) <EOL> signals . m2m_changed . disconnect ( last_playlist , sender = Playlist . tracks . through ) <EOL> signals . pre_save . disconnect ( get_info_if_no_image ) <EOL> signals . m2m_changed . disconnect ( update_fans ) <EOL> self . sync_playlists ( old ) <EOL> def get_user ( self , pk ) : <EOL> if pk in self . old_root_ids : <EOL> return User . objects . get ( pk = settings . ROOT_USERID ) <EOL> else : <EOL> try : <EOL> return User . objects . get ( pk = pk ) <EOL> except User . DoesNotExist : <EOL> return User . objects . get ( pk = settings . ROOT_USERID ) <EOL> def count_table ( self , old , table ) : <EOL> old . execute ( '<STR_LIT>' % table ) <EOL> result = old . fetchone ( ) <EOL> return result [ <NUM_LIT:0> ] <EOL> def sync_friends ( self , old ) : <EOL> print "<STR_LIT>" <EOL> prog = ProgressBar ( <NUM_LIT:0> , self . count_table ( old , '<STR_LIT>' ) , <NUM_LIT> , mode = '<STR_LIT>' ) <EOL> old . execute ( '<STR_LIT>' ) <EOL> for old_friend in old . fetchall ( ) : <EOL> if not old_friend [ <NUM_LIT:0> ] : continue <EOL> if not old_friend [ <NUM_LIT:1> ] : continue <EOL> try : <EOL> user1 = self . get_user ( old_friend [ <NUM_LIT:0> ] ) <EOL> user2 = self . get_user ( old_friend [ <NUM_LIT:1> ] ) <EOL> follow ( user1 , user2 , False ) <EOL> follow ( user2 , user1 , False ) <EOL> except : <EOL> continue <EOL> prog . increment_amount ( ) <EOL> print prog , '<STR_LIT:\r>' , <EOL> sys . stdout . flush ( ) <EOL> def sync_followers ( self , old ) : <EOL> print "<STR_LIT>" <EOL> prog = ProgressBar ( <NUM_LIT:0> , self . count_table ( old , '<STR_LIT>' ) , <NUM_LIT> , mode = '<STR_LIT>' ) <EOL> old . execute ( '<STR_LIT>' ) <EOL> for old_follower in old . fetchall ( ) : <EOL> if not old_follower [ <NUM_LIT:0> ] : continue <EOL> if not old_follower [ <NUM_LIT:1> ] : continue <EOL> try : <EOL> user = self . get_user ( old_follower [ <NUM_LIT:0> ] ) <EOL> follower = self . get_user ( old_follower [ <NUM_LIT:1> ] ) <EOL> follow ( follower , user , False ) <EOL> except : <EOL> continue <EOL> prog . increment_amount ( ) <EOL> print prog , '<STR_LIT:\r>' , <EOL> sys . stdout . flush ( ) <EOL> def sync_categories ( self , old ) : <EOL> print "<STR_LIT>" <EOL> prog = ProgressBar ( <NUM_LIT:0> , self . count_table ( old , '<STR_LIT>' ) , <NUM_LIT> , mode = '<STR_LIT>' ) <EOL> old . execute ( '<STR_LIT>' ) <EOL> for old_category in old . fetchall ( ) : <EOL> category , created = PlaylistCategory . objects . get_or_create ( <EOL> pk = old_category [ <NUM_LIT:0> ] ) <EOL> category . name = old_category [ <NUM_LIT:4> ] <EOL> category . save ( ) <EOL> if old_category [ <NUM_LIT:2> ] : <EOL> category . parent , pcreated = PlaylistCategory . objects . get_or_create ( <EOL> pk = old_category [ <NUM_LIT:2> ] ) <EOL> category . parent . save ( ) <EOL> category . save ( ) <EOL> prog . increment_amount ( ) <EOL> print prog , '<STR_LIT:\r>' , <EOL> sys . stdout . flush ( ) <EOL> def sync_users_accounts ( self , old ) : <EOL> print "<STR_LIT>" <EOL> prog = ProgressBar ( <NUM_LIT:0> , self . count_table ( old , '<STR_LIT>' ) , <NUM_LIT> , mode = '<STR_LIT>' ) <EOL> old . execute ( '''<STR_LIT>''' ) <EOL> for old_user in old . fetchall ( ) : <EOL> if not old_user [ <NUM_LIT:1> ] : continue <EOL> if old_user [ <NUM_LIT:0> ] in self . old_root_ids : <EOL> user = User . objects . get ( pk = settings . ROOT_USERID ) <EOL> else : <EOL> user , created = User . objects . get_or_create ( pk = old_user [ <NUM_LIT:0> ] ) <EOL> count = <NUM_LIT:0> <EOL> user . username = slughifi ( old_user [ <NUM_LIT:1> ] ) [ : <NUM_LIT:30> ] or count <EOL> while User . objects . filter ( username = user . username ) . exclude ( pk = user . pk ) . count ( ) : <EOL> user . username = '<STR_LIT>' % ( slughifi ( old_user [ <NUM_LIT:1> ] ) [ : <NUM_LIT> ] , count ) <EOL> count += <NUM_LIT:1> <EOL> user . email = old_user [ <NUM_LIT:2> ] or '<STR_LIT>' <EOL> user . first_name = old_user [ <NUM_LIT:1> ] <EOL> user . save ( ) <EOL> user . playlistprofile . points = old_user [ <NUM_LIT:5> ] or <NUM_LIT:0> <EOL> if old_user [ <NUM_LIT:3> ] : <EOL> user . playlistprofile . user_location = old_user [ <NUM_LIT:3> ] <EOL> if old_user [ <NUM_LIT:4> ] : <EOL> user . playlistprofile . avatar_url = old_user [ <NUM_LIT:4> ] <EOL> user . playlistprofile . save ( ) <EOL> old . execute ( '<STR_LIT>' + str ( old_user [ <NUM_LIT:0> ] ) ) <EOL> for old_account in old . fetchall ( ) : <EOL> if old_account [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> p , created = FacebookProfile . objects . get_or_create ( uid = old_account [ <NUM_LIT:2> ] , user = user ) <EOL> upstream = None <EOL> try : <EOL> filename , message = urllib . urlretrieve ( '<STR_LIT>' % old_account [ <NUM_LIT:2> ] ) <EOL> fp = open ( filename ) <EOL> upstream = simplejson . load ( fp ) <EOL> fp . close ( ) <EOL> except : <EOL> pass <EOL> if upstream : <EOL> p . nick = upstream [ '<STR_LIT:name>' ] <EOL> if '<STR_LIT>' in upstream : <EOL> p . url = upstream [ '<STR_LIT>' ] <EOL> else : <EOL> p . url = '<STR_LIT>' % old_account [ <NUM_LIT:2> ] <EOL> p . save ( ) <EOL> elif old_account [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> p , created = TwitterProfile . objects . get_or_create ( twitter_id = old_account [ <NUM_LIT:2> ] , user = user ) <EOL> old . execute ( '<STR_LIT>' % old_account [ <NUM_LIT:2> ] ) <EOL> old_profile = old . fetchone ( ) <EOL> if old_profile is not None : <EOL> p . access_token = old_profile [ <NUM_LIT:1> ] <EOL> p . token_secret = old_profile [ <NUM_LIT:2> ] <EOL> p . nick = old_account [ <NUM_LIT:5> ] . replace ( '<STR_LIT>' , '<STR_LIT>' ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> p . url = old_account [ <NUM_LIT:5> ] <EOL> p . save ( ) <EOL> elif old_account [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> p , created = GfcProfile . objects . get_or_create ( uid = old_account [ <NUM_LIT:2> ] , user = user ) <EOL> p . save ( ) <EOL> prog . increment_amount ( ) <EOL> print prog , '<STR_LIT:\r>' , <EOL> sys . stdout . flush ( ) <EOL> def sync_tiny_playlist ( self , old ) : <EOL> print "<STR_LIT>" <EOL> prog = ProgressBar ( <NUM_LIT:0> , self . count_table ( old , '<STR_LIT>' ) , <NUM_LIT> , mode = '<STR_LIT>' ) <EOL> old . execute ( '<STR_LIT>' ) <EOL> for old_liked in old . fetchall ( ) : <EOL> if old_liked [ <NUM_LIT:0> ] == <NUM_LIT:0> : <EOL> continue <EOL> try : <EOL> user = self . get_user ( old_liked [ <NUM_LIT:0> ] ) <EOL> except User . DoesNotExist : <EOL> prog . increment_amount ( ) <EOL> print prog , '<STR_LIT:\r>' , <EOL> sys . stdout . flush ( ) <EOL> continue <EOL> if not old_liked [ <NUM_LIT:1> ] : <EOL> prog . increment_amount ( ) <EOL> print prog , '<STR_LIT:\r>' , <EOL> sys . stdout . flush ( ) <EOL> continue <EOL> artist_name = old_liked [ <NUM_LIT:2> ] <EOL> if not artist_name : <EOL> artist_name = '<STR_LIT>' <EOL> try : <EOL> artist = Artist . objects . get ( name__iexact = artist_name ) <EOL> except Artist . DoesNotExist : <EOL> artist = Artist ( name = artist_name ) <EOL> artist . save ( ) <EOL> try : <EOL> track = Track . objects . get ( name__iexact = old_liked [ <NUM_LIT:1> ] , artist = artist ) <EOL> except Track . DoesNotExist : <EOL> track = Track ( artist = artist , name = old_liked [ <NUM_LIT:1> ] ) <EOL> track . save ( ) <EOL> try : <EOL> user . playlistprofile . tiny_playlist . tracks . add ( track ) <EOL> except PlaylistProfile . DoesNotExist : <EOL> user . save ( ) <EOL> user . playlistprofile . tiny_playlist . tracks . add ( track ) <EOL> prog . increment_amount ( ) <EOL> print prog , '<STR_LIT:\r>' , <EOL> sys . stdout . flush ( ) <EOL> def sync_users_fans ( self , old ) : <EOL> print "<STR_LIT>" <EOL> prog = ProgressBar ( <NUM_LIT:0> , self . count_table ( old , '<STR_LIT>' ) , <NUM_LIT> , mode = '<STR_LIT>' ) <EOL> old . execute ( '<STR_LIT>' ) <EOL> for old_activity in old . fetchall ( ) : <EOL> user = self . get_user ( old_activity [ <NUM_LIT:1> ] ) <EOL> if old_activity [ <NUM_LIT:5> ] == <NUM_LIT:9> : <EOL> artist = Artist . objects . get ( pk = old_activity [ <NUM_LIT:6> ] ) <EOL> artist . fans . add ( user . playlistprofile ) <EOL> else : <EOL> print '<STR_LIT>' <EOL> prog . increment_amount ( ) <EOL> print prog , '<STR_LIT:\r>' , <EOL> sys . stdout . flush ( ) <EOL> def sync_artists ( self , old ) : <EOL> print "<STR_LIT>" <EOL> prog = ProgressBar ( <NUM_LIT:0> , self . count_table ( old , '<STR_LIT>' ) , <NUM_LIT> , mode = '<STR_LIT>' ) <EOL> old . execute ( '<STR_LIT>' ) <EOL> for old_activity in old . fetchall ( ) : <EOL> if not old_activity [ <NUM_LIT:2> ] : continue <EOL> try : <EOL> artist = Artist . objects . get ( name__iexact = old_activity [ <NUM_LIT:2> ] ) <EOL> except Artist . DoesNotExist : <EOL> artist = Artist ( name = old_activity [ <NUM_LIT:2> ] ) <EOL> artist . save ( ) <EOL> if not artist . image_small : <EOL> artist . name = old_activity [ <NUM_LIT:2> ] <EOL> artist . lastfm_get_info ( ) <EOL> artist . name = old_activity [ <NUM_LIT:2> ] <EOL> artist . save ( ) <EOL> try : <EOL> user = self . get_user ( old_activity [ <NUM_LIT:0> ] ) <EOL> except User . DoesNotExist : <EOL> continue <EOL> artist . fans . add ( user . playlistprofile ) <EOL> prog . increment_amount ( ) <EOL> print prog , '<STR_LIT:\r>' , <EOL> sys . stdout . flush ( ) <EOL> def sync_playlists ( self , old ) : <EOL> print "<STR_LIT>" <EOL> prog = ProgressBar ( <NUM_LIT:0> , self . count_table ( old , '<STR_LIT>' ) , <NUM_LIT> , mode = '<STR_LIT>' ) <EOL> old . execute ( '<STR_LIT>' ) <EOL> for old_playlist in old . fetchall ( ) : <EOL> try : <EOL> playlist = Playlist . objects . get ( id = old_playlist [ <NUM_LIT:0> ] ) <EOL> except Playlist . DoesNotExist : <EOL> playlist = Playlist ( id = old_playlist [ <NUM_LIT:0> ] ) <EOL> playlist . name = old_playlist [ <NUM_LIT:1> ] <EOL> playlist . play_counter = old_playlist [ <NUM_LIT:2> ] <EOL> playlist . creation_datetime = old_playlist [ <NUM_LIT:3> ] or datetime . datetime . now ( ) <EOL> playlist . creation_user = self . get_user ( old_playlist [ <NUM_LIT:4> ] ) <EOL> old . execute ( '''<STR_LIT>''' % int ( playlist . pk ) ) <EOL> for old_cat in old . fetchall ( ) : <EOL> if not old_cat [ <NUM_LIT:0> ] : <EOL> continue <EOL> playlist . category = PlaylistCategory . objects . get ( pk = old_cat [ <NUM_LIT:0> ] ) <EOL> playlist . save ( ) <EOL> old . execute ( '''<STR_LIT>''' % int ( playlist . pk ) ) <EOL> for old_fan in old . fetchall ( ) : <EOL> if not old_fan [ <NUM_LIT:0> ] : continue <EOL> fan = self . get_user ( old_fan [ <NUM_LIT:0> ] ) <EOL> playlist . fans . add ( fan . playlistprofile ) <EOL> playlist . fans_count = playlist . fans . count ( ) <EOL> old . execute ( '''<STR_LIT>''' % int ( playlist . pk ) ) <EOL> playlist . tags = '<STR_LIT:U+002C>' . join ( [ i [ <NUM_LIT:0> ] for i in old . fetchall ( ) ] ) <EOL> playlist . save ( ) <EOL> old . execute ( '<STR_LIT>' % int ( playlist . pk ) ) <EOL> for old_song in old . fetchall ( ) : <EOL> if not old_song [ <NUM_LIT:0> ] : <EOL> continue <EOL> artist_name = old_song [ <NUM_LIT:2> ] <EOL> if not artist_name : <EOL> artist_name = '<STR_LIT>' <EOL> try : <EOL> artist = Artist . objects . get ( name__iexact = artist_name ) <EOL> except Artist . DoesNotExist : <EOL> artist = Artist ( name = artist_name ) <EOL> artist . save ( ) <EOL> try : <EOL> track = Track . objects . get ( name__iexact = old_song [ <NUM_LIT:1> ] , artist = artist ) <EOL> except Track . DoesNotExist : <EOL> track = Track ( artist = artist , name = old_song [ <NUM_LIT:1> ] ) <EOL> track . save ( ) <EOL> playlist . tracks . add ( track ) <EOL> playlist . tracks_count = playlist . tracks . count ( ) <EOL> playlist . save ( ) <EOL> prog . increment_amount ( ) <EOL> print prog , '<STR_LIT:\r>' , <EOL> sys . stdout . flush ( ) <EOL> def sync_tracks ( self , old ) : <EOL> print "<STR_LIT>" <EOL> prog = ProgressBar ( <NUM_LIT:0> , self . count_table ( old , '<STR_LIT>' ) , <NUM_LIT> , mode = '<STR_LIT>' ) <EOL> old . execute ( '''<STR_LIT>''' ) <EOL> for old_track in old . fetchall ( ) : <EOL> try : <EOL> artist = Artist . objects . get ( name__iexact = old_track [ <NUM_LIT:5> ] ) <EOL> except Artist . DoesNotExist : <EOL> artist = Artist ( name = old_track [ <NUM_LIT:5> ] ) <EOL> artist . save ( ) <EOL> try : <EOL> track = Track . objects . get ( name__iexact = old_track [ <NUM_LIT:1> ] ) <EOL> except Track . DoesNotExist : <EOL> track = Track ( name = old_track [ <NUM_LIT:1> ] , artist = artist ) <EOL> track . play_counter = old_track [ <NUM_LIT:2> ] <EOL> track . youtube_id = old_track [ <NUM_LIT:3> ] <EOL> track . youtube_bugs = old_track [ <NUM_LIT:6> ] <EOL> track . save ( ) <EOL> prog . increment_amount ( ) <EOL> print prog , '<STR_LIT:\r>' , <EOL> sys . stdout . flush ( ) </s>
