示例#1
0
def fetchMovieLikes(data):
    usr_id, fid_list, graph,i = data
    NUM_CATEGORIES=4
    batchstr_array = []
    for fid in fid_list:
        batchstr_array.append('{"method":"GET","relative_url":"%s/movies?fields=link,name,category,likes,app_id,release_date"},{"method":"GET","relative_url":"%s/music?fields=link,name,category,likes,app_id,release_date"},{"method":"GET","relative_url":"%s/books?fields=link,name,category,likes,app_id,release_date"},{"method":"GET","relative_url":"%s/television?fields=link,name,category,likes,app_id,release_date"}' %(fid,fid,fid,fid))   
    batchstr = '['+ ','.join(batchstr_array) + ']'
    #print batchstr
    postdata={'batch':batchstr}
    db_insert_list=[]
    try:
        fb_response= graph.request(post_data=postdata)
       
        #print "yo, look mamovies",type(fb_response[0]['body'])
        
        for j in range(len(fid_list)):
            insert_items_list = []
            for k in range(NUM_CATEGORIES):
                fr_items_str = fb_response[j*NUM_CATEGORIES+k]['body']
                fr_items = json.loads(fr_items_str)
                #print fr_movies['data']
                #['body']['data']
                if (fr_items['data'] is not None) and (len(fr_items['data']) > 0):
                    insert_items_list.extend(fr_items['data'])
                    #FacebookUserConverter.store_foreachfriend(usr_id, fid_list[j], fr_items['data'])
            db_inserts = FacebookUserConverter.store_foreachfriend(usr_id, fid_list[j], insert_items_list)
            db_insert_list.extend(db_inserts)
           
    except (TypeError,IOError) as e:
        print "Type error", e 
        
    try:
        insert_many(db_insert_list)
    except IntegrityError,ie:
        print "in fetchMovieLikes(api.py):",ie
示例#2
0
    def handle(self, *args, **options):
        """
        Sync images on the filesystem with the record of images in the database.

        Any images on the filesystem that don't exist in the database should have
        new database entries. Any images in the database that no longer exist on the
        filesystem should be deleted from the database.
        """
        print "Syncing path %s to database %s" % (self.path, self.database)
        if options["dry_run"]:
            print "Dry run"
        else:
            print "Real thing"

        root_path, base_path = os.path.split(self.path)
        files = self.get_files(self.path)
        files = [os.path.join(base_path, filename) for filename in files]

        # Delete image records that don't have files on the filesystem.
        docs = SpeciesImage.objects.exclude(image__in=files)
        logging.info("Deleting %i files from database", docs.count())

        if options["dry_run"]:
            print "Deleting %i files from database" % docs.count()
            print docs
        else:
            docs.delete()

        # Update images in the database that have changed on the filesystem.
        files_in_db = set(
            SpeciesImage.objects.filter(image__in=files).values_list(
                "image", flat=True))
        files = set(files)
        files_not_in_db = files - files_in_db

        # Create SpeciesImage instances for files that are in the filesystem but not
        # the database.
        objects = []
        for filename in files_not_in_db:
            if filename.endswith(("medium", "thumbnail", "Thumbs.db")):
                continue

            kwargs = {
                "image": filename,
                "species": self.get_species_by_filename(filename)
            }
            objects.append(SpeciesImage(**kwargs))

        if options["dry_run"]:
            print "Inserting %i objects into %s" % (len(objects),
                                                    self.database)
            print objects
        else:
            # Save new records in one query.
            insert_many(objects, self.database)
