Browse Source

DB_URI configurabile

boyska 3 years ago
parent
commit
1925a89cb5
2 changed files with 22 additions and 9 deletions
  1. 7 3
      app.py
  2. 15 6
      parse_comment.php

+ 7 - 3
app.py

@@ -1,4 +1,4 @@
-import sqlite3
+import os
 from datetime import datetime
 
 from flask import Flask, redirect, render_template, request, url_for
@@ -9,7 +9,9 @@ from flask_sqlalchemy import SQLAlchemy
 from sqlalchemy.orm import relationship
 
 app = Flask(__name__)
-app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///news.db"
+app.config["SQLALCHEMY_DATABASE_URI"] = os.getenv(
+    "DB_URI", "sqlite:///news.db"
+)
 db = SQLAlchemy(app)
 
 
@@ -119,7 +121,9 @@ def search_by_month(year, month):
     pagination = Pagination(page=page, total=news.count(), per_page=50)
     news = news[first:last]
 
-    return render_template("search_results.html", results=news, pagination=pagination)
+    return render_template(
+        "search_results.html", results=news, pagination=pagination
+    )
 
 
 @app.route("/news/<int:nid>")

+ 15 - 6
parse_comment.php

@@ -51,9 +51,13 @@ function extract_metadata_from_html(string $html): array {
     if($date !== false) {
         $meta['published'] = strftime('%s', $date);
     }
-    $author = json_decode(pup_selector($html, 'td.titoloFTR .small strong json{}'))[0]->{'text'};
-    if($author !== '') {
-        $meta['author'] = $author;
+    $authors = json_decode(pup_selector($html, 'td.titoloFTR .small strong json{}'));
+    $meta['author'] = '';
+    if(count($authors) > 0) {
+        $author = $authors[0]->{'text'};
+        if($author !== '') {
+            $meta['author'] = $author;
+        }
     }
     return $meta;
 }
@@ -81,9 +85,13 @@ function extract_comments_from_html(string $html): array {
         if($date !== false) {
             $meta['published'] = strftime('%s', $date);
         }
-        $author = json_decode(pup_selector($comment, 'table td.titoloFTR .small strong json{}'))[0]->{'text'};
-        if($author !== '') {
-            $meta['author'] = $author;
+        $authors = json_decode(pup_selector($html, 'td.titoloFTR .small strong json{}'));
+        $meta['author'] = '';
+        if(count($authors) > 0) {
+            $author = $authors[0]->{'text'};
+            if($author !== '') {
+                $meta['author'] = $author;
+            }
         }
         array_push($comments, $meta);
     }
@@ -192,6 +200,7 @@ $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP5);
 $i = 0;
 $db->beginTransaction();
 while($f = fgets(STDIN)) {
+    echo $f;
     $f = str_replace("\n", '', $f);
     parse_save($db, $f, $parser);
     $i++;