Exemple #1
1
	def get_systematics(question_id):
		
		mysql = Mysql()

		mysql.connect_master()
		
		query_sql = "select D.name as module_name,C.name as unit_name,B.name as topic_name from (select topic_id from link_question_topic where question_id=%(question_id)d)A left outer join (select id,name,unit_id from entity_topic)B on (A.topic_id=B.id) left outer join (select id,name,module_id from entity_unit)C on (B.unit_id=C.id) left outer join (select id,name from entity_module)D on (C.module_id=D.id);" 
		
		try:
			if mysql.query(query_sql,question_id = int(question_id)):

				res = mysql.fetchall()
				systematics_list = []

				for line in res:
					module = line[0]
					unit = line[1]
					topic = line[2]					
					systematics_dict = {'module':module,'unit':unit,'topic':topic}
					systematics_list.append(systematics_dict)

				return systematics_list
			else:
				return False

		except DBException as e:
			LOG.error('get systematics error [%s]' % e)
			raise CKException('get systematics error')
Exemple #2
0
 def get(self):
     enter_func(self)
     if not set(['subject', 'grade', 'version']).issubset(self.request.arguments.keys()):
         return leave_func(self, 1)
     params = { 'subject': self.request.arguments['subject'][0], 'grade': self.request.arguments['grade'][0], 'version': self.request.arguments['version'][0] }
     if 'unit' in self.request.arguments.keys():
         params['Unite'] = self.request.arguments['unit'][0]
     if 'lesson' in self.request.arguments.keys():
         params['Lesson'] = self.request.arguments['lesson'][0]
     if 'word' in self.request.arguments.keys():
         params['word'] = self.request.arguments['word'][0]
     if 'stype' in self.request.arguments.keys():
         params['stype'] = self.request.arguments['stype'][0]
     if 'order' in self.request.arguments.keys():
         params['order'] = self.request.arguments['order'][0]
     if 'page_num' in self.request.arguments.keys():
         params['pn'] = self.request.arguments['page_num'][0]
     else:
         params['pn'] = 1
     if 'page_size' in self.request.arguments.keys():
         params['rn'] = self.request.arguments['page_size'][0]
     params['token'] = generate_token()
     params['host'] = host
     url = 'http://wenku.baidu.com/api/interface/getsubject?%s' % urllib.urlencode(params)
     LOG.info(url)
     ret = json.loads(urllib2.urlopen(url).read().decode('raw_unicode_escape'))
     LOG.info(ret)
     if 0 != ret['status']['code']:
         LOG.error('baidu library interface error: %s' % ret['status'])
         return leave_func(self, 100)
     ret = dict(error_process(0).items() + ret['data'].items())
     leave_func(self, 0)
     return self.write(json.dumps(ret, ensure_ascii=False))
Exemple #3
0
	def connect_master(self):

		if self.connect_flag:
			self.cur.close()
			self.conn.close()		
			self.connect_flag = False

		configer = Configer()
	
		host = configer.get_configer('MYSQL','host')
		port = configer.get_configer('MYSQL','port')
		user = configer.get_configer('MYSQL','user')
		passwd = configer.get_configer('MYSQL','passwd')
		db = configer.get_configer('MYSQL','db')
		charset = configer.get_configer('MYSQL','charset')

		try:
			self.conn = MySQLdb.connect(
					host = host,
					port = int(port),
					user = user,
					passwd = passwd,
					db = db,
					charset = charset)
			
			self.cur = self.conn.cursor()
			self.connect_flag = True

		except MySQLdb.Error,e:
			self.status = self.status_enum.CONN_ERR
			msg = 'connect failed'
			LOG.error('Error:%s' % str(e))
			raise DBException(msg)
Exemple #4
0
	def q_subject_list():

		mysql = Mysql()
		
		mysql.connect_master()

		query_sql = "select distinct subject from entity_question_new where type is not null;"	

		subject_list = []

		try:
			if mysql.query(query_sql):
				subject_tuple =  mysql.fetchall()
				
				for type in subject_tuple:
					tmp_tuple = (type[0])
					subject_list.append(tmp_tuple)
				return subject_list

			else:
				return None

		except DBException as e:
			LOG.error('get subject error [%s]' % e)
			raise CkException('get subject error')
Exemple #5
0
	def _upload_qiniu(self,ori_url):
		from gl import LOG
		LOG.info('Original Image Url [%s]' % ori_url)
		if not self.img_url_exp.match(ori_url):
			suffix = ori_url[ori_url.rfind('.'):]	
			qiniu_file_name = md5(ori_url).hexdigest() + suffix
			
			LOG.info('Open Refer Imgage[%s]' % ori_url)

			request = urllib2.Request(ori_url)
			response = urllib2.urlopen(request)
			img_data =  response.read()

			#LOG.info('img data [%s]' % img_data)	

			qiniu = QiniuWrap()
			res = qiniu.upload_data('qdimg',qiniu_file_name,img_data)

			if not res:
				qiniu_url = self.qiniu_prefix % ('qdimg',qiniu_file_name)
				LOG.info('[%s] local [%s] successful' % (ori_url,qiniu_url))			
				return qiniu_url
			else:
				LOG.error('upload qiniu error [%s]' % res)
				return None
