David 10 лет назад
Родитель
Сommit
80a23e883e
5 измененных файлов с 113 добавлено и 26 удалено
  1. 24 1
      db.py
  2. 55 20
      front/index.html
  3. 22 2
      front/js/app.js
  4. 4 1
      news.py
  5. 8 2
      web.py

+ 24 - 1
db.py

@@ -1,5 +1,6 @@
 from pymongo import MongoClient,errors
 from bson.objectid import ObjectId
+import datetime
 import re
 
 class db():
@@ -17,9 +18,31 @@ class db():
         return el
 
 
+    def filter_articles(self, filter, limit=80):
+        db_filter={}
+        db_filter["matches"]=True
+        db_filter["scrap_date"]={}
+        if "maxdate" in filter and len(filter["maxdate"])>7:
+            db_filter["scrap_date"]["$lt"]=datetime.datetime.strptime(filter["maxdate"],"%Y-%m-%d")
+
+        if "mindate" in filter and len(filter["mindate"])>7:
+            db_filter["scrap_date"]["$gt"]=datetime.datetime.strptime(filter["mindate"],"%Y-%m-%d")
+
+        if db_filter["scrap_date"]=={}:
+            del db_filter["scrap_date"]
+
+
+
+        db_filter["weight"]={"$gt":int(filter["minweight"])}
+
+        print(db_filter)
+        #{'site': 'site', 'mindate': 'mind', 'maxddate': 'maxd', 'minweight': 'minwe'}
+
+        return [ self.jsonable(p) for p in self.c_articles.find(db_filter,{"html":0,"text":0}).limit(limit) ]
+
     def articles(self, id=None, limit=100):
         if id is None:
-            return [ self.jsonable(p) for p in self.c_articles.find().limit(limit) ]
+            return [ self.jsonable(p) for p in self.c_articles.find({"matches":True},{"html":0,"text":0}).limit(limit) ]
 
         ret=self.c_articles.find_one({"_id":ObjectId(id)})
         return self.jsonable(ret)

+ 55 - 20
front/index.html

@@ -6,6 +6,7 @@
 
 		<!-- Optional theme -->
 		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
+		<link rel="stylesheet" href="style.css">
 
 		<!-- Latest compiled and minified JavaScript -->
 		<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
@@ -16,39 +17,73 @@
 <body>
 	<div ng-controller="main">
 		<div class="row">
-			<div class="col-sm-2 col-md-2 col-lg-2 col-xl-2" style="min-height:100%;background-color:red">
+			<div class="col-sm-3 col-md-3 col-lg-3 col-xl-3">
+				<div class="row">
+					<div class="col-sm-4 col-md-4 col-lg-4">MinDate:</div>
+					<input class="col-sm-8 col-md-8 col-lg-8" ng-model="mindate" type="text"/>
+				</div>
+				<div class="row">
+					<div class="col-sm-4 col-md-4 col-lg-4">MaxDate:</div>
+					<input class="col-sm-8 col-md-8 col-lg-8" ng-model="maxdate" type="text"/>
+				</div>
+				<div class="row">
+					<div class="col-sm-4 col-md-4 col-lg-4">Site:</div>
+					<input class="col-sm-8 col-md-8 col-lg-8" ng-model="site" type="text"/>
+				</div>
+				<div class="row">
+					<div class="col-sm-4 col-md-4 col-lg-4">Keyword:</div>
+					<input class="col-sm-8 col-md-8 col-lg-8" ng-model="keyword" type="text"/>
+				</div>
+				<div class="row">
+					<div class="col-sm-4 col-md-4 col-lg-4">MinWeight:</div>
+					<input class="col-sm-8 col-md-8 col-lg-8" ng-model="minweight" type="text"/>
+				</div>
+				<div class="row">
+					<div class="col-md-4"></div>
+					<div class="col-md-8">
+						<button ng-click="filter()" class="btn btn-default">Filter</button>
+					</div>
+				</div>
+
 			</div>
-			<div class="col-sm-4 col-md-4 col-lg-4 col-xl-4" style="min-height:100%;background-color:blue">
-				<div class="row" style="background-color:white; border:1px solid black" ng-repeat="n in news">
+			<div class="col-sm-4 col-md-4 col-lg-4 col-xl-4">
+				<div class="row" style="border:1px solid black" ng-repeat="n in news">
 					<div class="col-md-12">
 						<div class="row">
-							<div class="col-md-7">
+							<div class="col-md-12">
+								{{n.title}}
 							</div>
-							<div class="col-md-5">
+						</div>
+						<div class="row" style="font-size:11px">
+							<div class="col-md-8">
 								{{n.scrap_date}}
 							</div>
