Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import pymysql
- import os
- import time
- import shutil
- import urllib2
- from PIL import Image
- import hashlib
- from urlparse import urlparse
- from random import *
- class Process(object):
- "Object to process images"
- def __init__(self):
- self.image_folder = '/opt/www/memecdn.net/www/images'
- self.thumbnails = '/opt/www/memecdn.net/www/images/thumbnails'
- self.list = '/opt/www/memecdn.net/www/url_list.txt'
- self.db = 'memecdn'
- self.user = 'memecdn'
- self.sql_pass = 'bz7GY8Yp8bYwrMsX'
- self.sql_host = 'db.zswap.net'
- self.port = 3306
- def sql_connect(self):
- '''
- Connects to a mysql object
- '''
- conn = pymysql.connect(host=self.sql_host, port=self.port, user=self.user, passwd=self.sql_pass, db=self.db)
- return conn
- def remove_broken_images(self):
- images = next(os.walk(self.image_folder))[2]
- for image in images:
- full_path = self.image_folder + image
- if os.path.getsize(full_path) == 0:
- os.remove(full_path)
- print('Removed broken file: %s' % full_path)
- def build_image_list(self):
- url_list = []
- with open(self.list) as f:
- for line in f.readlines():
- url_list.append(line)
- return url_list
- def empty_table(table, self):
- conn = sql_connect()
- cur = conn.cursor()
- sql = "TRUNCATE TABLE %s" % (table)
- try:
- cur.execute(sql)
- except Exception as e:
- print e
- conn.commit()
- def check_size(image, self):
- try:
- im = Image.open(image)
- width, height = im.size
- resolution = [width, height]
- return resolution
- except:
- return None
- def create_thumbnail(image, out):
- size = 256, 256
- width = 0
- height = 0
- try:
- resolution = check_size(image)
- width = resolution[0]
- height = resolution[1]
- except:
- pass
- if width > 256 and height > 256:
- im = Image.open(image)
- im.thumbnail(size)
- im.save(out, "PNG")
- def get_image(url):
- url = url.strip()
- image_id = int(time.time)) + randint(1,99999)
- image_dict = {}
- o = urlparse(url)
- # get some initial information
- image_dict.update({'netloc': o[1]})
- image_dict.update({'path': o[2]})
- image_dict.update({'local_file': image_id})
- image_dict.update({'file_type': url.split(".")[-1]})
- # actually get the image
- header={'User-Agent':"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36"}
- try:
- req = urllib2.Request(url, headers={'User-Agent' : header })
- raw_img = urllib2.urlopen(req).read()
- f = open(os.path.join('images/', str(image_dict.get('local_file')) + "." + image_dict.get('file_type')), 'wb')
- f.write(raw_img)
- f.close()
- except Exception as e:
- print "failed to get %s" % (url)
- print e
- return image_dict
- def clean_thumbnails(self):
- path = self.thumbnails
- shutil.rmtree(path)
- if not os.path.exists(path):
- os.makedirs(path)
- def main():
- '''
- Entry point for class Process to process images
- '''
- process_images = Process()
- process_images.empty_table(images)
- process_images.remove_broken_images()
- for image in process_images.build_image_list():
- print process_images.get_image(image)
- if __name__ = '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement