def getIpStatics(): q = sql_session.query(Ip) top_city = q.order_by(desc(Ip.visit))[0] total_visit = int(sql_session.query(func.sum(Ip.visit)).scalar()) return dict( ip_count=q.count(), top_ip=top_city.ip, top_num=top_city.visit, top_pos=top_city.position, total_visit=total_visit, )
def sitemap(): html = '''<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"> %s </urlset> ''' articles = sql_session.query(Article).order_by(Article.date.desc()).all() res = ['''<url> <loc>http://gorthon.sinaapp.com</loc> <changefreq>always</changefreq> <lastmod>%s+08:00</lastmod> <priority>1.00</priority> </url>''' % articles[0].date.strftime('%Y-%m-%dT%H:%M:%S')] for a in articles: url = a.url aid = a.aid date = a.date.strftime('%Y-%m-%dT%H:%M:%S') if url is not None: aid = '%s/%s' % (aid, url) res.append(''' <url> <loc>http://gorthon.sinaapp.com/article/home/%s</loc> <changefreq>monthly</changefreq> <lastmod>%s+08:00</lastmod> <priority>0.8</priority> </url>''' % (aid, date)) return html % ''.join(res)
def saveIp(ip): """ ip统计,将ip的地点入库; sina的api """ if web.config.debug: d = {'country': u'书记', 'province': u'书记', 'city': u'书记'} else: url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=%s' % ip d = json.loads(urllib2.urlopen(url).read()) try: position = d['country'] + '@' + d['province'] + '@' + d['city'] except KeyError: position = '@@' try: city = sql_session.query(Ip).filter_by(ip=ip)[0] city.visit += 1 except IndexError: city = Ip(ip, 1, position) sql_session.add(city) sql_session.commit() return d['city']