#!/usr/bin/python3 #!/usr/bin/env python3 from http.server import BaseHTTPRequestHandler, HTTPServer import datetime from time import perf_counter import os import json import urllib.parse import configuration import socket import requests from userio import * # Early cache version import hashlib from pathlib import Path import newsParser server = None csvFilename = None connectionResults = None def navigationHeader(url): pageContent = "
\n" pageContent += "
\n" pageContent += " 0\n" pageContent += "
\n" opacityValue=3; if 7 <= datetime.datetime.now().hour and 20 >= datetime.datetime.now().hour: opacityValue=10; pageContent += " " pageContent += " "+str(opacityValue)+"" pageContent += " \n" if url is not None: pageContent += " link\n" else: pageContent += newsParser.supportedList() pageContent += "\n" return pageContent class MyServer(BaseHTTPRequestHandler): def log_message(self, format, *args): # To silence the default output of server (too verbose) return def do_GET(self): global csvFilename global csvFilenameTemp global connectionResults rootdir = 'pages/static/' csvFilename = rootdir + server['csv'] csvFilenameTemp = rootdir + server['csvTemp'] if not os.path.exists(rootdir + self.path) or self.path == '/index.html' or self.path == '/': if self.headers['X-Real-IP'] is None: say("From: "+self.client_address[0]+" GET Received : "+self.path) else: say("From: "+self.headers['X-Real-IP']+" GET Received : "+self.path) page=[] data_begin = "" with open(configuration.get_pageBegin(),'r') as fStart: data_begin += fStart.read().replace( 'CSTAPPNAME', server['name'] ) data_end = "" with open(configuration.get_pageEnd(),'r') as fEnd: data_end += fEnd.read().replace( 'CSTAPPNAME', server['name'] ).replace( 'CSTAPPVERSION', server['version'] ) s = self.path urlArgs = urllib.parse.parse_qs(s[2:]) url = None if "url" in urlArgs: url = urlArgs['url'][0] url = "http"+url.split("http")[1] url = url.split("?utm")[0] data_page = "" #Forging response if configuration.get_debug() != 0: data_page += "

Request: "+self.path+"

\n" data_page += ""+json.dumps(urlArgs)+"\n" #Form data_form = "
\n\
\n\ \n\ \n\
\n\
\n" data_page += data_form data_page += navigationHeader(url); data_page += "
\n" # Try cached version first if url is None: say("Root") else: urlHash=hashlib.md5(str.encode(url)) urlHashStr=str(urlHash.hexdigest()) cacheFilename = server['cachedir']+"/"+urlHashStr cacheFile = Path(cacheFilename) say("URL Hash: "+urlHashStr) cachedAlready=cacheFile.is_file() if 0 == configuration.isCacheEnabled(): say("Cache Disabled") cachedAlready=False if cachedAlready: # Import cached version say("Reading cache file: "+cacheFilename) txt = Path(cacheFilename).read_text() data_page += txt else: articleNew = newsParser.getArticle(url) data_page += articleNew # Write cache say("Creating cache file: "+cacheFilename) with open(cacheFilename, "w") as newCacheFile: newCacheFile.write(articleNew) newCacheFile.close() data_page += "
\n" page.append(data_begin) page.append(data_page) page.append(data_end) content = ''.join(page) self.send_response(200) self.send_header("Content-type", "text/html") self.send_header('Server',server['name']+" v"+server['version']) self.end_headers() self.wfile.write(content.encode('utf-8')) elif not os.path.exists(rootdir + self.path): self.send_header('Server',server['name']+" v"+server['version']) self.send_error(404, 'file not found') else: try: #say(self.path) f = open(rootdir + self.path,'rb') #open requested file self.send_response(200) if self.path.endswith('.css'): self.send_header('Content-type','text/css') elif self.path.endswith('.bmp'): self.send_header('Content-type','image/x-ms-bmp') elif self.path.endswith('.png'): self.send_header('Content-type','image/png') elif self.path.endswith('.jpg'): self.send_header('Content-type','image/jpeg') else: self.send_header('Content-type','text/html') self.send_header('Server',server['name']+" v"+server['version']) self.end_headers() self.wfile.write(f.read()) f.close() return except IOError: self.send_header('Server',server['name']+" v"+server['version']) self.send_error(404, 'file not found') if __name__ == "__main__": #global server server = configuration.get_server() say(server['name']+" v"+server['version']) webServer = HTTPServer((server['address'], server['port']), MyServer) say("Server started http://%s:%s" % (server['address'], server['port'])) # ~ newsParser.newsTheAtlantic.article("https://www.theatlantic.com/international/archive/2021/05/biden-israel-gaza-hamas-ceasefire/618949/") # ~ newsParser.newsLiberation.article("https://www.liberation.fr/politique/un-militaire-ca-se-presente-ou-ca-se-tait-selon-darmanin-20210510_4VGKW767PZCGVLDXY55BHC5CBU/") # ~ newsParser.newsSCMP.article("https://www.scmp.com/news/china/science/article/3140669/what-does-africa-need-make-more-coronavirus-vaccines?module=lead_hero_story_1&pgtype=homepage") # ~ newsParser.newsBFM.article("https://www.bfmtv.com/sante/rrisque-accru-de-developper-le-syndrome-de-guillain-barre-avec-le-vaccin-johnson-johnson_AN-202107120507.html") # ~ newsParser.newsFrandroidCom.article("https://www.frandroid.com/comment-faire/tutoriaux/986141_pass-sanitaire-europeen-comment-le-telecharger-pour-voyager-sereinement") # ~ newsParser.newsWaPo.article("https://www.washingtonpost.com/business/interactive/2021/pandora-papers-offshore-finance/") # ~ newsParser.newsWaPo.article("https://www.washingtonpost.com/technology/2021/05/14/pimeyes-facial-recognition-search-secrecy/") try: webServer.serve_forever() except KeyboardInterrupt: pass webServer.server_close() print("Server stopped.")