<commit_before>import json
import time
import platform

MYSQL_HOST     = "iam007.cskkndpfwwgp.ap-northeast-1.rds.amazonaws.com"
MYSQL_USERNAME = "jiangerji"
MYSQL_PASSWORD = "<PASSWORD>"
MYSQL_DATABASE = "baidu"

if platform.system() == 'Windows':
    MYSQL_USERNAME = "root"
    MYSQL_PASSWORD = "<PASSWORD>"
    MYSQL_HOST = "localhost"
    MYSQL_DATABASE = "test"

PRODUCT_ARTICEL_URL = "http://iam007.cn/index.php/14-product-category/%s-%s"
NEWS_ARTICEL_URL = "http://iam007.cn/index.php/detail/%s-%s"
EVALUATION_ARTICEL_URL = "http://iam007.cn/index.php/evaluation/item/%s-%s"


"""
select erji_content.id, erji_content.title, erji_tz_portfolio_xref_content.images, c.buy_url from erji_content, erji_tz_portfolio_xref_content, (select erji_content.id, products.buy_url from erji_content, products where erji_content.state=1 and erji_content.title = products.product_name and erji_content.catid=14) c where erji_content.id = erji_tz_portfolio_xref_content.id and erji_content.id = c.id;
"""

dal = None#DAL('sqlite://wanke.sqlite3.sqlite')
def _init():
    global dal, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_HOST, MYSQL_DATABASE
    if dal is None:
        """
        SQLite  sqlite://storage.db
        MySQL   mysql://username:password@localhost/test
        PostgreSQL  postgres://username:password@localhost/test
        """
        command = "mysql://%s:%s@%s/%s"%(MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_HOST, MYSQL_DATABASE)
        dal = DAL(command)

def convertBuyUrl(buyUrl):
    try:
        if buyUrl.find("item.jd.com") != -1:
            endIndex = buyUrl.rfind('.')
            startIndex = buyUrl.rfind('/')
            itemId = buyUrl[startIndex+1:endIndex]
            buyUrl = "http://m.jd.com/product/%s.html"%itemId
    except Exception, e:
        pass

    return buyUrl

"""
获取当前最新的产品列表，最多返回4个
"""
def latest():
    global dal
    preTime = time.time()
    _init()
    print "init cost:", (time.time() - preTime)
    preTime = time.time()

    parseRequest()

    limit = int(request.vars.get("limit", 4))

    command = 'select a.id, a.title, a.alias, c.buy_url, b.images, a.catid, a.hits, a.introtext from erji_content a join erji_tz_portfolio_xref_content b on a.state=1 and a.catid = 14 and a.id=b.contentid join products c on c.product_title = a.title and c.buy_url != "" order by a.created desc limit 0, 10;'

    lastestProducts = dal.executesql(command)

    result = {}
    result["e"] = 0

    jsonProducts = []
    for product in lastestProducts[0:limit]:
        jsonProduct = {}
        jsonProduct["i"] = product[0] # content id
        jsonProduct["n"] = product[1] # product name
        # 文章的连接
        jsonProduct['u'] = PRODUCT_ARTICEL_URL%(str(product[0]), product[2])
        jsonProduct["b"] = convertBuyUrl(product[3]) # product buy url
        jsonProduct["t"] = os.path.basename(product[4]) # product thumbnail
        jsonProduct["c"] = product[5] # content category id
        jsonProduct["h"] = product[6] # content hits
        jsonProduct["it"] = product[7] # content introduction
        
        jsonProducts.append(jsonProduct)

    result["d"] = jsonProducts

    print "lastest cost:", (time.time() - preTime)
    return json.dumps(result)

"""
获取当前最热的产品列表，最多返回10个
"""
def hotest():
    global dal
    preTime = time.time()
    _init()
    parseRequest()

    limit = int(request.vars.get("limit", 4))

    command = 'select a.id, a.title, a.alias, c.buy_url, b.images, a.catid, a.hits from erji_content a join erji_tz_portfolio_xref_content b on a.state=1 and a.catid = 14 and (a.created > date_sub(sysdate(), INTERVAL 14 DAY)) and a.id=b.contentid join products c on c.product_title = a.title order by a.hits desc limit 0, 10 ;'

    hotestProducts = dal.executesql(command)

    result = {}
    result["e"] = 0

    jsonProducts = []
    for product in hotestProducts[0:limit]:
        jsonProduct = {}
        jsonProduct["i"] = product[0] # content id
        jsonProduct["n"] = product[1] # product title
        # 文章的连接
        jsonProduct['u'] = PRODUCT_ARTICEL_URL%(str(product[0]), product[2])
        jsonProduct["b"] = convertBuyUrl(product[3]) # product buy url
        jsonProduct["t"] = os.path.basename(product[4]) # product thumbnail
        jsonProduct["c"] = product[5]
        jsonProduct["h"] = product[6]
        
        jsonProducts.append(jsonProduct)

    result["d"] = jsonProducts

    print "lastest cost:", (time.time() - preTime)
    return json.dumps(result)

"""
获取推荐页顶部互动的广告信息
http://5172.16.17.32/iam007/apis/ads
"""
def ads():
    global dal

    preTime = time.time()

    _init()
    parseRequest()
    limit = int(request.vars.get("limit", 4))

    command = 'select a.id, a.title, a.alias, c.buy_url, b.images, a.catid, a.hits from erji_content a join erji_tz_portfolio_xref_content b on a.state = 1 and a.id = b.contentid left join products c on c.product_title = a.title order by hits desc limit 0, 10;'

    allAds = dal.executesql(command)
    result = {}
    result["e"] = 0

    jsonAds = []
    for ad in allAds[0:limit]:
        jsonProduct = {}
        jsonProduct["i"] = ad[0] # content id
        jsonProduct["n"] = ad[1] # content title

        categoryId = int(ad[5])
        jsonProduct["c"] = categoryId # category id
        if categoryId == 8:
            jsonProduct['u'] = NEWS_ARTICEL_URL%(str(ad[0]), ad[2])
        elif categoryId == 14:
            jsonProduct['u'] = PRODUCT_ARTICEL_URL%(str(ad[0]), ad[2])
        elif categoryId == 15:
            jsonProduct['u'] = EVALUATION_ARTICEL_URL%(str(ad[0]), ad[2])
        else:
            continue

        jsonProduct["b"] = convertBuyUrl(ad[3]) # content buy url
        jsonProduct["t"] = os.path.basename(ad[4]) # product thumbnail
        jsonProduct["h"] = ad[6]
        
        jsonAds.append(jsonProduct)

    result["d"] = jsonAds
    print "ads cost:", (time.time() - preTime)
    return json.dumps(result)
<commit_msg>add detail<commit_after>import json
import time
import platform

MYSQL_HOST     = "iam007.cskkndpfwwgp.ap-northeast-1.rds.amazonaws.com"
MYSQL_USERNAME = "jiangerji"
MYSQL_PASSWORD = "<PASSWORD>"
MYSQL_DATABASE = "baidu"

if platform.system() == 'Windows':
    MYSQL_USERNAME = "root"
    MYSQL_PASSWORD = "<PASSWORD>"
    MYSQL_HOST = "localhost"
    MYSQL_DATABASE = "test"

PRODUCT_ARTICEL_URL = "http://iam007.cn/index.php/14-product-category/%s-%s"
NEWS_ARTICEL_URL = "http://iam007.cn/index.php/detail/%s-%s"
EVALUATION_ARTICEL_URL = "http://iam007.cn/index.php/evaluation/item/%s-%s"


"""
select erji_content.id, erji_content.title, erji_tz_portfolio_xref_content.images, c.buy_url from erji_content, erji_tz_portfolio_xref_content, (select erji_content.id, products.buy_url from erji_content, products where erji_content.state=1 and erji_content.title = products.product_name and erji_content.catid=14) c where erji_content.id = erji_tz_portfolio_xref_content.id and erji_content.id = c.id;
"""

dal = None#DAL('sqlite://wanke.sqlite3.sqlite')
def _init():
    global dal, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_HOST, MYSQL_DATABASE
    if dal is None:
        """
        SQLite  sqlite://storage.db
        MySQL   mysql://username:password@localhost/test
        PostgreSQL  postgres://username:password@localhost/test
        """
        command = "mysql://%s:%s@%s/%s"%(MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_HOST, MYSQL_DATABASE)
        dal = DAL(command)

def convertBuyUrl(buyUrl):
    try:
        if buyUrl.find("item.jd.com") != -1:
            endIndex = buyUrl.rfind('.')
            startIndex = buyUrl.rfind('/')
            itemId = buyUrl[startIndex+1:endIndex]
            buyUrl = "http://m.jd.com/product/%s.html"%itemId
    except Exception, e:
        pass

    return buyUrl

