-
Notifications
You must be signed in to change notification settings - Fork 0
/
birdsay.py
executable file
·91 lines (77 loc) · 2.19 KB
/
birdsay.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python
"""
.__
____ ____ ___ __| |__ ___________ ____
/ \_/ __ \\ \/ / | \_/ __ \_ __ \/ _ \
| | \ ___/ > <| Y \ ___/| | \( <_> )
|___| /\___ >__/\_ \___| /\___ >__| \____/
\/ \/ \/ \/ \/
David Pereira - [email protected]
https://github.com/nexhero
"""
import sys
import textwrap
import requests
from bs4 import BeautifulSoup
#the bird come from http://www.chris.com/ascii/index.php?art=animals/birds%20%28land%29
def birdSay(text , tweet_type = "NORMAL"):
if tweet_type == "USER":
tweet = getUserTweet(text)
elif tweet_type == "TAG":
tweet = getTagTweet(text)
else:
tweet = getSearchTweet(text)
lines = buildText(tweet)
return buildBorde(lines) + buildBird()
def buildBird():
return """
___ /
( "> /
)(
// )
--//""--
-/------
"""
def buildText(text):
lines = textwrap.wrap(text, 40)
maxlen = len(max(lines, key=len))
return [ line.ljust(maxlen) for line in lines ]
def buildBorde(lines):
borde = []
bsize = len(lines[0])
borde.append("_" * bsize)
for line in lines:
borde.append("| %s |" % (line))
borde.append("-" * bsize)
return "\n".join(borde)
def getTweet(data):
soup = BeautifulSoup(data)
ps = soup.find("p", class_="js-tweet-text")
if ps == None:
return "There are not tweets in this account"
else:
return ps.text
def getUserTweet(user):
url = "https://twitter.com/" + user
r = requests.get(url)
data = r.text
ps = getTweet(data)
return ps
def getTagTweet(tag):
url = "https://twitter.com/search?f=realtime&q=%23" + tag
r = requests.get(url)
data = r.text
tweet = getTweet(data)
return tweet
def getSearchTweet(text):
url = "https://twitter.com/search?f=realtime&q=" + text
r = requests.get(url)
data = r.text
tweet = getTweet(data)
return tweet
#if len(sys.argv) < 2:
# print "Usage: '%s string'" % sys.argv[0]
#sys.exit(0)
#else:
# print birdSay(sys.argv[1])
#print birdSay("Linus__Torvalds")