Advertisement
Guest User

jake elliott

a guest
Nov 9th, 2009
799
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.37 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import urllib, os, random, ftplib
  4. from BeautifulSoup import BeautifulStoneSoup
  5. import Image
  6.  
  7. base_path = os.path.dirname(__file__)
  8.  
  9. download_dir = os.path.join(base_path,'source_images')
  10. glitch_dir = os.path.join(base_path,'glitch_images')
  11.  
  12. min_offset = 2500
  13.  
  14. flat_file_db_path = os.path.join(base_path,'kittens_db.txt')
  15.  
  16. #------------------------------------------------------------------------
  17. # utils
  18. #------------------------------------------------------------------------
  19.  
  20. def get_random_points_in_file(file_content):
  21.     start_point = random.randint(min_offset,len(file_content))
  22.     end_point = start_point + random.randint(
  23.         0, len(file_content) - start_point)
  24.  
  25.     return start_point, end_point
  26.  
  27. def post_to_tumblr(file_path):
  28.     title_chars = '______________________/|\\|/...*'
  29.     caption = ''.join([random.choice(title_chars) for x in range(random.randint(10,20))])
  30.    
  31.     filename = file_path.split(os.path.sep)[-1]
  32.    
  33.     s = ftplib.FTP(MY_FTP_SERVER,MY_FTP_USERNAME,MY_FTP_PASSWORD)
  34.     img_binary_data = open(file_path, 'rb')
  35.  
  36.     s.storbinary('STOR MY_FTP_SERVER/ktngltchs/%s' % filename, img_binary_data)
  37.  
  38.     src = "http://MY_FTP_SERVER/ktngltchs/%s" % filename
  39.  
  40.     print src
  41.    
  42.     post_data = urllib.urlencode(
  43.         {'email': MY_EMAIL_ADDRESS,
  44.          'type': 'photo',
  45.          'password': MY_PASSWORD,
  46.          'caption': caption,
  47.          'generator': 'kitten_glitches',
  48.          'source': src,
  49.          'group': MY_TUMBLR_BLOG
  50.          })
  51.    
  52.  
  53.     response = urllib.urlopen('http://www.tumblr.com/api/write', post_data)
  54.  
  55.     print response, response.read()
  56.    
  57.  
  58. #------------------------------------------------------------------------
  59. # glitch methods
  60. #------------------------------------------------------------------------
  61.  
  62. def repeat_section(file_content):
  63.     start_point, end_point = get_random_points_in_file(file_content)
  64.  
  65.     section = file_content[start_point:end_point]
  66.  
  67.     repeated = ''
  68.  
  69.     for x in range(1,random.randint(1,5)):
  70.         repeated += section
  71.  
  72.     return file_content[:start_point] + repeated + file_content[end_point:]
  73.  
  74. def splice_section_elsewhere(file_content):
  75.     start_point, end_point = get_random_points_in_file(file_content)
  76.  
  77.     section = file_content[start_point:end_point]
  78.    
  79.     repeated = ''
  80.  
  81.     for x in range(1,random.randint(1,5)):
  82.         repeated += section
  83.  
  84.     new_start_point, new_end_point = get_random_points_in_file(file_content)
  85.  
  86.     return file_content[:new_start_point] + repeated + file_content[new_end_point:]    
  87.  
  88.  
  89. glitch_techniques = (repeat_section, splice_section_elsewhere)
  90.  
  91. #------------------------------------------------------------------------
  92. # main
  93. #------------------------------------------------------------------------
  94.  
  95. def make_kitten_glitch(url):
  96.     filename = url.split('/')[-1]
  97.     urllib.urlretrieve(url, os.path.join(download_dir, filename))
  98.  
  99.     f = open(os.path.join(download_dir, filename), 'r')
  100.     file_content = f.read()
  101.     f.close()
  102.  
  103.     for run_time in range(0,random.randint(2,20)):
  104.         file_content = random.choice(glitch_techniques)(file_content)
  105.  
  106.     f = open(os.path.join(glitch_dir, filename), 'w')
  107.     f.write(file_content)
  108.     f.close()
  109.  
  110.     im = Image.open(os.path.join(glitch_dir, filename))
  111.     im.save(os.path.join(glitch_dir, filename.replace('.jpg','.png')))
  112.    
  113.     return os.path.join(glitch_dir, filename.replace('.jpg','.png'))
  114.  
  115. def find_kitten():
  116.     f = open(flat_file_db_path, 'r')
  117.     old_kittens = [x.strip() for x in f.readlines()]
  118.     f.close()
  119.    
  120.     response = urllib.urlopen('http://api.flickr.com/services/feeds/photos_public.gne?tags=kittens&lang=en-us&format=rss_200').read()
  121.  
  122.     soup = BeautifulStoneSoup(response)
  123.  
  124.     img_list = []
  125.    
  126.     for x in soup.findAll('media:thumbnail'):
  127.         img_url = dict(x.attrs)['url'].replace('_s.jpg','.jpg')
  128.         img_list.append(img_url)
  129.  
  130.     random.shuffle(img_list)
  131.  
  132.     for x in img_list:
  133.         if not x in old_kittens:
  134.             f = open(flat_file_db_path, 'a')            
  135.             f.write(x + "\n")
  136.             f.close()
  137.             return x
  138.  
  139.     f.close()
  140.    
  141. if __name__ == '__main__':
  142.     kitten_url = find_kitten()
  143.     if kitten_url:
  144.         kitten_path = make_kitten_glitch(kitten_url)
  145.        
  146.         post_to_tumblr(kitten_path)
  147.     else:
  148.         print "no kittens"
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement