Advertisement
overloop

threads-catalog.py

Nov 22nd, 2015
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.94 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Sat Nov 21 14:29:50 2015
  4.  
  5. @author: User
  6. """
  7.  
  8. from bs4 import BeautifulSoup
  9. import re
  10. import os
  11.  
  12. # theres probably better way
  13. def innerHtml(element):
  14.     t = []
  15.     for child in element.children:
  16.         t.append('%s' % child)
  17.     return '\n'.join(t)
  18.  
  19. home = 'C:/wamp/www/4chan'
  20.  
  21.  
  22. threads = []
  23.  
  24. for item in os.listdir(home):
  25.     threadNumber = item
  26.     m = re.match('^[0-9]+$',item)
  27.     if m == None:
  28.         continue
  29.     print threadNumber
  30.     thread = home + "/" + threadNumber + "/index.html"
  31.    
  32.     f = open(thread,'r')
  33.     html = f.read()
  34.     f.close()
  35.     soup = BeautifulSoup(html, 'html.parser')
  36.    
  37.     post = soup.find(class_="opContainer")
  38.    
  39.     postMessage = post.find(class_="postMessage")
  40.     fileThumb = post.find(class_="fileThumb")
  41.     postImage = fileThumb.find('img')
  42.    
  43.     imgStyle = postImage.get('style')
  44.    
  45.     w = re.search('width: ([0-9]+)px',imgStyle)
  46.     h = re.search('height: ([0-9]+)px',imgStyle)
  47.     w = float(w.group(1))
  48.     h = float(h.group(1))
  49.     #print float(w.group(1) + ' ' + h.group(1)
  50.    
  51.     asp = w / h
  52.    
  53.     w2 = 150
  54.     h2 = w2 / asp
  55.    
  56.     newStyle = 'width: %dpx; height: %dpx' % (w2,h2)
  57.     image = '<a href="' + threadNumber + '/index.html"><img src="' + threadNumber + '/' + postImage.get('src') + '" class="thumb" style="'+newStyle+'"/></a>'
  58.     teaser = '<div class="teaser">'+innerHtml(postMessage)+'</div>'
  59.     result = '<div class="thread">' + image + teaser + '</div>'
  60.     threads.append(result)
  61.    
  62.     #result = topHtml + result + bottomHtml
  63.    
  64. dest = home + '/index.html'
  65. topHtml = '<html><head><link href="https://s.4cdn.org/css/catalog_yotsuba_b_new.635.css" rel="stylesheet" id="base-css" type="text/css"></head><body><div id="threads" class="extended-small">'
  66. bottomHtml = '</div></body></html>'
  67.  
  68. result = topHtml + "\n".join(threads) + bottomHtml
  69.  
  70. f = open(dest,'w')
  71. f.write(result)
  72. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement