Ejemplo n.º 1
0
def remove_images(app):
  from datetime import datetime

  while True:
    time.sleep(1800)
    conf = app.config['IMAGE_DELETE']
    with app.app_context():
      if ( datetime.utcnow().hour in conf['TIME_OF_DAY'] and
           datetime.utcnow().weekday() in conf['WEEKDAY'] ):
        images = Img.get_all_imgs()
        db_imgs = [img.location + img.filename for img in images]

        posts = Post.get_all()
        post_imgs = get_all_imgs((post.body_html for post in posts))

        diff_imgs = set(db_imgs) - set(post_imgs)

        if diff_imgs:
          app.logger.debug('Images to delete: {}'.format(db_imgs))

        for i in images:
          if i.location + i.filename in diff_imgs:
            if os.path.isfile(imgs.path(i.filename)):
              os.remove(imgs.path(i.filename))
              f, e = os.path.splitext(i.filename)

              if os.path.isfile(imgs.path(f + '_crop' + e)):
                os.remove(imgs.path(f + '_crop' + e))
            db.session.delete(i)
            db.session.commit()
Ejemplo n.º 2
0
def remove_images(app):
    from datetime import datetime

    while True:
        time.sleep(1800)
        conf = app.config['IMAGE_DELETE']
        with app.app_context():
            if (datetime.utcnow().hour in conf['TIME_OF_DAY']
                    and datetime.utcnow().weekday() in conf['WEEKDAY']):
                images = Img.get_all_imgs()
                db_imgs = [img.location + img.filename for img in images]

                posts = Post.get_all()
                post_imgs = get_all_imgs((post.body_html for post in posts))

                diff_imgs = set(db_imgs) - set(post_imgs)

                if diff_imgs:
                    app.logger.debug('Images to delete: {}'.format(db_imgs))

                for i in images:
                    if i.location + i.filename in diff_imgs:
                        if os.path.isfile(imgs.path(i.filename)):
                            os.remove(imgs.path(i.filename))
                            f, e = os.path.splitext(i.filename)

                            if os.path.isfile(imgs.path(f + '_crop' + e)):
                                os.remove(imgs.path(f + '_crop' + e))
                        db.session.delete(i)
                        db.session.commit()