# Path: endorsements/models.py
# class Account(models.Model):
#     objects = AccountManager()
# 
#     twitter_id = models.CharField(max_length=20, primary_key=True)
# 
#     screen_name = models.CharField(max_length=50)
#     name = models.CharField(max_length=100)
#     description = models.TextField(blank=True)
#     location = models.CharField(max_length=100, blank=True)
# 
#     protected = models.BooleanField()
#     verified = models.BooleanField()
# 
#     profile_image_url = models.URLField(max_length=300, null=True)
#     url = models.URLField(max_length=300, blank=True, null=True)
# 
#     followers_count = models.PositiveIntegerField()
#     friends_count = models.PositiveIntegerField()
#     statuses_count = models.PositiveIntegerField()
# 
#     last_updated = models.DateTimeField(auto_now=True)
# 
#     endorser = models.ForeignKey(Endorser)
# 
#     class Meta:
#         ordering = ['-followers_count']
# 
#     def __unicode__(self):
#         return '@{username} ({name})'.format(
#             username=self.screen_name,
#             name=self.name
#         )
# 
#     def get_large_image(self):
#         return self.profile_image_url.replace('normal', '400x400')
# 
#     def get_absolute_url(self):
#         return "https://twitter.com/{username}".format(
#             username=self.screen_name
#         )
# 
#     def get_profile_image(self):
#         return '<img src="%s" height="100" />' % self.profile_image_url
#     get_profile_image.allow_tags = True
#     get_profile_image.short_description = 'Profile image'
# 
#     def save_image_to_s3(self):
#         """TODO"""
#         import boto
#         s3_connection = boto.connect_s3()
#         bucket = s3_connection.get_bucket('endorsementdb.com')
# 
#         url = self.get_large_image()
# 
#         response = requests.get(url, stream=True)
#         with open('/tmp/profile_image.png', 'wb') as out_file:
#             shutil.copyfileobj(response.raw, out_file)
#             del response
# 
#         key = bucket.new_key('images/endorsers/%d.png' % self.endorser.pk)
#         key.set_contents_from_filename(out_file.name)
#         key.make_public()


import shutil
import boto
import requests
from django.core.management.base import BaseCommand, CommandError
from endorsements.models import Account




class Command(BaseCommand):
    help = 'Save images for all accounts'

    def add_arguments(self, parser):
        parser.add_argument('usernames', nargs='+')

    def handle(self, *args, **options):
        s3_connection = boto.connect_s3()
        bucket = s3_connection.get_bucket('endorsementdb.com')

        usernames = options['usernames']
        for username in usernames:

# Path: endorsements/models.py
# class Account(models.Model):
#     objects = AccountManager()
# 
#     twitter_id = models.CharField(max_length=20, primary_key=True)
# 
#     screen_name = models.CharField(max_length=50)
#     name = models.CharField(max_length=100)
#     description = models.TextField(blank=True)
#     location = models.CharField(max_length=100, blank=True)
# 
#     protected = models.BooleanField()
#     verified = models.BooleanField()
# 
#     profile_image_url = models.URLField(max_length=300, null=True)
#     url = models.URLField(max_length=300, blank=True, null=True)
# 
#     followers_count = models.PositiveIntegerField()
#     friends_count = models.PositiveIntegerField()
#     statuses_count = models.PositiveIntegerField()
# 
#     last_updated = models.DateTimeField(auto_now=True)
# 
#     endorser = models.ForeignKey(Endorser)
# 
#     class Meta:
#         ordering = ['-followers_count']
# 
#     def __unicode__(self):
#         return '@{username} ({name})'.format(
#             username=self.screen_name,
#             name=self.name
#         )
# 
#     def get_large_image(self):
#         return self.profile_image_url.replace('normal', '400x400')
# 
#     def get_absolute_url(self):
#         return "https://twitter.com/{username}".format(
#             username=self.screen_name
#         )
# 
#     def get_profile_image(self):
#         return '<img src="%s" height="100" />' % self.profile_image_url
#     get_profile_image.allow_tags = True
#     get_profile_image.short_description = 'Profile image'
# 
#     def save_image_to_s3(self):
#         """TODO"""
#         import boto
#         s3_connection = boto.connect_s3()
#         bucket = s3_connection.get_bucket('endorsementdb.com')
# 
#         url = self.get_large_image()
# 
#         response = requests.get(url, stream=True)
#         with open('/tmp/profile_image.png', 'wb') as out_file:
#             shutil.copyfileobj(response.raw, out_file)
#             del response
# 
#         key = bucket.new_key('images/endorsers/%d.png' % self.endorser.pk)
#         key.set_contents_from_filename(out_file.name)
#         key.make_public()


# Path: endorsements/management/commands/saveimages.py
import shutil
import boto
import requests
from django.core.management.base import BaseCommand, CommandError
from endorsements.models import Account





class Command(BaseCommand):
    help = 'Save images for all accounts'

    def add_arguments(self, parser):
        parser.add_argument('usernames', nargs='+')

    def handle(self, *args, **options):
        s3_connection = boto.connect_s3()
        bucket = s3_connection.get_bucket('endorsementdb.com')

        usernames = options['usernames']
        for username in usernames:

            account = Account.objects.get_from_username(username)