+							<div class="col-md-4">
+								Weight: {{n.weight}}
+							</div>
 						</div>
-						<!--
-						source: {{n.source}}
-						-->
-						<div class="summary" style="font-size:11px">
+						<!-- source: {{n.source}} -->
+						<div class="summary" style="font-size:12px">
 							{{n.summary}}
 						</div>
-							<div class="row">
-								<div class="col-md-7">
-									<a ng-href="{{n.url}}">Visit site</a>
-								</div>
-								<div class="col-md-5">
-									<a ng-click="getArticle(n._id)">Read complete article</a>
-								</div>
+						<div class="row">
+							<div class="col-md-7">
+								<a ng-href="{{n.url}}">Visit site</a>
+							</div>
+							<div class="col-md-5">
+								<a ng-click="getArticle(n)">Read complete article</a>
 							</div>
-						Keyword(s): {{n.kw.join(", ")}}
+						</div>
+
+						<div class="row">
+							<div class="col-md-12 keywords">
+								Keyword(s): {{n.kw.join(", ")}}
+							</div>
+						</div>
 					</div>
 				</div>
 			</div>
-			<div class="col-sm-6 col-md-6 col-lg-6 col-xl-6" style="min-height:100%;background-color:green">
-
-			</div>
+			<div id="news" class="col-sm-5 col-md-5 col-lg-5 col-xl-5" ng-bind-html="newsArticle"></div>
 		</div>
 	</div>
 </body>

+ 22 - 2
front/js/app.js

@@ -1,7 +1,8 @@
 app=angular.module('app', []);
 
-app.controller('main', function($scope,$http) {
+app.controller('main', function($scope,$http,$sce) {
 	$scope.news=[];
+	$scope.newsArticle="";
 
 	$scope.getNews = function() {
 		$http.get("/pymnts/articles/").then(
@@ -19,7 +20,26 @@ app.controller('main', function($scope,$http) {
 			console.log("error");
 			console.log(data);
 		});
-	}
+	};
+	$scope.getArticle = function(el) {
+		$http.get("/pymnts/article/"+el._id).then(
+			function(data) {
+				$scope.newsArticle=$sce.trustAsHtml(data.data.articles.html);
+			},
+			function(data){
+			}
+		);
+	};
+	$scope.filter = function() {
+		$http.post("/pymnts/articles/",{"mindate":$scope.mindate,"maxdate":$scope.maxdate,"site":$scope.site,"keyword":$scope.keywod,"minweight":$scope.minweight}).then(
+			function(data) {
+				$scope.news=data.data.articles;
+			},
+			function(data) {
+			}
+		);
+	};
+
 	$scope.getNews();
 	/*
 

+ 4 - 1
news.py

@@ -21,6 +21,7 @@ class News():
     WEIGHT_SUM=0
     MATCHES=False
     MATCHING_KW=[]
+    TITLE=""
 
     def __init__(self,url,source,kw,lang="en"):
         url=sanitizeUrl(url)
@@ -44,6 +45,7 @@ class News():
         self.PUBLISH_DATE=a.publish_date
         self.SUMMARY=a.summary
         self.TEXT=a.text
+        self.TITLE=a.title
         self.HTML=htmlmin.minify(a.article_html,remove_empty_space=True)
         lowertext=self.TEXT.lower()
         for k in kw:
@@ -66,7 +68,8 @@ class News():
                 "matches": self.MATCHES,
                 "weight": self.WEIGHT_SUM,
                 "kw": self.MATCHING_KW,
-                "html": self.HTML 
+                "html": self.HTML, 
+                "title": self.TITLE
                 }
 
     def dedup(self,val):

+ 8 - 2
web.py

@@ -1,7 +1,8 @@
 #!/usr/bin/env python3
 
-from flask import Flask,abort,jsonify
+from flask import Flask,abort,jsonify,request
 from db import db
+import json
 
 app = Flask(__name__)
 d = db()
@@ -12,10 +13,15 @@ BASEPATH="/pymnts"
 def index():
     return "Hello, World!"
 
+@app.route(BASEPATH+'/articles/', methods=['POST'])
+@app.route(BASEPATH+'/articles', methods=['POST'])
+def filter_articles():
+    return jsonify({'articles': d.filter_articles(request.get_json(),limit=80)}) #limit
+
 @app.route(BASEPATH+'/articles/', methods=['GET'])
 @app.route(BASEPATH+'/articles', methods=['GET'])
 def get_articles():
-    return jsonify({'articles': d.articles(limit=100)}) #limit
+    return jsonify({'articles': d.articles(limit=80)}) #limit
 
 @app.route(BASEPATH+'/article/<string:art_id>', methods=['GET'])
 def get_article(art_id):