Skip to content

Commit 65410f0

Browse files
committed
Merge pull request Show-Me-the-Code#153 from vvzwvv/master
Problem 0010 solved
2 parents acf280a + 40c5e11 commit 65410f0

7 files changed

Lines changed: 136 additions & 0 deletions

File tree

vvzwvv/0004/0004.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python
2+
#-*- coding: utf-8 -*-
3+
4+
import urllib
5+
import re
6+
7+
def getHtml(url):
8+
page = urllib.urlopen(url)
9+
html = page.read()
10+
return html
11+
12+
def getImg(html):
13+
# src="http://imgsrc.baidu.com/forum/w%3D580%3Bcp%3Dtieba%2C10%2C302%3Bap%3D%C9%BC%B1%BE%D3%D0%C3%C0%B0%C9%2C90%2C310/sign=8800a2e3b3119313c743ffb855036fa7/1e29460fd9f9d72abb1a7c3cd52a2834349bbb7e.jpg" bdwater=
14+
reg = r'src="(.+?\.jpg)" bdwater='
15+
img_re = re.compile(reg)
16+
img_list = re.findall(img_re, html)
17+
return img_list
18+
19+
def saveImg(img_list):
20+
x = 0
21+
for img_url in img_list:
22+
urllib.urlretrieve(img_url, '%s.jpg' % x)
23+
x += 1
24+
25+
26+
if __name__ == "__main__":
27+
html = getHtml("http://tieba.baidu.com/p/2166231880")
28+
saveImg(getImg(html))

vvzwvv/0010/0010.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env python
2+
#-*- coding: utf-8 -*-
3+
4+
import string
5+
import random
6+
from PIL import Image, ImageDraw, ImageFont, ImageFilter
7+
8+
def create_image(image_size = (300, 100),
9+
background_color = (255, 255, 255),
10+
font_type = "arialbd.ttf",
11+
font_size = 50,
12+
text_num = 4,
13+
point_chance = 50):
14+
15+
im = Image.new("RGB", image_size, background_color)
16+
draw = ImageDraw.Draw(im)
17+
image_width, image_height = image_size
18+
19+
def create_text():
20+
text_font = ImageFont.truetype(font_type, font_size)
21+
font_width, font_height = text_font.getsize("A")
22+
for i in range(text_num):
23+
text = random.choice(string.ascii_uppercase)
24+
text_loc = ((image_width - font_width) / text_num * (i + 0.5), (image_height - font_height) / 2.3)
25+
draw.text(text_loc, text, font = text_font, fill = (random.randint(0, 255) / 2, random.randint(0, 255) / 2, random.randint(0, 255) / 2))
26+
27+
def create_points():
28+
for w in range(image_width):
29+
for h in range(image_height):
30+
tmp = random.randint(0, 100)
31+
if tmp > point_chance:
32+
draw.point((w, h), fill = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
33+
34+
create_text()
35+
create_points()
36+
im = im.filter(ImageFilter.BLUR)
37+
38+
return im
39+
40+
if __name__ == "__main__":
41+
im = create_image()
42+
im.show()

vvzwvv/0010/arialbd.ttf

845 KB
Binary file not shown.

vvzwvv/0011/0011.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from cmd import Cmd
2+
3+
class CmdCheck(Cmd):
4+
5+
def __init__(self):
6+
Cmd.__init__(self)
7+
self.intro = "-------- Word Sensor (Input \"exit\" for termination) --------"
8+
self.prompt = "> "
9+
self.sensitive = map(lambda word: word.strip("\n"), open("filtered_words.txt").readlines())
10+
11+
def default(self, line):
12+
if any([word in line for word in self.sensitive]):
13+
print "Freedom"
14+
else:
15+
print "Human Rights"
16+
17+
def do_exit(self, line):
18+
exit()
19+
20+
if __name__ == "__main__":
21+
cmd = CmdCheck()
22+
cmd.cmdloop()

vvzwvv/0011/filtered_words.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
北京
2+
程序员
3+
公务员
4+
领导
5+
牛比
6+
牛逼
7+
你娘
8+
你妈
9+
love
10+
sex
11+
jiangge

vvzwvv/0012/0012.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from cmd import Cmd
2+
3+
class CmdCheck(Cmd):
4+
5+
def __init__(self):
6+
Cmd.__init__(self)
7+
self.intro = "-------- Word Sensor (Input \"exit\" for termination) --------"
8+
self.prompt = "> "
9+
self.sensitive = map(lambda word: word.strip("\n"), open("filtered_words.txt").readlines())
10+
11+
def default(self, line):
12+
for word in self.sensitive:
13+
if word in line:
14+
line = line.replace(word, ''.join("*" for i in range(len(word))))
15+
print line
16+
17+
def do_exit(self, line):
18+
exit()
19+
20+
if __name__ == "__main__":
21+
cmd = CmdCheck()
22+
cmd.cmdloop()

vvzwvv/0012/filtered_words.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
北京
2+
程序员
3+
公务员
4+
领导
5+
牛比
6+
牛逼
7+
你娘
8+
你妈
9+
love
10+
sex
11+
jiangge

0 commit comments

Comments
 (0)