def main(argv):
    monthid = 0 

    try:
        opts, args = getopt.getopt(argv, "m:", ['month'])
    except getopt.GetoptError:
        print 'mobilecheck.py -m <monthId>'
        sys.exit(2)

    for opt, arg in opts:
        if opt in ('-m', '--month'):
            monthid = arg 

    print 'MonthId [', monthid , ']'

    if monthid != 0:
        db = SpeedyDb();
   
        print 'running mobile check'
        
        sites = db.getUncheckedMobileSites(monthid)
        
        print 'Processing ', len(sites), ' sites'

        pool = Pool(processes=8)
        pool.map(processSite, sites)
        pool.close()
        pool.join()         
class MobileCheck(object):

    def __init__(self):
        self.db = SpeedyDb();
        

    def runCheck(self, monthId):
    
        print 'running mobile check'
        
        sites = self.db.getUncheckedMobileSites(monthId)
        
        print 'Processing ', len(sites), ' sites'

       #for site in sites:
       # 
       #     siteId = site[0]
       #     siteName = site[1]
       #     siteUrl = site[2]
       #     
       #     print ''
       #     print siteId, siteName, siteUrl, 
       #     self.getMobileResult(siteUrl, siteId, monthId)

    def getMobileResult(self, url, siteId, monthId):
    
        try:
            result = self.getMobileJson(url)
                        
            if result.__len__() > 10 :
                data = json.loads(result)
               
                usability = data['ruleGroups']['USABILITY']
                print usability['pass'],

                self.db.saveMobileCheck(siteId,monthId, usability['pass']);
        except Exception, e:
            print 'get mobile pass error:', url, e