Prechádzať zdrojové kódy

wp-xml parser: complete, I th ink

Martin 7 rokov pred
rodič
commit
ce520c49ca
1 zmenil súbory, kde vykonal 27 pridanie a 16 odobranie
  1. 27 16
      main.py

+ 27 - 16
main.py

@@ -16,12 +16,21 @@ _HTML_HEAD = '''
         .wrapper {
             width: 800px;
             margin: auto;
+            font-size: larger;
+            padding-bottom: 100px;
         }
         img {
             display: block;
             max-width: 100%;
             height: auto;
         }
+        table {
+            font-size: large;
+        }
+        td {
+            padding-left: 20px;
+            padding-top: 3px;
+        }
     </style>
 </head>
 '''
@@ -71,7 +80,6 @@ def _item(node):
     return data
 
 def _download(dest_path, url):
-    print('Downloading %s...' % url)
 
     extension = url.split('.')[-1]
     url_hash = hashlib.md5(url.encode('utf-8')).hexdigest()
@@ -79,11 +87,10 @@ def _download(dest_path, url):
     dest = os.path.join(dest_path, 'posts', 'files', dest_fname)
 
     if os.path.exists(dest):
-        print('already exists.')
+        # print('already exists.')
         return dest_fname
 
-    # return dest_fname
-
+    print('Downloading %s...' % url)
     resp = requests.get(url)
     with open(dest, 'wb') as f:
         f.write(resp.content)
@@ -95,6 +102,7 @@ def _write_files(dest_path, posts):
         f.write('<html>\n')
         f.write(_HTML_HEAD)
         f.write('<body>')
+        f.write('<div class="wrapper">')
         f.write('<table>')
         for post in posts:
             f.write('<tr>')
@@ -102,6 +110,7 @@ def _write_files(dest_path, posts):
             f.write('  <td><a href="posts/%s.html">%s</a></td>\n' % (post['id'], post['title']))
             f.write('</tr>\n')
         f.write('</table>')
+        f.write('</div>')
         f.write('</body></html>')
 
     for post in posts:
@@ -109,17 +118,22 @@ def _write_files(dest_path, posts):
             f.write('<html>\n')
             f.write(_HTML_HEAD)
             f.write('<body>')
-            f.write('<a href="../index.html">&lt;--- tillbaka</a><hr>')
             f.write('<div class="wrapper">')
+            f.write('<a href="../index.html">&lt;&lt;&lt; Tillbaka</a>')
+            if post['comments']:
+                f.write(' | <a href="#comments">Kommentarer</a>')
+            f.write('<hr>')
             f.write('<h1>%s: %s</h1>\n' % (post['date'], post['title']))
             f.write(post['content_html'] or '')
 
             if post['comments']:
                 f.write('<hr>')
+                f.write('<div id="comments">')
                 f.write('<h1>Kommentarer</h1>')
                 for comment in post['comments']:
                     f.write('<h2>%s: %s</h2>' % (comment['date'], comment['author']))
-                    f.write(comment['content'].replace('\n', '\n<br>'))
+                    f.write('<p>' + comment['content'].replace('\n', '</p><p>') + '</p>')
+                f.write('</div>')
 
             f.write('</div>')
             f.write('</body></html>')
@@ -139,9 +153,6 @@ def main(dest_path, xml_files):
                     continue
 
                 data = _item(item)
-
-
-
                 if data['status'] not in ['publish', 'inherit']:
                     continue
 
@@ -152,17 +163,20 @@ def main(dest_path, xml_files):
 
     posts.sort(key=lambda x: x['date'])
 
-    posts = [p for p in posts if p['id'] == '2481127cff605a6239722c80efd67f3c']
-
     p1 = re.compile(r'(\[caption.*?\])?<a href="(http[^>]*?)"><img[^>]*? src="(http[^>]*?)"[^>]*?></a>([^<]*?\[/caption\])?')
     p3 = re.compile(r'(\[caption.*?\])?<img[^>]*? src="(http[^>]*?)"[^>]*?>([^<]*?\[/caption\])?')
-    p4 = re.compile(r'\[gallery ids="(.*)"\]')
+    p4 = re.compile(r'\[gallery.*?ids="(.*?)"\]')
 
     drop_patterns = {
-        # re.compile(r'\[gallery ids.*?\]'),
+        re.compile(r'<span .*?>'),
     }
 
     for post in posts:
+        
+        if '<p' not in post['content_html']:
+            post['content_html'] = '<p>' + post['content_html'].replace('\n', '</p><p>') + '</p>'
+            
+
         for p in drop_patterns:
             for match in re.finditer(p, post['content_html']):
                 post['content_html'] = post['content_html'].replace(match.group(0), '')
@@ -201,9 +215,6 @@ def main(dest_path, xml_files):
 
             post['content_html'] = post['content_html'].replace(match.group(0), '\n'.join(body))
 
-
-            print(ids)
-
     _write_files(dest_path, posts)
 
 if __name__ == '__main__':