#!/usr/bin/env python3

import html
import json
import re
import urllib.request

def url_title(url):
    try:
        with urllib.request.urlopen(url) as h:
            page = h.read()
    except:
        return None, None
    title = re_title.search(page.decode())
    if title:
        title = html.unescape(title.group(1))
    return url, title

# regex searches for parsing (in global space so they are only compiled once)
re_mentions = re.compile('(?<=@)\w+')
re_emoticons = re.compile('\((\w+)\)')
re_urls = re.compile('(https?://[^\s]+)')
re_title = re.compile('<title>(.*?)</title>', re.IGNORECASE|re.DOTALL)

def parse(msg):
    contents = {}
    # look for @user mentions
    mentions = re_mentions.findall(msg)
    if mentions:
        contents['mentions'] = mentions

    # look for (emoticon)
    emoticons = re_emoticons.findall(msg)
    if emoticons:
        contents['emoticons'] = emoticons

    # look for http://link.com and read page for title
    urls = re_urls.findall(msg)
    links = []
    for url in urls:
        url, title = url_title(url)
        print(url, title)
        if url:
            links.append({'url': url, 'title': title})
    if links:
        contents['links'] = links

    return contents

if __name__ == '__main__':
    msgs = [
        '@chris you around?',
        'Good morning! (megusta) (coffee)',
        'Olympics are starting soon; http://www.nbcolympics.com',
        '@bob @john (success) such a cool feature; https://twitter.com/jdorfman/status/430511497475670016',
    ]

    for msg in msgs:
        print('Input:', msg)
        print('Return (string):\n', json.dumps(parse(msg), indent=2, separators=(',', ': ')))

#!/usr/bin/env python3

import html
import json
import re
import urllib.request

def url_title(url):
    try:
        with urllib.request.urlopen(url) as h:
            page = h.read()
    except:
        return None, None
    title = re_title.search(page.decode())
    if title:
        title = html.unescape(title.group(1))
    return url, title

# regex searches for parsing (in global space so they are only compiled once)
re_mentions = re.compile('(?<=@)\w+')
re_emoticons = re.compile('\((\w+)\)')
re_urls = re.compile('(https?://[^\s]+)')
re_title = re.compile('<title>(.*?)</title>', re.IGNORECASE|re.DOTALL)

def parse(msg):
    contents = {}
    # look for @user mentions
    mentions = re_mentions.findall(msg)
    if mentions:
        contents['mentions'] = mentions

    # look for (emoticon)
    emoticons = re_emoticons.findall(msg)
    if emoticons:
        contents['emoticons'] = emoticons

    # look for http://link.com and read page for title
    urls = re_urls.findall(msg)
    links = []
    for url in urls:
        url, title = url_title(url)
        if url:
            links.append({'url': url, 'title': title})
    if links:
        contents['links'] = links

    return contents

if __name__ == '__main__':
    msgs = [
        '@chris you around?',
        'Good morning! (megusta) (coffee)',
        'Olympics are starting soon; http://www.nbcolympics.com',
        '@bob @john (success) such a cool feature; https://twitter.com/jdorfman/status/430511497475670016',
    ]

    for msg in msgs:
        print('Input:', msg)
        print('Return (string):\n', json.dumps(parse(msg), indent=2, separators=(',', ': ')))

remove debug print