Exemple #6
0
    def _upload_qiniu(self, ori_url):
        from gl import LOG
        LOG.info('Original Image Url [%s]' % ori_url)
        if not self.img_url_exp.match(ori_url):
            suffix = ori_url[ori_url.rfind('.'):]
            qiniu_file_name = md5(ori_url).hexdigest() + suffix

            LOG.info('Open Refer Imgage[%s]' % ori_url)

            request = urllib2.Request(ori_url)
            response = urllib2.urlopen(request)
            img_data = response.read()

            #LOG.info('img data [%s]' % img_data)

            qiniu = QiniuWrap()
            res = qiniu.upload_data('qdimg', qiniu_file_name, img_data)

            if not res:
                qiniu_url = self.qiniu_prefix % ('qdimg', qiniu_file_name)
                LOG.info('[%s] local [%s] successful' % (ori_url, qiniu_url))
                return qiniu_url
            else:
                LOG.error('upload qiniu error [%s]' % res)
                return None
Exemple #7
0
	def get_group_list(system_id):
	
		mysql = Mysql()

		mysql.connect_master()
		
		query_sql = "select A.id,A.name,B.num from (select id,name from entity_group where system_id=%(system_id)d or id=0)A left outer join (select question_group,count(1) as num from entity_question where upload_id=%(system_id)d group by question_group)B on (A.id=B.question_group);" 
		
		try:
			if mysql.query(query_sql,system_id = system_id):

				res = mysql.fetchall()

				group_list = []

				for line in res:
					group_id = line[0]
					group_name = line[1]
					question_num = int(line[2]) if line[2] else 0					
					group_dict = {'id':int(group_id),'name':group_name,'num':int(question_num)}
					group_list.append(group_dict)
			
				return group_list
			else:
				return False

		except DBException as e:
			LOG.error('check topic error [%s]' % e)
			raise CKException('check topic error')
Exemple #8
0
    def connect_master(self):

        if self.connect_flag:
            self.cur.close()
            self.conn.close()
            self.connect_flag = False

        configer = Configer()

        host = configer.get_configer('MYSQL', 'host')
        port = configer.get_configer('MYSQL', 'port')
        user = configer.get_configer('MYSQL', 'user')
        passwd = configer.get_configer('MYSQL', 'passwd')
        db = configer.get_configer('MYSQL', 'db')
        charset = configer.get_configer('MYSQL', 'charset')

        try:
            self.conn = MySQLdb.connect(host=host,
                                        port=int(port),
                                        user=user,
                                        passwd=passwd,
                                        db=db,
                                        charset=charset)

            self.cur = self.conn.cursor()
            self.connect_flag = True

        except MySQLdb.Error, e:
            self.status = self.status_enum.CONN_ERR
            msg = 'connect failed'
            LOG.error('Error:%s' % str(e))
            raise DBException(msg)
Exemple #9
0
 def get(self):
     enter_func(self)
     params = {'token': generate_token(), 'host': host, 'zone': 8}
     if 'zone' in self.request.arguments.keys(
     ) and self.request.arguments['zone'][0].isdigit():
         params['zone'] = int(self.request.arguments['zone'][0])
     redis_handle = redis.Redis(Configer().get_configer('REDIS', 'host'))
     if redis_handle.exists('json_class:%d' % params['zone']):
         leave_func(self, 0)
         return self.write(
             redis_handle.get('json_class:%d' % params['zone']))
     url = 'http://wenku.baidu.com/api/interface/getclass?%s' % urllib.urlencode(
         params)
     LOG.info(url)
     ret = json.loads(
         urllib2.urlopen(url).read().decode('raw_unicode_escape'))
     LOG.info(ret)
     if 0 != ret['status']['code']:
         LOG.error('baidu library interface error: %s' % ret['status'])
         return leave_func(self, 100)
     result = error_process(0)
     result['data'] = ret['data']
     result = json.dumps(result, sort_keys=True, ensure_ascii=False)
     redis_handle.set('json_class:%d' % params['zone'], result)
     leave_func(self, 0)
     return self.write(result)
Exemple #10
0
	def q_mark_list():

		mysql = Mysql()
		
		mysql.connect_master()

		query_sql = "select id,name from link_question_mark where enable=1;"	

		mark_list = []

		try:
			if mysql.query(query_sql):
				mark_tuple =  mysql.fetchall()
				
				for mark in mark_tuple:
					tmp_tuple = (mark[0],mark[1])
					mark_list.append(tmp_tuple)
				return mark_list

			else:
				return None

		except DBException as e:
			LOG.error('get mark error [%s]' % e)
			raise CkException('get mark error')
