#!/usr/bin/env python2.7

from BeautifulSoup import BeautifulSoup
from urllib2 import urlopen, Request, HTTPError

from sys import exit, stdout

import logging
log = logging.getLogger(__name__)

class Instructable:
    """ Instructable container """
    pass

# URL to index page containing all instructables
INSTRUCTABLE_ENTRIES_URL = 'http://www.instructables.com/id/{g}'

# URL for individual instructable
INSTRUCTABLE_URL = 'http://www.instructables.com/{id}'

# Instructable groups to find
groups = [
    'YuKonstruct-Makerspace-Contest-Entries',
    'The-Inventors-House-Hackerspace',
    'Robot-House-Makerspace-Contest',
    'Hacedores-Makerspace',
    'Eureka-Factory-Summer-Contest-Entries',
    'Weekend-Projects-1',
    'E4D-banjarapalya-Makerspace',
    'Chase-Space',
    'Helping-The-Society',
    'Maui-Makers-Instructables',
    'Fab-Lab-Egypt',
    'BrainTrust',
    'OSMM-Maker-Space-Contest-Entries',
    'Hack42s-makerspace-contest-entries',
    'Makerspace-Contest-1'
]

def GetInstructables(group):
    """
    """
    # Parse HTML
    soup = BeautifulSoup(urlopen(INSTRUCTABLE_ENTRIES_URL.format(g=group)))
    instructables = []

    # Process each caption (instructable)
    for caption in soup.findAll('div', {'class': 'caption'}):
        # Populate Instructable
        title = caption.findAll('span', {'class': 'title'})
        stats = caption.findAll('span', {'class': 'stat'})
        # Set values
        if 2 == len(stats):
            instructable = Instructable()
            # Set attributes
            setattr(instructable, 'title', title[0].a.string)
            views = stats[0].string
            setattr(instructable, 'views', int(float(views[:-1]) * 1000) if 'K' == views[-1] else int(views))
            setattr(instructable, 'favourites', int(stats[1].string))
            # Add to list of instructables for this group
            instructables.append(instructable)

    # Success
    return instructables

""" Main entry point.
"""
def main():
    try:
        print '<html></body>'

        # Process each group
        for group in groups:
            print '<table width="100%">'
            print '<thead><tr><th>{g}</th><th>Views</th><th>Favourites</th></tr></thead><tbody>'.format(g=group)
            instructables = GetInstructables(group)
            for instructable in instructables:
                print '<tr><td>{t}</td><td>{v}</td><td>{f}</td></tr>'.format(
                    v=instructable.views,
                    f=instructable.favourites,
                    t=instructable.title
                )

            print '<tr><td></td><td>{v}</td><td>{f}</td></tr>'.format(
                v=sum([x.views for x in instructables ]),
                f=sum([x.favourites for x in instructables ]),
            )
            print '</tbody></table>'
        print '</body></html>'

        # Success
        return 0
    except KeyboardInterrupt:
        # Caught ctrl-c
        return 0
    except Exception as e:
        raise

if __name__ == '__main__':
    exit(main())