"""
获取当前最新的产品列表，最多返回4个
"""
def latest():
    global dal
    preTime = time.time()
    _init()
    print "init cost:", (time.time() - preTime)
    preTime = time.time()

    parseRequest()

    limit = int(request.vars.get("limit", 4))

    command = 'select a.id, a.title, a.alias, c.buy_url, b.images, a.catid, a.hits, a.introtext from erji_content a join erji_tz_portfolio_xref_content b on a.state=1 and a.catid = 14 and a.id=b.contentid join products c on c.product_title = a.title and c.buy_url != "" order by a.created desc limit 0, 10;'

    lastestProducts = dal.executesql(command)

    result = {}
    result["e"] = 0

    jsonProducts = []
    for product in lastestProducts[0:limit]:
        jsonProduct = {}
        jsonProduct["i"] = product[0] # content id
        jsonProduct["n"] = product[1] # product name
        # 文章的连接
        jsonProduct['u'] = PRODUCT_ARTICEL_URL%(str(product[0]), product[2])
        jsonProduct["b"] = convertBuyUrl(product[3]) # product buy url
        jsonProduct["t"] = os.path.basename(product[4]) # product thumbnail
        jsonProduct["c"] = product[5] # content category id
        jsonProduct["h"] = product[6] # content hits
        jsonProduct["it"] = product[7] # content introduction
        
        jsonProducts.append(jsonProduct)

    result["d"] = jsonProducts

    print "lastest cost:", (time.time() - preTime)
    return json.dumps(result)

"""
获取当前最热的产品列表，最多返回10个
"""
def hotest():
    global dal
    preTime = time.time()
    _init()
    parseRequest()

    limit = int(request.vars.get("limit", 4))

    command = 'select a.id, a.title, a.alias, c.buy_url, b.images, a.catid, a.hits from erji_content a join erji_tz_portfolio_xref_content b on a.state=1 and a.catid = 14 and (a.created > date_sub(sysdate(), INTERVAL 14 DAY)) and a.id=b.contentid join products c on c.product_title = a.title order by a.hits desc limit 0, 10 ;'

    hotestProducts = dal.executesql(command)

    result = {}
    result["e"] = 0

    jsonProducts = []
    for product in hotestProducts[0:limit]:
        jsonProduct = {}
        jsonProduct["i"] = product[0] # content id
        jsonProduct["n"] = product[1] # product title
        # 文章的连接
        jsonProduct['u'] = PRODUCT_ARTICEL_URL%(str(product[0]), product[2])
        jsonProduct["b"] = convertBuyUrl(product[3]) # product buy url
        jsonProduct["t"] = os.path.basename(product[4]) # product thumbnail
        jsonProduct["c"] = product[5]
        jsonProduct["h"] = product[6]
        
        jsonProducts.append(jsonProduct)

    result["d"] = jsonProducts

    print "lastest cost:", (time.time() - preTime)
    return json.dumps(result)

"""
获取推荐页顶部互动的广告信息
http://5172.16.17.32/iam007/apis/ads
"""
def ads():
    global dal

    preTime = time.time()

    _init()
    parseRequest()
    limit = int(request.vars.get("limit", 4))

    command = 'select a.id, a.title, a.alias, c.buy_url, b.images, a.catid, a.hits from erji_content a join erji_tz_portfolio_xref_content b on a.state = 1 and a.id = b.contentid left join products c on c.product_title = a.title order by hits desc limit 0, 10;'

    allAds = dal.executesql(command)
    result = {}
    result["e"] = 0

    jsonAds = []
    for ad in allAds[0:limit]:
        jsonProduct = {}
        jsonProduct["i"] = ad[0] # content id
        jsonProduct["n"] = ad[1] # content title

        categoryId = int(ad[5])
        jsonProduct["c"] = categoryId # category id
        if categoryId == 8:
            jsonProduct['u'] = NEWS_ARTICEL_URL%(str(ad[0]), ad[2])
        elif categoryId == 14:
            jsonProduct['u'] = PRODUCT_ARTICEL_URL%(str(ad[0]), ad[2])
        elif categoryId == 15:
            jsonProduct['u'] = EVALUATION_ARTICEL_URL%(str(ad[0]), ad[2])
        else:
            continue

        jsonProduct["b"] = convertBuyUrl(ad[3]) # content buy url
        jsonProduct["t"] = os.path.basename(ad[4]) # product thumbnail
        jsonProduct["h"] = ad[6]
        
        jsonAds.append(jsonProduct)

    result["d"] = jsonAds
    print "ads cost:", (time.time() - preTime)
    return json.dumps(result)

"""
获取某个content的详细内容
"""
def detail():
    global dal

    preTime = time.time()

    _init()
    parseRequest()
    contentId = int(request.vars.get("contentid", -1))
    print contentId
    if contentId == -1:
        return ""

    command = 'select a.id, a.`fulltext`, b.product_thumbnails from erji_content a join products b on a.id = %d and a.title = b.product_title and b.buy_url != "";'%contentId
    print command
    products = dal.executesql(command)
    result = {}
    if len(products) > 0:
        product = products[0]
        
        result['i'] = product[0]
        result['f'] = product[1]
        result['ts'] = product[2]

    return json.dumps(result)


{'fraction_non_alphanumeric': 0.1405686824189027, 'fraction_numerical': 0.016259511413696436, 'mean_word_length': 3.039469427369783, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3698721', 'n_tokens_mistral': 4517, 'n_tokens_neox': 4396, 'n_words': 919}