Beispiel #1
0
 def saveCookie(self,cookieJar):
     cache = BaeMemcache()
     tempDIR = const.APP_TMPDIR
     cookieFile = tempDIR+'/cookie.txt'
     cookieJar.save(cookieFile,ignore_discard=True, ignore_expires=True)
     #pickle.dump(cookieJar, open(cookieFile,'w'))
     d = open(cookieFile,'r')
     ck = d.read()
     #ck = pickle.dumps(cookieJar,True)
     cache.delete('cookie')
     cache.set('cookie',ck)
     return ck
Beispiel #2
0
def testWeb(request):
    cache = BaeMemcache()
    craw = webCrawler()
    cookies = craw.loadCookie()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies))
    
    m = hashlib.md5('142536')
    crpt = m.hexdigest()
    info ={'username':'******','pwd':crpt,
       'imgcode':'','f':'json'}
    data = urllib.urlencode(info)
    
    resp = opener.open(BASE_WCURL+'/cgi-bin/login?lang=zh_CN',data)
    craw.saveCookie(cookies)
    
    s=resp.read()
    ret = json.loads(s)
    cache.set('token',ret['ErrMsg'])
    return HttpResponse(ret['ErrMsg'])
Beispiel #3
0
    def plainLogin(self,user,pwd):
        cache = BaeMemcache()
        m = hashlib.md5(pwd)
        crpt = m.hexdigest()
        info ={'username':user,'pwd':crpt,
       'imgcode':'','f':'json'}
        data = urllib.urlencode(info)
        
        #HTTP Fetch
        #cookies = self.loadCookie()
        cookies = cookielib.MozillaCookieJar()
        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies))
        resp = opener.open(BASE_WCURL+'/cgi-bin/login?lang=zh_CN',data)
        
        
        #parse response
        s=resp.read()
        #log(s)
        ret = json.loads(s)
        redir = ret['ErrMsg']
        token =matchStr(redir,'[\d]{8,}')
        #log('token='+token)
        cache.set('token',token)
        self.saveCookie(cookies)

        if(ret['Ret']==302):
            #login sucess
            cookies = self.loadCookie()
            
            opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies))
            resp = opener.open(BASE_WCURL+ret['ErrMsg'])
            rt = self.saveCookie(cookies)
            return 'Login Success! '
        else:
            #login failed
            #To-do: Add a verification code handling routine
            
            #end to-do
            self.saveCookie(cookies)
            
            return 'Login Failed! '
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        
        ranking = []

        m = md5.new()
        m.update(self.request.get('p'))
        key = m.digest()
        cache = BaeMemcache()
        if cache.get(key) != None:
            self.response.write(cache.get(key))
            return
        
        if cache.get('ranking') != None :
            ranking = cache.get('ranking')

        lim = dict({'SO2': 2.5, 'CO': 0.25, 'O3': 25, 'PM10': 17, 'NO2': 11})

        mydb = MySQLdb.connect( host = const.MYSQL_HOST,port = int(const.MYSQL_PORT), user = const.MYSQL_USER, passwd = const.MYSQL_PASS, db = '')
        cursor = mydb.cursor()
        cursor.execute('select * from sites')
        rows = cursor.fetchall() 

        if self.request.get('debug') == 'true':
            self.response.write(self.request.get('p'))
        else:
            p = eval(self.request.get('p'))
          
            ret = []

            sum = 0
            for pp in p['checkins']:
                date = pp['date']
                yy = str(date[0:4])
                mm = str(int(date[5:7]))
                dd = str(int(date[8:10]))
                hh = str(int(date[11:13]))
                lat = pp['lat']
                lon = pp['lng']
                
                min = 1111
                id = 1

                for row in rows:
                    x = lat - float(row[1])
                    y = lon - float(row[2])
                    if x < 0:
                        x = x * -1
                    if y < 0:
                        y = y * -1
                    distance = x + y
                    if distance < min:
                        min = distance
                        id = row[0]



                id = str(id)
                cmd = 'select type, val from data2 where id=' + id + ' and yy=' + yy + ' and mm=' + mm + ' and dd=' + dd + ' and hh=' + hh + ' limit 100'
                cursor.execute(cmd)
                vals = cursor.fetchall()
                
                score = 0

                for val in vals:
                    type = val[0]        
                    value = val[1]
                    if type in lim:
                       score = score + value - lim[type]

                ret.append(score*21 + 123)

                sum += score*21 + 123                

            ranking.append((self.request.get('username') , sum))
            ranking = sorted(set(ranking), key=lambda x: x[1], reverse=True)
            cache.set('ranking', ranking)

            if self.request.get('debug') == 'test':
                for r in ranking:
                    self.response.write(r)

            self.response.write(ret) 

            cache.set(key, str(ret))

        mydb.close()
Beispiel #5
0
            mc = memcache.Client([Setting.memcache], debug=0)
    except:
        error("Memcache Error")
    def save(filename, filedata):
        f = None
        try:
            f = open(dir_file(filename), 'wb')
            f.write(filedata)
        except:
            pass
        finally:
            if f: f.close()
        return filename

try:
    mc.set('memcache', '1', 3600)
except:
    mc = None

def tablename(table):
    return Setting.db_prefix + table

def cache_file_name(cacheName):
    return Setting.data_dir + "/cache/" + cacheName + ".inc"

def set_cache(cacheName, cacheValue, ttl):
    if mc:
        mc.set(cacheName, cacheValue, ttl)
    elif Setting.runtime == "LOCAL":
        if 'Page' in cacheName:
            cacheValue = str(cacheValue)