示例#3
0
    def handle(self, *args, **options):
        """
        Sync images on the filesystem with the record of images in the database.

        Any images on the filesystem that don't exist in the database should have
        new database entries. Any images in the database that no longer exist on the
        filesystem should be deleted from the database.
        """
        print "Syncing path %s to database %s" % (self.path, self.database)
        if options["dry_run"]:
            print "Dry run"
        else:
            print "Real thing"

        root_path, base_path = os.path.split(self.path)
        files = self.get_files(self.path)
        files = [os.path.join(base_path, filename) for filename in files]

        # Delete image records that don't have files on the filesystem.
        docs = SpeciesImage.objects.exclude(image__in=files)
        logging.info("Deleting %i files from database", docs.count())

        if options["dry_run"]:
            print "Deleting %i files from database" % docs.count()
            print docs
        else:
            docs.delete()

        # Update images in the database that have changed on the filesystem.
        files_in_db = set(SpeciesImage.objects.filter(image__in=files).values_list("image", flat=True))
        files = set(files)
        files_not_in_db = files - files_in_db

        # Create SpeciesImage instances for files that are in the filesystem but not
        # the database.
        objects = []
        for filename in files_not_in_db:
            if filename.endswith(("medium", "thumbnail", "Thumbs.db")):
                continue

            kwargs = {
                "image": filename,
                "species": self.get_species_by_filename(filename)
            }
            objects.append(SpeciesImage(**kwargs))

        if options["dry_run"]:
            print "Inserting %i objects into %s" % (len(objects), self.database)
            print objects
        else:
            # Save new records in one query.
            insert_many(objects, self.database)
示例#4
0
 def _store_likes(self, user, likes):
     if likes:
         from django_facebook.models import FacebookLike
         from dateutil.parser import parse
         #base_queryset = FacebookLike.objects.filter(user_id=user.id)
         #global_defaults = dict(user_id=user.id)
         #id_field = 'facebook_id'
         #default_dict = {}
         insert_list=[]
         for like in likes:
             name = like.get('name')
             pic = like.get('picture')
             page_url = like.get('link')
             num_likes = like.get('likes')
             
             if like.get('app_id') is not None:
                 continue
                 
             release_date_string = like.get('release_date')
             created_time_string = like.get('created_time')
             created_time = None
             release_date=None
             if created_time_string:
                 created_time = datetime.datetime.strptime(like['created_time'], "%Y-%m-%dT%H:%M:%S+0000")
             if release_date_string:
                 try:
                     release_date = parse(like['release_date'])
                 except ValueError, ve:
                     release_date = None
             new_like_item = FacebookLike(
                 user_id=user.id,
                 facebook_id=like['id'],
                 created_time=created_time,
                 category=like.get('category'),
                 name=name,
                 pic=pic,
                 page_url=page_url,
                 num_likes=num_likes,
                 release_date=release_date
             )
             insert_list.append(new_like_item)
         
         try:
             insert_many(insert_list)
         except IntegrityError, ie:
             print "in _store_likes:",ie
示例#5
0
    def _store_likes(self, user, likes):
        if likes:
            from django_facebook.models import FacebookLike
            from dateutil.parser import parse
            #base_queryset = FacebookLike.objects.filter(user_id=user.id)
            #global_defaults = dict(user_id=user.id)
            #id_field = 'facebook_id'
            #default_dict = {}
            insert_list = []
            for like in likes:
                name = like.get('name')
                pic = like.get('picture')
                page_url = like.get('link')
                num_likes = like.get('likes')

                if like.get('app_id') is not None:
                    continue

                release_date_string = like.get('release_date')
                created_time_string = like.get('created_time')
                created_time = None
                release_date = None
                if created_time_string:
                    created_time = datetime.datetime.strptime(
                        like['created_time'], "%Y-%m-%dT%H:%M:%S+0000")
                if release_date_string:
                    try:
                        release_date = parse(like['release_date'])
                    except ValueError, ve:
                        release_date = None
                new_like_item = FacebookLike(user_id=user.id,
                                             facebook_id=like['id'],
                                             created_time=created_time,
                                             category=like.get('category'),
                                             name=name,
                                             pic=pic,
                                             page_url=page_url,
                                             num_likes=num_likes,
                                             release_date=release_date)
                insert_list.append(new_like_item)

            try:
                insert_many(insert_list)
            except IntegrityError, ie:
                print "in _store_likes:", ie
示例#6
0
def newUser(request):
    Username=request.POST['username']
    Pseudonyme=request.POST['pseudonyme']
    Langue=request.POST['langue']
    newuser=User(username=Username,pseudonyme=Pseudonyme,langue=Langue)
    newuser.save()
    response = HttpResponseRedirect("/jeu/")
    
    votes=get_list_or_404(Vote,question_id=4)
    scoreList=[]
    for vote in votes:
        scoreList.append(ScoreDep(user=newuser,score=0,name=vote.name))
    insert_many(scoreList)
    response.set_cookie('username', Username)
    
    
    
    return response
示例#7
0
 def _store_friends(self, user, friends):
     from django_facebook.models import FacebookUser
     #store the users for later retrieval
     if friends:
         #see which ids this user already stored
         #base_queryset = FacebookUser.objects.filter(user_id=user.id)
         #global_defaults = dict(user_id=user.id)
         #default_dict = {}
         insert_list=[]
         for f in friends:
             name = f.get('name')
             new_fruser = FacebookUser(user_id=user.id, name=name, facebook_id=str(f['id']))
             insert_list.append(new_fruser)
             #default_dict[str(f['id'])] = dict(name=name)
             
         #id_field = 'facebook_id'
         try:
             insert_many(insert_list)
         except IntegrityError, ie:
             print "In _store_friends, IntegrityError:", ie
         """
示例#8
0
def fetchMovieLikes(data):
    usr_id, fid_list, graph, i = data
    NUM_CATEGORIES = 4
    batchstr_array = []
    for fid in fid_list:
        batchstr_array.append(
            '{"method":"GET","relative_url":"%s/movies?fields=link,name,category,likes,app_id,release_date"},{"method":"GET","relative_url":"%s/music?fields=link,name,category,likes,app_id,release_date"},{"method":"GET","relative_url":"%s/books?fields=link,name,category,likes,app_id,release_date"},{"method":"GET","relative_url":"%s/television?fields=link,name,category,likes,app_id,release_date"}'
            % (fid, fid, fid, fid))
    batchstr = '[' + ','.join(batchstr_array) + ']'
    #print batchstr
    postdata = {'batch': batchstr}
    db_insert_list = []
    try:
        fb_response = graph.request(post_data=postdata)

        #print "yo, look mamovies",type(fb_response[0]['body'])

        for j in range(len(fid_list)):
            insert_items_list = []
            for k in range(NUM_CATEGORIES):
                fr_items_str = fb_response[j * NUM_CATEGORIES + k]['body']
                fr_items = json.loads(fr_items_str)
                #print fr_movies['data']
                #['body']['data']
                if (fr_items['data']
                        is not None) and (len(fr_items['data']) > 0):
                    insert_items_list.extend(fr_items['data'])
                    #FacebookUserConverter.store_foreachfriend(usr_id, fid_list[j], fr_items['data'])
            db_inserts = FacebookUserConverter.store_foreachfriend(
                usr_id, fid_list[j], insert_items_list)
            db_insert_list.extend(db_inserts)

    except (TypeError, IOError) as e:
        print "Type error", e

    try:
        insert_many(db_insert_list)
    except IntegrityError, ie:
        print "in fetchMovieLikes(api.py):", ie
示例#9
0
    def _store_friends(self, user, friends):
        from django_facebook.models import FacebookUser
        #store the users for later retrieval
        if friends:
            #see which ids this user already stored
            #base_queryset = FacebookUser.objects.filter(user_id=user.id)
            #global_defaults = dict(user_id=user.id)
            #default_dict = {}
            insert_list = []
            for f in friends:
                name = f.get('name')
                new_fruser = FacebookUser(user_id=user.id,
                                          name=name,
                                          facebook_id=str(f['id']))
                insert_list.append(new_fruser)
                #default_dict[str(f['id'])] = dict(name=name)

            #id_field = 'facebook_id'
            try:
                insert_many(insert_list)
            except IntegrityError, ie:
                print "In _store_friends, IntegrityError:", ie
            """