def handle(self, *args, **options): api = API(settings.VK_APP_ID, '*****@*****.**', 'DeaDa1u5_M-M') groups = Vkontakte.objects.all() for g in groups: if g.group_id: try: response = api.wall.get(owner_id=g.group_id, count=10) if response['count']: for post in response['items']: if 'attachments' in post and post['attachments']: for attachment in post['attachments']: if attachment['type'] == 'photo': picture = None if 'photo_807' in attachment and attachment['photo_807']: picture = attachment['photo_807'] elif 'photo_604' in attachment and attachment['photo_604']: picture = attachment['photo_604'] if picture: response = requests.get(picture) image = StringIO.StringIO(response.content) p = Picture(image=ContentFile(image.read()), data_source='vkontakte') p.save() except VkAPIMethodError as e: print e.message
def handle(self, *args, **options): tags = args[0] if len(args) else None extensions = ('jpg', 'jpeg', 'png', 'gif') files = [] for e in extensions: files.extend(glob('{}/upload/*.{}'.format(settings.MEDIA_ROOT, e))) for f in files: try: c = open(f, 'r') p = Picture(image=File(c)) p.save() p.tags.add(*tags.split(',')) self.stdout.write('Imported {}'.format(f)) except IntegrityError as e: self.stdout.write('Skipped {}'.format(f)) except Exception as e: self.stdout.write('[ERROR] {} ({})'.format(e.message, type(e)))