Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import urllib, os, random, ftplib
- from BeautifulSoup import BeautifulStoneSoup
- import Image
- base_path = os.path.dirname(__file__)
- download_dir = os.path.join(base_path,'source_images')
- glitch_dir = os.path.join(base_path,'glitch_images')
- min_offset = 2500
- flat_file_db_path = os.path.join(base_path,'kittens_db.txt')
- #------------------------------------------------------------------------
- # utils
- #------------------------------------------------------------------------
- def get_random_points_in_file(file_content):
- start_point = random.randint(min_offset,len(file_content))
- end_point = start_point + random.randint(
- 0, len(file_content) - start_point)
- return start_point, end_point
- def post_to_tumblr(file_path):
- title_chars = '______________________/|\\|/...*'
- caption = ''.join([random.choice(title_chars) for x in range(random.randint(10,20))])
- filename = file_path.split(os.path.sep)[-1]
- s = ftplib.FTP(MY_FTP_SERVER,MY_FTP_USERNAME,MY_FTP_PASSWORD)
- img_binary_data = open(file_path, 'rb')
- s.storbinary('STOR MY_FTP_SERVER/ktngltchs/%s' % filename, img_binary_data)
- src = "http://MY_FTP_SERVER/ktngltchs/%s" % filename
- print src
- post_data = urllib.urlencode(
- {'email': MY_EMAIL_ADDRESS,
- 'type': 'photo',
- 'password': MY_PASSWORD,
- 'caption': caption,
- 'generator': 'kitten_glitches',
- 'source': src,
- 'group': MY_TUMBLR_BLOG
- })
- response = urllib.urlopen('http://www.tumblr.com/api/write', post_data)
- print response, response.read()
- #------------------------------------------------------------------------
- # glitch methods
- #------------------------------------------------------------------------
- def repeat_section(file_content):
- start_point, end_point = get_random_points_in_file(file_content)
- section = file_content[start_point:end_point]
- repeated = ''
- for x in range(1,random.randint(1,5)):
- repeated += section
- return file_content[:start_point] + repeated + file_content[end_point:]
- def splice_section_elsewhere(file_content):
- start_point, end_point = get_random_points_in_file(file_content)
- section = file_content[start_point:end_point]
- repeated = ''
- for x in range(1,random.randint(1,5)):
- repeated += section
- new_start_point, new_end_point = get_random_points_in_file(file_content)
- return file_content[:new_start_point] + repeated + file_content[new_end_point:]
- glitch_techniques = (repeat_section, splice_section_elsewhere)
- #------------------------------------------------------------------------
- # main
- #------------------------------------------------------------------------
- def make_kitten_glitch(url):
- filename = url.split('/')[-1]
- urllib.urlretrieve(url, os.path.join(download_dir, filename))
- f = open(os.path.join(download_dir, filename), 'r')
- file_content = f.read()
- f.close()
- for run_time in range(0,random.randint(2,20)):
- file_content = random.choice(glitch_techniques)(file_content)
- f = open(os.path.join(glitch_dir, filename), 'w')
- f.write(file_content)
- f.close()
- im = Image.open(os.path.join(glitch_dir, filename))
- im.save(os.path.join(glitch_dir, filename.replace('.jpg','.png')))
- return os.path.join(glitch_dir, filename.replace('.jpg','.png'))
- def find_kitten():
- f = open(flat_file_db_path, 'r')
- old_kittens = [x.strip() for x in f.readlines()]
- f.close()
- response = urllib.urlopen('http://api.flickr.com/services/feeds/photos_public.gne?tags=kittens&lang=en-us&format=rss_200').read()
- soup = BeautifulStoneSoup(response)
- img_list = []
- for x in soup.findAll('media:thumbnail'):
- img_url = dict(x.attrs)['url'].replace('_s.jpg','.jpg')
- img_list.append(img_url)
- random.shuffle(img_list)
- for x in img_list:
- if not x in old_kittens:
- f = open(flat_file_db_path, 'a')
- f.write(x + "\n")
- f.close()
- return x
- f.close()
- if __name__ == '__main__':
- kitten_url = find_kitten()
- if kitten_url:
- kitten_path = make_kitten_glitch(kitten_url)
- post_to_tumblr(kitten_path)
- else:
- print "no kittens"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement