Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions modules/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

r_username = re.compile(r'^[a-zA-Z0-9_]{1,15}$')
r_link = re.compile(r'^https?://twitter.com/\S+$')
r_p = re.compile(r'(?ims)(<p class="js-tweet-text.*?</p>)')
r_p1 = re.compile(r'(?ims)(<p class="ProfileTweet-text js-tweet-text.*?</p>)')
r_p2 = re.compile(r'(?ims)(<p class="js-tweet-text.*?</p>)')
r_tag = re.compile(r'(?ims)<[^>]+>')
r_anchor = re.compile(r'(?ims)(<a.*?</a>)')
r_expanded = re.compile(r'(?ims)data-expanded-url=["\'](.*?)["\']')
Expand All @@ -33,13 +34,29 @@ def replacement(match):
return r_tag.sub('', anchor)
return r_anchor.sub(replacement, tweet)

def read_tweet(url):
def read_tweet1(url):
bytes = web.get(url)
shim = '<div class="content clearfix">'
shim = '<div class="ProfileTweet-contents">'
if shim in bytes:
bytes = bytes.split(shim, 1).pop()

for text in r_p.findall(bytes):
for text in r_p1.findall(bytes):
text = expand(text)
text = r_tag.sub('', text)
text = text.strip()
text = r_whiteline.sub(' ', text)
text = r_breaks.sub(' ', text)
return decode(text)
return "Sorry, couldn't get a tweet from %s" % url

def read_tweet2(url):
bytes = web.get(url)
abc = '/'.join(url.split('/')[3:])
shim = '<a href="/%s"' % abc
if shim in bytes:
bytes = bytes.split(shim, 1).pop()

for text in r_p2.findall(bytes):
text = expand(text)
text = r_tag.sub('', text)
text = text.strip()
Expand All @@ -52,7 +69,7 @@ def format(tweet, username):
return '%s (@%s)' % (tweet, username)

def user_tweet(username):
tweet = read_tweet('https://twitter.com/' + username + "?" + str(time.time()))
tweet = read_tweet1('https://twitter.com/' + username + "?" + str(time.time()))
return format(tweet, username)

def id_tweet(tid):
Expand All @@ -63,7 +80,7 @@ def id_tweet(tid):
url = message.get("Location")
if not url: return "Sorry, couldn't get a tweet from %s" % link
username = url.split('/')[3]
tweet = read_tweet(url)
tweet = read_tweet2(url)
return format(tweet, username)
return "Sorry, couldn't get a tweet from %s" % link

Expand All @@ -81,9 +98,8 @@ def twitter(phenny, input):
elif r_username.match(arg):
phenny.say(user_tweet(arg))
elif r_link.match(arg):
username = arg.split('/')[3]
tweet = read_tweet(arg)
phenny.say(format(tweet, username))
tid = arg.split('/')[5]
phenny.say(id_tweet(tid))
else: phenny.reply("Give me a link, a username, or a tweet id")

twitter.commands = ['tw', 'twitter']
Expand Down