Exemple #11
0
 def get(self):
     enter_func(self)
     if not set(['word']).issubset(self.request.arguments.keys()):
         return leave_func(self, 1)
     if 'page_num' in self.request.arguments.keys() and self.request.arguments['page_num'][0]:
         page_num = int(self.request.arguments['page_num'][0])
     else:
         page_num = 1
     if 'page_size' in self.request.arguments.keys() and self.request.arguments['page_size'][0]:
         page_size = int(self.request.arguments['page_size'][0])
     else:
         page_size = 10
     keyword = self.request.arguments['word'][0]
     if 'uid' in self.request.arguments.keys() and self.request.arguments['uid'][0]:
         uid = int(self.request.arguments['uid'][0])
         LOG.info('data_statistics: %s' % {'uid': uid, 'keyword': keyword, 'func': self.__class__.__name__})
     if not keyword or page_num < 1 or page_size < 1:
         return leave_func(self, 1)
     url = 'http://wenku.baidu.com/api/interface/search?%s' % urllib.urlencode({ 'word': keyword, 'pn': page_num, 'rn': page_size, 'token': generate_token(), 'host': host })
     LOG.info(url)
     docs = json.loads(urllib2.urlopen(url).read().decode('raw_unicode_escape'))
     LOG.info(docs)
     if 0 != docs['status']['code']:
         LOG.error(docs['status'])
         return leave_func(self, 100)
     ret = dict(error_process(0).items() + docs['data'].items())
     if 'jsonp' in self.request.arguments.keys():
         jsonp = self.request.arguments['jsonp'][0]
         leave_func(self, 0)
         return self.write('%s(%s)' % (jsonp, json.dumps(ret, ensure_ascii=False)))
     leave_func(self, 0)
     return self.write(json.dumps(ret, ensure_ascii=False))
Exemple #12
0
	def select_collection(self,collection):
		
		try:
			self.collection = self.db[collection]

		except ServerSelectionTimeoutError as e:
			LOG.error('mongo select failed [%s]' % e)
			raise DBException('mongo select failed')
Exemple #13
0
    def select_collection(self, collection):

        try:
            self.collection = self.db[collection]

        except ServerSelectionTimeoutError as e:
            LOG.error('mongo select failed [%s]' % e)
            raise DBException('mongo select failed')
Exemple #14
0
    def connect(self, db):

        configer = Configer()

        mongo_host = configer.get_configer('MONGO', 'host')
        mongo_port = int(configer.get_configer('MONGO', 'port'))

        try:
            self.client = MongoClient(host=mongo_host, port=mongo_port)
            self.db = self.client[db]

        except ConnectionFailure, e:
            LOG.error('mongo connect failed [%s]' % e)
            raise DBException('mongo connect failed')
Exemple #15
0
	def connect(self,db):
	
		configer = Configer()

		mongo_host = configer.get_configer('MONGO','host')
		mongo_port = int(configer.get_configer('MONGO','port'))
			
		try:
			self.client = MongoClient(host = mongo_host,port = mongo_port)
			self.db = self.client[db]
	
		except ConnectionFailure,e:
			LOG.error('mongo connect failed [%s]' % e)
			raise DBException('mongo connect failed')
Exemple #16
0
 def get(self):
     enter_func(self)
     if not set(['word']).issubset(self.request.arguments.keys()):
         return leave_func(self, 1)
     if 'page_num' in self.request.arguments.keys(
     ) and self.request.arguments['page_num'][0]:
         page_num = int(self.request.arguments['page_num'][0])
     else:
         page_num = 1
     if 'page_size' in self.request.arguments.keys(
     ) and self.request.arguments['page_size'][0]:
         page_size = int(self.request.arguments['page_size'][0])
     else:
         page_size = 10
     keyword = self.request.arguments['word'][0]
     if 'uid' in self.request.arguments.keys(
     ) and self.request.arguments['uid'][0]:
         uid = int(self.request.arguments['uid'][0])
         LOG.info('data_statistics: %s' % {
             'uid': uid,
             'keyword': keyword,
             'func': self.__class__.__name__
         })
     if not keyword or page_num < 1 or page_size < 1:
         return leave_func(self, 1)
     url = 'http://wenku.baidu.com/api/interface/search?%s' % urllib.urlencode(
         {
             'word': keyword,
             'pn': page_num,
             'rn': page_size,
             'token': generate_token(),
             'host': host
         })
     LOG.info(url)
     docs = json.loads(
         urllib2.urlopen(url).read().decode('raw_unicode_escape'))
     LOG.info(docs)
     if 0 != docs['status']['code']:
         LOG.error(docs['status'])
         return leave_func(self, 100)
     ret = dict(error_process(0).items() + docs['data'].items())
     if 'jsonp' in self.request.arguments.keys():
         jsonp = self.request.arguments['jsonp'][0]
         leave_func(self, 0)
         return self.write('%s(%s)' %
                           (jsonp, json.dumps(ret, ensure_ascii=False)))
     leave_func(self, 0)
     return self.write(json.dumps(ret, ensure_ascii=False))
Exemple #17
0
    def get_group_list(system_id):

        mysql = Mysql()

        mysql.connect_master()

        query_sql = "select A.id,count(1) from entity_group A,entity_question B where (A.system_id=%(system_id)d or A.id=0) and A.enable=1 and B.upload_id=%(system_id)d and A.id=B.question_group group by B.question_group;"

        group_sql = "select id,name from entity_group where (system_id=%(system_id)d or id=0) order by create_time desc;"

        try:
            group_dict = OrderedDict()
            group_list = []
            default_num = 0

            mysql.query(group_sql, system_id=system_id)
            group_res = mysql.fetchall()

            mysql.query(query_sql, system_id=system_id)
            num_res = mysql.fetchall()

            for group in group_res:
                group_id = group[0]
                group_name = group[1]
                group_dict[group_id] = {
                    'id': int(group_id),
                    'name': group_name,
                    'num': 0
                }

            for num in num_res:
                gid = num[0]
                num = num[1]
                group_dict[gid]['num'] = num

            for gid in group_dict:
                if 0 == gid:
                    default_num = group_dict[gid]['num']
                    continue

                group_list.append(group_dict[gid])

            return group_list, default_num

        except DBException as e:
            LOG.error('check topic error [%s]' % e)
            raise CKException('check topic error')
Exemple #18
0
	def update_json_by_id(oldid,json):
		
		mysql = Mysql()

		mysql.connect_master()

		query_sql = "update entity_question_new set json='%(question_json)s' where oldid=%(oldid)d;"	

		try:
			if mysql.query(query_sql,oldid = int(oldid),question_json = json):
				return True
			else:
				return False

		except DBException as e:
			LOG.error('update json error [%s]' % e)
			raise CkException('update json error')
Exemple #19
0
    def chapter_id_exist(chapter_id):

        mysql = Mysql()

        mysql.connect_master()

        query_sql = "select 1 from entity_teaching_chapter where id='%(chapter_id)d';"

        try:
            if mysql.query(query_sql, chapter_id=int(chapter_id)):
                return True
            else:
                return False

        except DBException as e:
            LOG.error('check chapter error [%s]' % e)
            raise CKException('check chapter error')
Exemple #20
0
	def group_id_exist(group_id):
		
		mysql = Mysql()

		mysql.connect_master()
		
		query_sql = "select 1 from entity_group where id = '%(group_id)d';" 
		
		try:
			if mysql.query(query_sql,group_id = int(group_id)):
				return True
			else:
				return False

		except DBException as e:
			LOG.error('check topic error [%s]' % e)
			raise CKException('check topic error')
Exemple #21
0
	def chapter_id_exist(chapter_id):

		mysql = Mysql()

		mysql.connect_master()
		
		query_sql = "select 1 from entity_teaching_chapter where id='%(chapter_id)d';" 
		
		try:
			if mysql.query(query_sql,chapter_id = int(chapter_id)):
				return True
			else:
				return False

		except DBException as e:
			LOG.error('check chapter error [%s]' % e)
			raise CKException('check chapter error')
Exemple #22
0
	def is_type(type_id):
		
		mysql = Mysql()
		
		mysql.connect_master()

		query_sql = "select name from entity_question_type where type_id = %(type_id)d and enable = 1;"
		
		try:
			if mysql.query(query_sql,type_id = int(type_id)):
				return mysql.fetch()[0]
			else:
				return False

		except DBException as e:
			LOG.error('check type error [%s]' % e)
			raise CkException('check type error')
Exemple #23
0
	def is_seriess(seriess_id):
		
		mysql = Mysql()
		
		mysql.connect_master()

		query_sql = "select 1 from entity_seriess where id = %(seriess_id)d;"
		
		try:
			if mysql.query(query_sql,seriess_id = int(seriess_id)):
				return True
			else:
				return False

		except DBException as e:
			LOG.error('check seriess error [%s]' % e)
			raise CkException('check seriess error')
Exemple #24
0
	def is_topic(topic_id):
		
		mysql = Mysql()

		mysql.connect_master()
		
		query_sql = "select 1 from entity_topic where id = %(topic_id)d;" 
		
		try:
			if mysql.query(query_sql,topic_id = int(topic_id)):
				return True
			else:
				return False

		except DBException as e:
			LOG.error('check topic error [%s]' % e)
			raise CKException('check topic error')
Exemple #25
0
 def get(self):
     enter_func(self)
     if not set(['doc_id']).issubset(self.request.arguments.keys()):
         return leave_func(self, 1)
     doc_id = self.request.arguments['doc_id'][0]
     if 'uid' in self.request.arguments.keys():
         uid = self.request.arguments['uid'][0]
         LOG.info('data_statistics: %s' % {'uid': uid, 'doc_id': doc_id, 'func': self.__class__.__name__})
     url = 'http://wenku.baidu.com/api/interface/getdocinfobyid?doc_id=%s&token=%s&host=%s' % (doc_id, generate_token(), host)
     LOG.info(url)
     ret = json.loads(urllib2.urlopen(url).read().decode('raw_unicode_escape'))
     LOG.info(ret)
     if 0 != ret['status']['code']:
         LOG.error('baidu library interface error: %s' % ret['status'])
         return leave_func(self, 100)
     ret = dict(error_process(0).items() + ret['data'].items())
     leave_func(self, 0)
     return self.write(ret)
Exemple #26
0
	def verify(username,oldid,newid,verify):
		
		mysql = Mysql()

		mysql.connect_master()

		query_sql = "insert into entity_verify (username,oldid,newid,state) values ('%(username)s',%(oldid)d,%(newid)d,%(verify)d);"	

		try:
			if mysql.query(query_sql,username = username,oldid = int(oldid),newid = int(newid),verify = int(verify)):
				return mysql.get_last_id()

			else:
				return None

		except DBException as e:
			LOG.error('add mark error [%s]' % e)
			raise CkException('add mark error')
Exemple #27
0
	def add_mark(name):

		mysql = Mysql()

		mysql.connect_master()

		query_sql = "insert into link_question_mark (name,mark_time) values ('%(name)s',now());"	

		try:
			if mysql.query(query_sql,name = name):
				return mysql.get_last_id()

			else:
				return None

		except DBException as e:
			LOG.error('add mark error [%s]' % e)
			raise CkException('add mark error')
Exemple #28
0
	def q_mark(oldid,newid,mark):

		mysql = Mysql()

		mysql.connect_master()

		query_sql = "insert into entity_question_mark (oldid,newid,mark,mark_time) values (%(oldid)d,%(newid)d,%(mark)d,now());"	

		try:
			if mysql.query(query_sql,oldid = oldid,newid = newid,mark = mark):
				return 'success'

			else:
				return None

		except DBException as e:
			LOG.error('mark error [%s]' % e)
			raise CkException('mark error')
Exemple #29
0
	def get_json_by_id(oldid):
		
		mysql = Mysql()

		mysql.connect_master()

		query_sql = "select json from entity_question_new where oldid=%(oldid)d;"	

		try:
			if mysql.query(query_sql,oldid = int(oldid)):
				json = mysql.fetch()[0]
				return json
			else:
				return False

		except DBException as e:
			LOG.error('get json error [%s]' % e)
			raise CkException('get json error')
Exemple #30
0
	def q_subject_filter(type,start,num):

		mysql = Mysql()

		mysql.connect_master()

		query_sql = "select oldid,subject from entity_question_new where subject = '%(type)s' limit %(start)d,%(num)d;"	

		try:
			if mysql.query(query_sql,type = type,start = start,num = num):
				return mysql.fetchall()

			else:
				return None

		except DBException as e:
			LOG.error('filtet type error [%s]' % e)
			raise CkException('filter type error')
Exemple #31
0
	def q_subject_filter_num(type):

		mysql = Mysql()

		mysql.connect_master()

		query_sql = "select count(*) from entity_question_new where subject = '%(type)s';"	

		try:
			if mysql.query(query_sql,type = type):
				return mysql.fetchall()[0][0]

			else:
				return None

		except DBException as e:
			LOG.error('filtet type error [%s]' % e)
			raise CkException('filter type error')
Exemple #32
0
	def get_group_list(system_id):
	
		mysql = Mysql()

		mysql.connect_master()
		
		query_sql = "select A.id,count(1) from entity_group A,entity_question B where (A.system_id=%(system_id)d or A.id=0) and A.enable=1 and B.upload_id=%(system_id)d and A.id=B.question_group group by B.question_group;" 
		
		group_sql = "select id,name from entity_group where (system_id=%(system_id)d or id=0) order by create_time desc;"
		
		try:
			group_dict = OrderedDict()
			group_list = []
			default_num = 0

			mysql.query(group_sql,system_id = system_id)
			group_res = mysql.fetchall()

			mysql.query(query_sql,system_id = system_id)
			num_res = mysql.fetchall()

			for group in group_res:
				group_id = group[0]
				group_name = group[1]
				group_dict[group_id] = {'id':int(group_id),'name':group_name,'num':0}

			for num in num_res:
				gid = num[0]
				num = num[1]
				group_dict[gid]['num'] = num					

			for gid in group_dict:
				if 0 == gid:
					default_num = group_dict[gid]['num']
					continue

				group_list.append(group_dict[gid])

			return group_list,default_num
		
		except DBException as e:
			LOG.error('check topic error [%s]' % e)
			raise CKException('check topic error')
Exemple #33
0
	def add_user(username,password):

		password = Base.md5(password)
	
		mysql = Mysql()

		mysql.connect_master()

		query_sql = "insert into verify_user (username,password) values ('%(username)s','%(password)s');"	

		try:
			if mysql.query(query_sql,username = username,password = password):
				return mysql.get_last_id()

			else:
				return None

		except DBException as e:
			LOG.error('add user error [%s]' % e)
			raise CkException('add user error')
Exemple #34
0
	def check_user(username,password):
		
		password = Base.md5(password)
	
		mysql = Mysql()

		mysql.connect_master()

		query_sql = "select password from verify_user where username='******';"	

		try:
			if mysql.query(query_sql,username = username):
				pwd = mysql.fetch()[0]
				if password == pwd:
					return True
			else:
				return False

		except DBException as e:
			LOG.error('check user error [%s]' % e)
			raise CkException('check user error')
Exemple #35
0
 def get(self):
     enter_func(self)
     if not set(['subject', 'grade', 'version']).issubset(
             self.request.arguments.keys()):
         return leave_func(self, 1)
     params = {
         'subject': self.request.arguments['subject'][0],
         'grade': self.request.arguments['grade'][0],
         'version': self.request.arguments['version'][0]
     }
     if 'unit' in self.request.arguments.keys():
         params['Unite'] = self.request.arguments['unit'][0]
     if 'lesson' in self.request.arguments.keys():
         params['Lesson'] = self.request.arguments['lesson'][0]
     if 'word' in self.request.arguments.keys():
         params['word'] = self.request.arguments['word'][0]
     if 'stype' in self.request.arguments.keys():
         params['stype'] = self.request.arguments['stype'][0]
     if 'order' in self.request.arguments.keys():
         params['order'] = self.request.arguments['order'][0]
     if 'page_num' in self.request.arguments.keys():
         params['pn'] = self.request.arguments['page_num'][0]
     else:
         params['pn'] = 1
     if 'page_size' in self.request.arguments.keys():
         params['rn'] = self.request.arguments['page_size'][0]
     params['token'] = generate_token()
     params['host'] = host
     url = 'http://wenku.baidu.com/api/interface/getsubject?%s' % urllib.urlencode(
         params)
     LOG.info(url)
     ret = json.loads(
         urllib2.urlopen(url).read().decode('raw_unicode_escape'))
     LOG.info(ret)
     if 0 != ret['status']['code']:
         LOG.error('baidu library interface error: %s' % ret['status'])
         return leave_func(self, 100)
     ret = dict(error_process(0).items() + ret['data'].items())
     leave_func(self, 0)
     return self.write(json.dumps(ret, ensure_ascii=False))
Exemple #36
0
    def connect_test(self):

        if self.connect_flag:
            self.cur.close()
            self.conn.close()
            self.connect_flag = False

        try:
            self.conn = MySQLdb.connect(host='10.60.0.151',
                                        port=3306,
                                        user='******',
                                        passwd='1a2s3dqwe',
                                        db='test',
                                        charset='utf8')

            self.cur = self.conn.cursor()

        except MySQLdb.Error, e:
            self.status = self.status_enum.CONN_ERR
            msg = 'connect failed'
            LOG.error('Error:%s' % str(e))
            raise DBException(msg)
Exemple #37
0
 def get(self):
     enter_func(self)
     params = { 'token': generate_token(), 'host': host, 'zone': 8 }
     if 'zone' in self.request.arguments.keys() and self.request.arguments['zone'][0].isdigit():
         params['zone'] = int(self.request.arguments['zone'][0])
     redis_handle = redis.Redis(Configer().get_configer('REDIS', 'host'))
     if redis_handle.exists('json_class:%d' % params['zone']):
         leave_func(self, 0)
         return self.write(redis_handle.get('json_class:%d' % params['zone']))
     url = 'http://wenku.baidu.com/api/interface/getclass?%s' % urllib.urlencode(params)
     LOG.info(url)
     ret = json.loads(urllib2.urlopen(url).read().decode('raw_unicode_escape'))
     LOG.info(ret)
     if 0 != ret['status']['code']:
         LOG.error('baidu library interface error: %s' % ret['status'])
         return leave_func(self, 100)
     result = error_process(0)
     result['data'] = ret['data']
     result = json.dumps(result, sort_keys=True, ensure_ascii=False)
     redis_handle.set('json_class:%d' % params['zone'], result)
     leave_func(self, 0)
     return self.write(result)
Exemple #38
0
	def connect_test(self):

		if self.connect_flag:
			self.cur.close()
			self.conn.close()
			self.connect_flag = False

		try:
			self.conn = MySQLdb.connect(
					host = '10.60.0.151',
					port = 3306,
					user = '******',
					passwd = '1a2s3dqwe', 
					db = 'test',
					charset = 'utf8')
			
			self.cur = self.conn.cursor()

		except MySQLdb.Error,e:
			self.status = self.status_enum.CONN_ERR
			msg = 'connect failed'
			LOG.error('Error:%s' % str(e))
			raise DBException(msg)
Exemple #39
0
 def get(self):
     enter_func(self)
     if not set(['doc_id']).issubset(self.request.arguments.keys()):
         return leave_func(self, 1)
     doc_id = self.request.arguments['doc_id'][0]
     if 'uid' in self.request.arguments.keys():
         uid = self.request.arguments['uid'][0]
         LOG.info('data_statistics: %s' % {
             'uid': uid,
             'doc_id': doc_id,
             'func': self.__class__.__name__
         })
     url = 'http://wenku.baidu.com/api/interface/getdocinfobyid?doc_id=%s&token=%s&host=%s' % (
         doc_id, generate_token(), host)
     LOG.info(url)
     ret = json.loads(
         urllib2.urlopen(url).read().decode('raw_unicode_escape'))
     LOG.info(ret)
     if 0 != ret['status']['code']:
         LOG.error('baidu library interface error: %s' % ret['status'])
         return leave_func(self, 100)
     ret = dict(error_process(0).items() + ret['data'].items())
     leave_func(self, 0)
     return self.write(ret)
Exemple #40
0
	def post(self):

		for i in range(1):
			LOG.info('API IN[%s]' % (self.__class__.__name__))
                        LOG.info('PARAMETER IN[%s]' % self.request.arguments)

                        ret = {'code':'','message':''}
			essential_keys = set(['filepath','timestamp','secret'])

			if Base.check_parameter(set(self.request.arguments.keys()),essential_keys):
				ret['code'] = 1
				ret['message'] = 'invalid parameter'
				LOG.error('ERROR[in parameter invalid]')
				break
			
			filepath = ''.join(self.request.arguments['filepath'])
			timestamp = ''.join(self.request.arguments['timestamp'])
			secret = ''.join(self.request.arguments['secret'])

			if Base.empty(filepath) or Base.empty(timestamp) or Base.empty(secret):
					ret['code'] = 1
					ret['message'] = 'invalid parameter'
					LOG.error('ERROR[parameter empty]')
					break
			
			key = filepath + timestamp
			secret_key = sha1(key).hexdigest()
			
			if secret == secret_key:
				doit = Doit()
				doit.transcode(filepath)
				LOG.info('QUEUE[put transcode task(%s)}' % filepath)

				ret['code'] = 0
				ret['message'] = 'success'
				break
                        else:
                                ret['code'] = 4
                                ret['message'] = 'secure key error'
                                LOG.error('ERR[secure key error]')
                                break

                self.write(json.dumps(ret))
                LOG.info('PARAMETER OUT[%s]' % ret)
                LOG.info('API OUT[%s]' % (self.__class__.__name__))
Exemple #41
0
    def post(self):

        for i in range(1):

            LOG.info('API IN[%s]' % (self.__class__.__name__))
            LOG.info('PARAMETER IN[%s]' % self.request.arguments)

            ret = {'code': '', 'message': ''}

            essential_keys = set(['question_id', 'group_id'])

            if Base.check_parameter(set(self.request.arguments.keys()),
                                    essential_keys):
                ret['code'] = 1
                ret['message'] = '无效参数'
                LOG.error('ERROR[in parameter invalid]')
                break

            question_id = ''.join(self.request.arguments['question_id'])
            group_id = ''.join(self.request.arguments['group_id'])

            if Base.empty(question_id) or Base.empty(group_id):
                ret['code'] = 1
                ret['message'] = '无效参数'
                LOG.error('ERROR[parameter empty]')
                break

            configer = Configer()
            remote_host = configer.get_configer('REMOTE', 'host')
            remote_port = configer.get_configer('REMOTE', 'port')
            remote_uri = configer.get_configer('REMOTE', 'uri')

            remote_url = "http://%s:%s/%s" % (remote_host, remote_port,
                                              remote_uri)

            token = self.get_cookie("teacher_id")
            LOG.info('TOKEN[%s]' % token)

            if token is None:
                ret['code'] = 6
                ret['message'] = '无效参数'
                LOG.error('ERROR[token empty]')
                break

            post_data = {'token': token}

            client = httpclient.AsyncHTTPClient()
            response = yield gen.Task(client.fetch,
                                      remote_url,
                                      method='POST',
                                      body=urllib.urlencode(post_data))
            #response = Http.post(remote_url,post_data)

            LOG.info('REMOTE RES CODE[%d]' % response.code)

            if 200 == response.code:
                encode_body = json.loads(response.body)

                if 0 == encode_body['code'] or 2 == encode_body['code']:
                    ret['code'] = 7
                    ret['message'] = '无效参数'
                    LOG.error('ERROR[token not exist]')
                    break

                if 1 == encode_body['code']:
                    subject_id = encode_body['subject_id']
                    grade_id = encode_body['grade_id']
                    system_id = encode_body['system_id']
                    org_type = encode_body['org_type']

                    db = Mysql()

                    question_sql = "update entity_question set question_group=%(group_id)d where upload_id=%(system_id)d and id=%(question_id)d;"

                    try:
                        db.connect_master()

                        question_res = db.query(question_sql,
                                                question_id=int(question_id),
                                                system_id=int(system_id),
                                                group_id=int(group_id))
                        if not question_res:
                            ret['code'] = 3
                            ret['message'] = '服务器错误'
                            LOG.error('ERROR[mysql error]')
                            break

                        question_sql = db.get_last_sql()
                        LOG.info('SQL[%s] - RES[%s]' %
                                 (question_sql, question_res))

                    except DBException as e:
                        db.rollback()
                        ret['code'] = 3
                        ret['message'] = '服务器错误'
                        LOG.error('ERROR[mysql error]')
                        break

                else:
                    ret['code'] = 3
                    ret['message'] = '服务器错误'
                    LOG.error('ERROR[remote error]')
                    break

            else:
                ret['code'] = 3
                ret['message'] = '服务器错误'
                LOG.error('ERROR[remote error]')
                break

            ret['code'] = 0
            ret['message'] = 'success'
            break

        self.write(json.dumps(ret))
        self.finish()
        LOG.info('PARAMETER OUT[%s]' % ret)
        LOG.info('API OUT[%s]' % (self.__class__.__name__))
