Exemplo n.º 1
0
    def addMedia(self,item):
        title = item.find("title").text # Ostrich reads newspaper
        link = item.find("link").text # http://mart-e.be/post/2013/01/18/media-id-la-presse-belge-evolue/3236806056_a0d1236ef3/
        media_id = item.find("{http://wordpress.org/export/1.2/}post_id").text # 2
        content = item.find("{http://purl.org/rss/1.0/modules/content/}encoded").text # the whole article
        slug = item.find("{http://wordpress.org/export/1.2/}post_name").text # nouveau-blog (CAN BE EMPTY)
        #allow_comments = item.find("{http://wordpress.org/export/1.2/}comment_status").text # open
        publish = item.find("{http://wordpress.org/export/1.2/}post_date").text # 2010-01-05 15:09:00
        
        attachment_url = item.find("{http://wordpress.org/export/1.2/}attachment_url").text
        if not attachment_url[-4:].lower() in [".jpg",".png",".gif"]:
            # TODO support other types            
            # raise Exception("Unknown file format {0}".format(attachment_url[-4:]))
            return False
        
        if slug:
            medias = Photo.objects.filter(slug=slug)
        else:
            medias = Page.objects.filter(id=int(media_id))
        if len(medias) != 0:
            print("Skipping {0}".format(medias[0].slug))
            return medias[0]

        try:
            print("Creating media '"+title+"'")
        except:
            print("Creating media #"+media_id)
            
        media = Photo()
        media.title = title
        media.id = media_id
        if slug:
            media.slug = slug[:50]
        else:
            media.slug = slugify(title)[:50]
        
        # move file
        # TODO use date from wordpress, not from today
        path = os.path.join(self.wp_content, attachment_url.split("wp-content/")[1])
        f = open(path, 'r')
        media.photo = File(f)

        publish = publish = datetime.strptime(publish,"%Y-%m-%d %H:%M:%S").replace(tzinfo=utc)
        media.uploaded = publish
        media.modified = publish
        media.save()        
        #media.save()
        f.close()


        return media
Exemplo n.º 2
0
def download_foto_pers(persona,link_foto):
   
    ok=True
    now=datetime.datetime.now()
    new_file_dir = os.path.dirname(settings.MEDIA_ROOT+now.strftime("%Y"+os.sep+"%m"+os.sep))
    if not os.path.exists(new_file_dir):
        os.makedirs(new_file_dir)
    
    try:
        link=link_foto
        pos=link.rfind('/')
        nome_foto=link[pos+1:]
        try:
            nome_foto=nome_foto.replace('%20','_')
            name = now.strftime("%Y"+os.sep+"%m"+os.sep) + "pers" + str(persona.id) + nome_foto
            #print name
            try:
                image = urllib.URLopener()
                image.retrieve(link,settings.MEDIA_ROOT + name)
            except:
                logger_download.warning('impossibile fare download')
                logger_download.warning(link)
                image=None
            if image!=None:
                try:
                    img = Photo.objects.get(image=name)
                except:    
                    img = Photo()
                    img.name = nome_foto
                    img.image = name
                    img.caption=persona.nome + ' ' + persona.cognome 
                    img.upload_date = datetime.datetime.now()
                    img.publish_date = datetime.datetime.now()                    
                    img.save()
            
                    persona.foto.add(img)
                    persona.save()
        except:
            
            logger_download.warning('problemi con download foto')
            logger_download.warning(sys.exc_info())
            
            
    except:
        logger_download.warning(sys.exc_info())
        ok=False
Exemplo n.º 3
0
def download_foto_episodio(episodio, link_foto):
    count = 0
    ok = True
    now = datetime.datetime.now()
    new_file_dir = os.path.dirname(settings.MEDIA_ROOT + now.strftime("%Y" + os.sep + "%m" + os.sep))
    if not os.path.exists(new_file_dir):
        os.makedirs(new_file_dir)

    try:
        link = link_foto
        pos = link.rfind("/")
        nome_foto = link[pos + 1 :]
        if nome_foto.find("no_image") == -1:
            try:
                nome_foto = nome_foto.replace("%20", "_")
                name = now.strftime("%Y" + os.sep + "%m" + os.sep) + "ep_" + str(episodio.id) + nome_foto
                image = urllib.URLopener()
                image.retrieve(link, settings.MEDIA_ROOT + name)

                try:
                    img = Photo.objects.get(image=name)
                except:
                    img = Photo()
                    img.name = nome_foto
                    img.image = name
                    img.caption = episodio.titolo
                    img.upload_date = datetime.datetime.now()
                    img.publish_date = datetime.datetime.now()

                    img.save()

                    episodio.foto.add(img)
                    episodio.save()
            except:
                print sys.exc_info()[0]
                print nome_foto
                print "problemi con download foto"

    except:
        ok = False
Exemplo n.º 4
0
    def handle(self, *args, **options):
        success = 0
        errors = []
        user_photos = {}
        base_dir = create_dir(settings.MEDIA_ROOT, 'attachments')
        imapSession = imaplib.IMAP4_SSL('imap.gmail.com')
        typ, accountDetails = imapSession.login(settings.PICS_EMAIL,
                                                settings.PICS_PASSWORD)
        date_string = datetime.date.today().strftime('%Y-%m-%d')
        if typ != 'OK':
            mail_admins("unable to authenticate email photos", ":(")
            raise

        imapSession.select('[Gmail]/All Mail')
        typ, data = imapSession.search(None, 'ALL')
        if typ != 'OK':
            mail_admins('Error searching Inbox.',
                        'download_photos failed at searching mailbox')
            raise

        # Iterating over all emails
        for msgId in data[0].split():
            typ, messageParts = imapSession.fetch(msgId, '(RFC822)')
            if typ != 'OK':
                errors.append('Error fetching mail.')
                continue

            mail = email.message_from_string(messageParts[0][1])
            user = None
            try:
                address = re.findall(r'[^><\ @]+@[^><\ @]+', mail['From'])[0]
            except IndexError:
                errors.append("error parsing to_address of:\n\n%s\n\n" %
                              messageParts[0][1])
                continue
            if address:
                user = user_from_email(address)
            for part in mail.walk():
                if part.get_content_maintype() == 'multipart':
                    # print part.as_string()
                    continue
                if part.get('Content-Disposition') is None:
                    # print part.as_string()
                    continue
                fname = part.get_filename()

                if not bool(fname):
                    continue
                fname = "%s-%s" % (int(
                    time.mktime(
                        datetime.datetime.now().utctimetuple())), fname)
                filePath = os.path.join(settings.MEDIA_ROOT, 'attachments',
                                        fname)
                fp = open(filePath, 'wb')
                fp.write(part.get_payload(decode=True))
                fp.close()
                photo = Photo(
                    filename=fname,
                    user=user,
                    source='email',
                    caption=mail['Subject'],
                )
                photo.save()
                success += 1
                if not address in user_photos:
                    user_photos[address] = 0

        imapSession.close()
        imapSession.logout()

        if success:
            mail_admins("%s email photos uploaded successfully" % success, "")
        if errors:
            mail_admins(
                "%s errors occurred in getting photos" % len(errors),
                "The following errors occurred" +
                "\n\n---------\n".join(errors))

        msg = '%s photos have been uploaded to %s from this email address.'\
            '\n\nTo modify the name and caption on these photos, visit the following url.\n\n%s'
        for addr, count in user_photos.items():
            if not user_from_email(user):
                pass  #!
            print addr
            if True:
                raise NotImplimentedError('need to create modify photos view')
            send_mail(
                'New Photos at %s' % settings.SITE_NAME,
                msg % (count, settings.SITE_NAME, reverse('modify_photos')),
                '*****@*****.**', [addr])
  def handle(self, *args, **options):
    success = 0
    errors = []
    user_photos = {}
    base_dir = create_dir(settings.MEDIA_ROOT,'attachments')     
    imapSession = imaplib.IMAP4_SSL('imap.gmail.com')
    typ, accountDetails = imapSession.login(settings.PICS_EMAIL,settings.PICS_PASSWORD)
    date_string = datetime.date.today().strftime('%Y-%m-%d')
    if typ != 'OK':
      mail_admins("unable to authenticate email photos",":(")
      raise

    imapSession.select('[Gmail]/All Mail')
    typ, data = imapSession.search(None, 'ALL')
    if typ != 'OK':
      mail_admins('Error searching Inbox.','download_photos failed at searching mailbox')
      raise

    # Iterating over all emails
    for msgId in data[0].split():
      typ, messageParts = imapSession.fetch(msgId, '(RFC822)')
      if typ != 'OK':
        errors.append('Error fetching mail.')
        continue

      mail = email.message_from_string(messageParts[0][1])
      user = None
      try:
        address = re.findall(r'[^><\ @]+@[^><\ @]+',mail['From'])[0]
      except IndexError:
        errors.append("error parsing to_address of:\n\n%s\n\n"%messageParts[0][1])
        continue
      if address:
        user = user_from_email(address)
      for part in mail.walk():
        if part.get_content_maintype() == 'multipart':
          # print part.as_string()
          continue
        if part.get('Content-Disposition') is None:
          # print part.as_string()
          continue
        fname = part.get_filename()
   
        if not bool(fname):
          continue
        fname = "%s-%s"%(int(time.mktime(datetime.datetime.now().utctimetuple())),fname)
        filePath = os.path.join(settings.MEDIA_ROOT, 'attachments', fname)
        fp = open(filePath, 'wb')
        fp.write(part.get_payload(decode=True))
        fp.close()
        photo = Photo(
          filename = fname,
          user = user,
          source = 'email',
          caption = mail['Subject'],
          )
        photo.save()
        success += 1
        if not address in user_photos:
          user_photos[address] = 0

    imapSession.close()
    imapSession.logout()

    if success:
      mail_admins("%s email photos uploaded successfully"%success,"")
    if errors:
      mail_admins("%s errors occurred in getting photos"%len(errors),
                  "The following errors occurred"+"\n\n---------\n".join(errors))

    msg = '%s photos have been uploaded to %s from this email address.'\
        '\n\nTo modify the name and caption on these photos, visit the following url.\n\n%s'
    for addr,count in user_photos.items():
      if not user_from_email(user):
        pass #!
      print addr
      if True:
        raise NotImplimentedError('need to create modify photos view')
      send_mail(
        'New Photos at %s'%settings.SITE_NAME,
        msg%(count,settings.SITE_NAME,reverse('modify_photos')),
        '*****@*****.**',
        [addr]
        )
Exemplo n.º 6
0
def download_foto(film,link_foto,tipo='classic'):
    #print 'download foto'
    count=0
    ok=True
    new_file_dir = os.path.dirname(settings.MEDIA_ROOT+film.data_ins.strftime("%Y"+os.sep+"%m"+os.sep))
    if not os.path.exists(new_file_dir):
        os.makedirs(new_file_dir)
    while ok==True:
        try:
            link=link_foto[count]
            pos=link.rfind('/')
            nome_foto=link[pos+1:]
            try:
                
                nome_foto=nome_foto.replace('%20','_')
                name = film.data_ins.strftime("%Y"+os.sep+"%m"+os.sep) + str(film.id) + nome_foto
                try:
                    image = urllib.URLopener()
                    image.retrieve(link,settings.MEDIA_ROOT + name)
                except:
                    image=None
                    logger_download.warning('foto non trovata')
                    logger_download.warning(sys.exc_info())
                    logger_download.info(link)
                #print link
                if image!=None:
                    try:
                        img = Photo.objects.get(image=name)
                    except:    
                        img = Photo()
                        img.name = nome_foto
                        img.image=name
                        img.upload_date = datetime.datetime.now()
                        img.publish_date = datetime.datetime.now()
                        img.save()
                        
                    #film.foto.add(img)
                    #film.save()
                    if img!=None:
                        try:
                            moviefoto=MovieFoto.objects.get(foto=img,movie=film)
                        except:
                            #nonlotrovo lo aggiungo
                            moviefoto=MovieFoto()
                            moviefoto.foto=img
                            moviefoto.movie=film
                            if tipo=='classic':
                                moviefoto.ordine=100
                            elif tipo=='locandina':
                                moviefoto.ordine=10
                            moviefoto.tipo=tipo
                            moviefoto.save()
                
            except:
                logger_download.warning('Problemi con il download di una foto')
                logger_download.warning(sys.exc_info())
                
                #print 'problemi con download foto'
                pass
            count=count+1
        except:
            ok=False