Beispiel #1
0
def addCol():
    column = request.args.get('newCol')
    if column:
        #g_all_fields.append(column)
        add_col_sql = ADD_COLUMN_SQL % (session['tables'][0], column)
        with DbHandler(DB_NAME) as db:
            db.execute_sql(add_col_sql)
    return redirect(url_for('index'))
Beispiel #2
0
def userConfig():
    newPword = request.form["newPword"]
    if newPword:
        with DbHandler(DB_NAME) as db:
            curUser = session.get('user')
            update_sql = UPDATE_USER_PASSWORD_SQL % (newPword, curUser)
            db.execute_sql(update_sql)
        return redirect(url_for('logout'))
    return redirect(url_for('index'))
def show_image():
    field = list(request.args.values())[0]
    with DbHandler(DB_NAME) as db:
        df = pd.read_sql(
            "select {field} from {table}".format(field=field,
                                                 table=session.get(
                                                     'tables', [])[0]),
            db.conn)
        counts = counts_max(df[field], 10)
        print_pie(counts, field)

    return redirect(url_for("images"))
def output():
    tables = session.get('tables', [])
    coding = list(request.args.values())[0]
    print("this is output process", tables)
    print("this is choosing the encoding format:", coding)
    with DbHandler(DB_NAME) as db:
        pd_csv = pd.read_sql("select * from {table}".format(table=tables[0]),
                             db.conn)
        pd_csv.to_csv("{}.csv".format(tables[0]), encoding=coding)
    filename = "{}.csv".format(tables[0])
    if os.path.exists(filename):
        print("this is output**********")
        return send_from_directory(r'./', filename, as_attachment=True)
Beispiel #5
0
from dbHandler import DbHandler
from model import *

v = Vertex(12, 12, 12, 0)
h = DbHandler('sqlite+pysqlite:///database.db')
h.add(v)
h.commit()
print(h.vertices)
Beispiel #6
0
    def run(self):
        with self.threadingSum:
            logging.debug("%s start" % self.url)
            dbHandler = DbHandler()
            if not dbHandler.hasQuestion(self.url):
                # 插入新的问题
                question = Question(self.url)
                title = question.get_title()
                detail = question.get_detail()
                answerNum = question.get_answer_num()
                followersNum = question.get_followers_num()
                tags = ""
                for tag in question.get_tags():
                    tags += tag + ";"
                tags = tags[0: len(tags) - 1]
                questionDict = {"url": self.url, "title": title, 
                                "detail": detail, "followers": followersNum, 
                                "answerNum": answerNum, "tags": tags}
                dbHandler.insertNewQuestion(questionDict)

                zh_qid = dbHandler.getQueIdByUrl(self.url)
                # 插入新的答案
                for answer_link in question.get_all_answer_link():

                    answer = Answer(answer_link)
                    author = answer.get_author()
                    votes = answer.get_votes()
                    answerDict = {"url": answer_link, "author": author, "zh_qid": zh_qid, 
                              "votes": votes}
                    dbHandler.insertNewAnswer(answerDict)

                    # 插入图片地址
                    zh_aid = dbHandler.getAnsIdByUrl(answer_link)

                    for imgUrl in answer.get_all_pics():
                        dbHandler.insertNewImgUrl(zh_aid, imgUrl)

                    contents = answer.get_answer_content()
                    self.storeTheAnswer(zh_aid, contents)

                dbHandler.close()

            logging.debug("%s done" % self.url)
Beispiel #7
0
 def __init__(self):
     self.dbHandler = DbHandler()
Beispiel #8
0
class ToHtml:

    def __init__(self):
        self.dbHandler = DbHandler()

    def createHtml(self):

        html_tpl = u'''<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN">
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="provider" content="www.cnepub.com"/>
        <meta name="builder" content="epubBuilder present by www.cnepub.com"/>
        <meta name="right" content="该文档由epubBuilder生成。epubBuilder为掌上书苑(www.cnepub.com)提供的epub制作工具,仅供个人交流与学习使用。在未获得掌上书苑的商业授权前,不得用于任何商业用途。"/>
        <link rel="stylesheet" type="text/css" href="css/main.css"/>
        <title>%(title)s</title>
        </head>
        <body>
        <center>
            <h3>%(title)s</h3>
        </center>
        <hr>
        <div class="band">
            %(band)s
        </div>
        <div class="answer-content">
            %(content)s
        </div>
        </body>
        '''

        questions = self.dbHandler.getQuestions()
        utils     = Utils()

        for question in questions:
            zh_qid    = question["zh_qid"]
            q_url     = question["url"]
            title     = question["title"]
            detail    = question["detail"]
            tags      = question["tags"]
            followers = question["followers"]
            answerNum = question["answerNum"]

            band = ""
            
            band += '<div class = "tags">\n'
            for tag in tags.split(";"):
                band += '<span>' + tag + '</span>\n'
            band += '</div>\n'
            band += '<div class = "info"><span><strong>%d</strong>个回答</span><span><strong>%d</strong>人关注该问题</span></div>\n' % (answerNum, followers)
            band += '<div class = "detail">' + detail + '</div>\n'
            band += '<div class = "url">原问题网址:<a href="' + q_url + '"><h4>' + q_url + '</h4></a></div>\n'
            
            contents = ""
            answers  = self.dbHandler.getAnswersByQid(zh_qid)
            for answer in answers:
                zh_aid = answer["zh_aid"]
                author = answer["author"]
                votes  = answer["votes"]
                contents += '<div class = "content">'
                contents += '<span id = "author">%s</span>\n' % author
                contents += '<span id = "votes">赞同: %s</span>\n' % votes

                tmp_txt_dir = utils.get("tmp_answer_content_dir")
                with open(os.getcwd() + tmp_txt_dir + "%s.txt" % (zh_aid), 'r') as txt:
                    contents += str(txt.read())
                    contents += "\n"
                
                contents += '</div>'

            tmp_html_dir = utils.get("tmp_answer_content_dir") + 'html'
            print tmp_html_dir

            html         = open(os.getcwd() + tmp_html_dir + "/" + title + ".html", 'w')
            html.write(html_tpl % {'title': title, 'band': band, 'content': contents})
Beispiel #9
0
 def __init__(self, threadingSum, url):
     threading.Thread.__init__(self)
     self.dbHandler = DbHandler()
     self.url = url
     self.threadingSum = threadingSum
Beispiel #10
0
        with self.threadingSum:
            logging.debug("%s start!" % self.url)
            pic_name = self.url.split("/")[-1]
            request = requests.get(self.url, stream=True, timeout=10)
            with open(os.getcwd() + "/tmp/html/images/" + pic_name,
                      'wb') as fd:
                for chunk in request.iter_content():
                    fd.write(chunk)
            logging.debug("%s done!" % self.url)


if __name__ == '__main__':
    #设置线程数
    threadingSum = threading.Semaphore(20)

    dbHandler = DbHandler()

    question = Question("http://www.zhihu.com/question/26702926")

    answer = Answer("http://www.zhihu.com/question/26702926/answer/33843851")
    img_urls = answer.get_all_pics()

    for url in img_urls:
        t = DownloadImg(threadingSum, url)
        t.start()

    for t in threading.enumerate():
        if t is threading.currentThread():
            continue
        t.join()