Exemple #42
0
    def post(self):

        for i in range(1):

            LOG.info('API IN[%s]' % (self.__class__.__name__))
            LOG.info('PARAMETER IN[%s]' % self.request.arguments)

            ret = {'code': '', 'message': ''}

            essential_keys = set(['name'])

            if Base.check_parameter(set(self.request.arguments.keys()),
                                    essential_keys):
                ret['code'] = 1
                ret['message'] = '无效参数'
                LOG.error('ERROR[in parameter invalid]')
                break

            group_name = ''.join(self.request.arguments['name'])
            #timestamp = ''.join(self.request.arguments['timestamp'])
            #secret = ''.join(self.request.arguments['secret'])

            if Base.empty(group_name):
                ret['code'] = 1
                ret['message'] = '无效参数'
                LOG.error('ERROR[parameter empty]')
                break

            #key = group_name + timestamp;
            #secret_key = sha1(key).hexdigest()

            configer = Configer()
            remote_host = configer.get_configer('REMOTE', 'host')
            remote_port = configer.get_configer('REMOTE', 'port')
            remote_uri = configer.get_configer('REMOTE', 'uri')

            remote_url = "http://%s:%s/%s" % (remote_host, remote_port,
                                              remote_uri)

            token = self.get_cookie("teacher_id")
            LOG.info('TOKEN[%s]' % token)

            if token is None:
                ret['code'] = 6
                ret['message'] = '无效参数'
                LOG.error('ERROR[token empty]')
                break

            post_data = {'token': token}

            client = httpclient.AsyncHTTPClient()
            response = yield gen.Task(client.fetch,
                                      remote_url,
                                      method='POST',
                                      body=urllib.urlencode(post_data))
            #response = Http.post(remote_url,post_data)

            LOG.info('REMOTE RES CODE[%d]' % response.code)

            if 200 == response.code:
                encode_body = json.loads(response.body)

                if 0 == encode_body['code'] or 2 == encode_body['code']:
                    ret['code'] = 7
                    ret['message'] = '无效参数'
                    LOG.error('ERROR[token not exist]')
                    break

                if 1 == encode_body['code']:
                    subject_id = encode_body['subject_id']
                    grade_id = encode_body['grade_id']
                    system_id = encode_body['system_id']
                    org_type = encode_body['org_type']

                    if Business.group_name_exist(group_name, system_id):
                        ret['code'] = 6
                        ret['message'] = '组名已存在'
                        LOG.error('ERROR[group exist]')
                        break

                    db = Mysql()

                    group_sql = "insert into entity_group (name,system_id) values ('%(group_name)s',%(system_id)d);"

                    try:
                        db.connect_master()
                        group_res = db.query(group_sql,
                                             group_name=group_name,
                                             system_id=system_id)

                        group_sql = db.get_last_sql()
                        group_id = db.get_last_id()
                        LOG.info('SQL[%s] - RES[%s] - INS[%d]' %
                                 (group_sql, group_res, group_id))

                    except DBException as e:
                        ret['code'] = 3
                        ret['message'] = '服务器错误'
                        LOG.error('ERROR[mysql error]')
                        break

                else:
                    ret['code'] = 3
                    ret['message'] = '服务器错误'
                    LOG.error('ERROR[remote error]')
                    break

            else:
                ret['code'] = 3
                ret['message'] = '服务器错误'
                LOG.error('ERROR[remote error]')
                break

            ret['id'] = group_id
            ret['name'] = group_name
            ret['code'] = 0
            ret['message'] = 'success'
            break

        self.write(json.dumps(ret))
        self.finish()
        LOG.info('PARAMETER OUT[%s]' % ret)
        LOG.info('API OUT[%s]' % (self.__class__.__name__))
Exemple #43
0
	def post(self):

		for i in range(1):
		
			LOG.info('API IN[%s]' % (self.__class__.__name__))
			LOG.info('PARA IN[%s]' % self.request.arguments)
			
			ret = {'code':'','message':''}

			schoolid = ''.join(self.request.arguments['schoolid'])
			filepath = ''.join(self.request.arguments['filepath'])
			timestamp = ''.join(self.request.arguments['timestamp'])
			secret = ''.join(self.request.arguments['secret'])

			key = schoolid + filepath + timestamp
			secret_key = sha1(key).hexdigest()

			if secret == secret_key:

				db = Mysql()
				db.connect_master()

				ip_sql  = "select ip from entity_school_info where school_id = '%s'" % (schoolid)

				schoolip = db.query(ip_sql)

				if schoolip is None:

					ret['code'] = -2 
					ret['message'] = 'schoolid is not exist'
					
					LOG.error('schoolid is not exist [%s]' % schoolid)
					break
					
				schoolip = ''.join(schoolip)

				LOG.info('remote school ip [%s]' % (schoolip))

				timestamp = str(int(time.time()))

				key = filepath + timestamp
				secret = sha1(key).hexdigest()

				post_data = {'filepath':filepath,'timestamp':timestamp,'secret':secret}

				local_port = Base.get_config('LOCAL','port')
				
				schoolip = 'http://' + schoolip +':' + local_port + '/transcode'

				client = httpclient.AsyncHTTPClient()
				response = yield gen.Task(client.fetch,schoolip,method = 'POST',body = urllib.urlencode(post_data))
				#Http.post(schoolip,post_data)

				if response.body is None:
					
					ret['code'] = -3
					ret['message'] = 'request local server failed'

					LOG.error('request local server failed [%s]' % schoolip)
					break
				
				ret['code'] = 0
				ret['message'] = 'success'

			else:
				ret['code'] = -1
				ret['message'] = 'secure key error'
				
				LOG.error('secure key error')
			
		self.write(json.dumps(ret))
		self.finish()

		LOG.info('PARA OUT[%s]' % ret)
		LOG.info('API OUT[%s]' % (self.__class__.__name__))