def insert_news(news_id, title, source_name, source_url, author, count, ico, type, imgs, summary=''): db = Database('insert') sql = """insert ignore into news (news_id, title, source_name, source_url, author, read_count, source_ico, type, summary) values('%s', '%s', '%s', '%s', '%s', %d, '%s', '%d', '%s') """ re_title = recombination(title) title = re_title if len(re_title) > 0 else title re = db.execute(sql, par=(news_id, title, source_name, source_url, author, count, ico, type, summary)) for img in imgs: if len(img) < 5: continue if img.find('https:') != 0 and img.find('http:') != 0: img = 'https:' + img db.execute( """insert ignore into image (news_id, url) values('%s', '%s')""", par=(news_id, img)) return re
def detail(id): if id == None: return '参数不能为空' thread = threading.Thread(target=increase, name='increase', args=(id, )) thread.start() db = Database('detail') data = db.query( """select * from detail where news_id = '%s' and status = 1""", (id, ), one=True) del db if data == None or len(data['content']) < 10: return script.error(id) p = re.compile('<[^>]+>') text = p.sub("", data['content']) data['description'] = text[0:150] data['description'] = re.sub(u'[\s\r\t\n\d]', '', data['description']) data['keywords'] = csynonym.divide(text) data['pubDate'] = time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime()) imgs = common.analysisImgs(data['content']) data['imgs'] = imgs[0:3] return data
def verify_user(username, password): db = Database('verify_user') re = db.query( """select password from account where number='%s' and status = 1 """, (username, ), True) if re and re.has_key('password') and re['password'] == common.md5( password): return True return False
def insert_detail(news_id, title, content, source, publishTime): db = Database('detail') sql = """insert ignore into detail (news_id, title, content, source, publish_time) values('%s', '%s', '%s', '%s', '%s') """ re_title = recombination(title) re_content = recombination(content) title = re_title if len(re_title) > 0 else title content = re_content if len(re_content) > 0 else content re = db.execute(sql, par=(news_id, title, content, source, publishTime)) return re
def banner(): db = Database('banner') re = db.query( """select * from news where create_time in (select max(create_time) from `news` where status=1 group by type)""" ) list = [] types = [] for n in re: if n['type'] not in types: types.append(n['type']) list.append(n) return manageNews(list)
def test(retry=None): db = Database('test') if retry == None: sql = "select * from news where status = 1" else: sql = "select * from news where status = 1 and news_id not in (select news_id from detail)" re = db.query(sql) del db for news in re: print insert_detail(common.md5(news['source_url']), news['news_id']) return '<center><h1> %d </h1></center>' % len(re)
def newslist(offset=0, count=10, type=1, PC=True): db = Database('newslist') if type == 6: # 推荐 re = recommend(offset) else: re = db.query( """select * from news where type = %d and status = 1 order by create_time desc limit %d offset %d""", (type, count, offset)) # print '----------------------- query data count: ' + str(len(re)) + ' -------------------------' list = manageNews(re, PC) random.shuffle(list) return list
def search(keyword, offset=0, count=10, PC=True): db = Database('search') if keyword == None: return [] newKey = '%' for c in keyword: newKey = newKey + c + '%' re = db.query( """select * from news where title like '%s' and status = 1 order by id desc limit %d offset %d""", (newKey, count, offset)) # print '----------------------- query data count: ' + str(len(re)) + ' -------------------------' list = manageNews(re, PC) return list
def hotList(max=5, type=6): db = Database('hotlist') if type != 6: re = db.query( """select * from news where type = %d and status = 1 order by read_count desc limit %d offset 0""", (type, max)) else: re = db.query( """select * from news where status = 1 order by read_count desc limit %d offset 0""", (max, )) list = [] for news in re: news['target'] = common.md5(news['source_url']) news['order'] = re.index(news) + 1 list.append(news) return list
def add_news(title, content, imgs, type, username): import uuid news_id = str(uuid.uuid1()) db = Database('add_news') user = db.query( """select nickname from account where number='%s' and status=1""", (username, ), True) del db imgs = imgs.split(',') re = script.insert_news(news_id, title, u'丽人资讯', 'wwww.somenews.cn', user['nickname'], 0, '', int(type), imgs) if re == 1: dre = script.insert_detail(news_id, title, content, user['nickname'], '') if dre == 1: script.appendIDs((news_id, )) script.post_tongji() return news_id return False
def manageNews(args=[], PC=True): db = Database('manageNews') if len(args) == 0: return args haveFullStyle = False list = [] for news in args: news['is_pc'] = PC imgs = db.query("""select url from image where news_id = '%s'""", (news['news_id'], )) srcs = [] for img in imgs: srcs.append(img['url']) rdm = random.randint(0, 9) length = len(srcs) if PC: count = 1 if length < 4 else 4 if rdm % 2 == 0 and news[ 'summary'] != None and haveFullStyle == False: haveFullStyle = True count = 2 else: count = 3 if rdm % 2 == 0 and news[ 'summary'] != None and haveFullStyle == False: haveFullStyle = True count = 2 news['imgs'] = srcs[:count] list.append(news) return list
def delete(news_id): db = Database('error') sql = """update news set status = -1 where news_id = '%s'""" db.execute(sql, (news_id, )) del db
def recommend(offset=0): db = Database('recommend') re = db.query( """select * from news where status = 1 order by create_time desc, read_count desc limit %d offset %d""", (20, offset)) return re
def increase(id): db = Database('increase') sql = """update news set read_count = read_count + 1 where news_id = '%s'""" db.execute(sql, (id, )) del db