def handle(self, **options): if not settings.MEDIA_ROOT: raise ImproperlyConfigured( 'Please set MEDIA_ROOT in your settings file') remote_url = options['remote_url'] try: val = URLValidator() val(remote_url) except ValidationError: raise CommandError('Please enter a valid URL') exercise_api = "{0}/api/v2/exercise/?limit=999" image_api = "{0}/api/v2/exerciseimage/?exercise={1}" thumbnail_api = "{0}/api/v2/exerciseimage/{1}/thumbnails/" headers = { 'User-agent': default_user_agent('wger/{} + requests'.format(get_version())) } # Get all exercises result = requests.get( exercise_api.format(remote_url), headers=headers).json() for exercise_json in result['results']: exercise_name = exercise_json['name'].encode('utf-8') exercise_uuid = exercise_json['uuid'] exercise_id = exercise_json['id'] self.stdout.write('') self.stdout.write( u"*** Processing {0} (ID: {1}, UUID: {2})".format( exercise_name, exercise_id, exercise_uuid)) try: exercise = Exercise.objects.get(uuid=exercise_uuid) except Exercise.DoesNotExist: self.stdout.write( ' Remote exercise not found in local DB, skipping...') continue # Get all images images = requests.get( image_api.format(remote_url, exercise_id), headers=headers).json() if images['count']: for image_json in images['results']: image_id = image_json['id'] result = requests.get( thumbnail_api.format(remote_url, image_id), headers=headers).json() image_name = os.path.basename(result['original']) self.stdout.write(' Fetching image {0} - {1}'.format( image_id, image_name)) try: image = ExerciseImage.objects.get(pk=image_id) self.stdout.write( ' --> Image already present locally, skipping...' ) continue except ExerciseImage.DoesNotExist: self.stdout.write( ' --> Image not found in local DB, creating now...' ) image = ExerciseImage() image.pk = image_id # Save the downloaded image, see link for details # http://stackoverflow.com/questions/1308386/programmatically-saving-image-to- retrieved_image = requests.get( result['original'], headers=headers) img_temp = NamedTemporaryFile(delete=True) img_temp.write(retrieved_image.content) img_temp.flush() image.exercise = exercise image.is_main = image_json['is_main'] image.status = image_json['status'] image.image.save( os.path.basename(image_name), File(img_temp), ) image.save() else: self.stdout.write( ' No images for this exercise, nothing to do')
def handle(self, *args, **options): if not settings.MEDIA_ROOT: raise ImproperlyConfigured( 'Please set MEDIA_ROOT in your settings file') remote_url = options['remote_url'] try: val = URLValidator() val(remote_url) except ValidationError: raise CommandError('Please enter a valid URL') exercise_api = "{0}/api/v2/exercise/?limit=999" image_api = "{0}/api/v2/exerciseimage/?exercise={1}" thumbnail_api = "{0}/api/v2/exerciseimage/{1}/thumbnails/" # Get all exercises result = json.load(urllib.urlopen(exercise_api.format(remote_url))) for exercise_json in result['results']: exercise_name = exercise_json['name'] exercise_uuid = exercise_json['uuid'] exercise_id = exercise_json['id'] self.stdout.write('') self.stdout.write(u"*** Processing {0} (ID: {1})".format( exercise_name, exercise_id)) try: exercise = Exercise.objects.get(uuid=exercise_uuid) except Exercise.DoesNotExist: self.stdout.write( ' Remote exercise not found in local DB, skipping...') continue # Get all images images = json.load( urllib.urlopen(image_api.format(remote_url, exercise_id))) if images['count']: for image_json in images['results']: image_id = image_json['id'] result = json.load( urllib.urlopen( thumbnail_api.format(remote_url, image_id))) image_name = os.path.basename(result['original']) self.stdout.write(' Fetching image {0} - {1}'.format( image_id, image_name)) try: image = ExerciseImage.objects.get(pk=image_id) self.stdout.write( ' --> Image already present locally, skipping...' ) continue except ExerciseImage.DoesNotExist: self.stdout.write( ' --> Image not found in local DB, creating now...' ) image = ExerciseImage() image.pk = image_id # Save the downloaded image, see link for details # http://stackoverflow.com/questions/1308386/programmatically-saving-image-to- retrieved_image = urllib.urlretrieve(result['original']) image.exercise = exercise image.is_main = image_json['is_main'] image.status = image_json['status'] image.image.save(os.path.basename(image_name), File(open(retrieved_image[0]), 'rb')) image.save() else: self.stdout.write( ' No images for this exercise, nothing to do')
def handle(self, **options): if not settings.MEDIA_ROOT: raise ImproperlyConfigured('Please set MEDIA_ROOT in your settings file') remote_url = options['remote_url'] try: val = URLValidator() val(remote_url) except ValidationError: raise CommandError('Please enter a valid URL') exercise_api = "{0}/api/v2/exercise/?limit=999&status=2" image_api = "{0}/api/v2/exerciseimage/?exercise={1}" thumbnail_api = "{0}/api/v2/exerciseimage/{1}/thumbnails/" headers = {'User-agent': default_user_agent('wger/{} + requests'.format(get_version()))} # Get all exercises result = requests.get(exercise_api.format(remote_url), headers=headers).json() for exercise_json in result['results']: exercise_name = exercise_json['name'].encode('utf-8') exercise_uuid = exercise_json['uuid'] exercise_id = exercise_json['id'] self.stdout.write('') self.stdout.write(u"*** Processing {0} (ID: {1}, UUID: {2})".format(exercise_name, exercise_id, exercise_uuid)) try: exercise = Exercise.objects.get(uuid=exercise_uuid) except Exercise.DoesNotExist: self.stdout.write(' Remote exercise not found in local DB, skipping...') continue # Get all images images = requests.get(image_api.format(remote_url, exercise_id), headers=headers).json() if images['count']: for image_json in images['results']: image_id = image_json['id'] result = requests.get(thumbnail_api.format(remote_url, image_id), headers=headers).json() image_name = os.path.basename(result['original']) self.stdout.write(' Fetching image {0} - {1}'.format(image_id, image_name)) try: image = ExerciseImage.objects.get(pk=image_id) self.stdout.write(' --> Image already present locally, skipping...') continue except ExerciseImage.DoesNotExist: self.stdout.write(' --> Image not found in local DB, creating now...') image = ExerciseImage() image.pk = image_id # Save the downloaded image, see link for details # http://stackoverflow.com/questions/1308386/programmatically-saving-image-to- retrieved_image = requests.get(result['original'], headers=headers) img_temp = NamedTemporaryFile(delete=True) img_temp.write(retrieved_image.content) img_temp.flush() image.exercise = exercise image.is_main = image_json['is_main'] image.status = image_json['status'] image.image.save( os.path.basename(image_name), File(img_temp), ) image.save() else: self.stdout.write(' No images for this exercise, nothing to do')
def handle(self, *args, **options): if not settings.MEDIA_ROOT: raise ImproperlyConfigured('Please set MEDIA_ROOT in your settings file') remote_url = options['remote_url'] val = URLValidator() try: val(remote_url) except ValidationError: raise CommandError('Please enter a valid URL') # Get all exercises result = json.load(urllib.urlopen("{0}/api/v1/exercise/?limit=0".format(remote_url))) for exercise_json in result['objects']: exercise_name = exercise_json['name'] exercise_id = exercise_json['id'] self.stdout.write(u"*** Processing {0} (ID: {1})".format(exercise_name, exercise_id)) try: exercise = Exercise.objects.get(pk=exercise_id) except Exercise.DoesNotExist: self.stdout.write('Remote exercise not found in local DB, skipping...') self.stdout.write('') continue if exercise_json['images']: # Get all images for image_resource in exercise_json['images']: result = json.load(urllib.urlopen("{0}{1}".format(remote_url, image_resource))) image_name = os.path.basename(result['image']) self.stdout.write('Fetching image {0} - {1}'.format(result['id'], image_name)) image_id = result['id'] try: image = ExerciseImage.objects.get(pk=image_id) self.stdout.write('--> Image already present locally, skipping...') continue except ExerciseImage.DoesNotExist: self.stdout.write('--> Image not found in local DB, creating now...') image = ExerciseImage() image.pk = image_id # Save the downloaded image, see link for details # http://stackoverflow.com/questions/1308386/programmatically-saving-image-to- retrieved_image = urllib.urlretrieve(result['image']) image.exercise = exercise image.is_main = result['is_main'] image.image.save( os.path.basename(image_name), File(open(retrieved_image[0]), 'rb') ) image.save() else: self.stdout.write('No images for this exercise, nothing to do') self.stdout.write('')