diff --git a/app.py b/app.py index 6316053..80eda5d 100644 --- a/app.py +++ b/app.py @@ -1,14 +1,28 @@ from flask import Flask,session,request,redirect,render_template - +import re app = Flask(__name__) +app.secret_key = 'this is secret_key you know ?' + +from views.page import page +from views.user import user +app.register_blueprint(page.pb) +app.register_blueprint(user.ub) @app.route('/') def hello_world(): # put application's code here return session.clear() +@app.before_request +def before_reuqest(): + pat = re.compile(r'^/static') + if re.search(pat,request.path):return + elif request.path == '/user/login' or request.path == '/user/register':return + elif session.get('username'):return + return redirect('/user/login') + @app.route('/') def catch_all(path): return render_template('404.html') if __name__ == '__main__': - app.run() \ No newline at end of file + app.run() diff --git a/wordCloudPicture.py b/wordCloudPicture.py new file mode 100644 index 0000000..23d1331 --- /dev/null +++ b/wordCloudPicture.py @@ -0,0 +1,47 @@ +import jieba +from wordcloud import WordCloud +import matplotlib.pyplot as plt +from PIL import Image,ImageDraw +from pymysql import * +import json +import numpy as np +def stopWordList(): + return [line.strip() for line in open('./model/stopWords.txt',encoding='utf8').readlines()] + +def get_img(field,tableName,targetImgSrc,resImgSrc): + con = connect(host='localhost',user='root',password='root',database='weiboarticles',port=3306,charset='utf8mb4') + cuser = con.cursor() + sql = f'select {field} from {tableName}' + cuser.execute(sql) + data = cuser.fetchall() + text = '' + for item in data: + text += item[0] + cuser.close() + con.close() + + cut = jieba.cut(text) + newCut = [] + for word in cut: + if word not in stopWordList():newCut.append(word) + string = ' '.join(newCut) + + img = Image.open(targetImgSrc) + img_arr = np.array(img) + wc = WordCloud( + background_color="#fff", + mask=img_arr, + font_path='STHUPO.TTF' + ) + wc.generate_from_text(string) + + fig = plt.figure(1) + plt.imshow(wc) + + plt.axis('off') + + plt.savefig(resImgSrc,dpi=500) + + +# get_img('content','comments','./static/comment.jpg','./static/commentCloud.jpg') +get_img('content','article','./static/content.jpg','./static/contentCloud.jpg')