Ejemplo n.º 1
0
    def __init__(self):
        logging.basicConfig(level = logging.DEBUG,
                            format = '%(levelname)s %(asctime)s (%(filename)s:%(funcName)s():%(lineno)d) %(message)s')
        settings = {
            "static_path": os.path.join(os.path.dirname(__file__), "static"),
        }
        logging.info(settings)
        handlers = [
            (r'/', MainHandler),
            (r'/admin', AdminHandler),
            (r'/update', UpdateHandler),
            (r'/export', ExportHandler),
            (r'/static/', tornado.web.StaticFileHandler, dict(path=settings['static_path']))
        ]
        tornado.web.Application.__init__(self, handlers, **settings)

        with open(FLAGS.config_file, "r") as fid:
            self.config = json.load(fid)
        self.db_config = self.config["db"]
        # wait mysql to start service
        logging.info('waiting mysql be full ready, 20s')
        time.sleep(20)
        db = dbhelper.DbHelper(**self.db_config)
        if not db.table_exist():
            db.create_table()
        # update index
        for dirname in self.config["dir"]:
            update = self.config["dir"][dirname]
            if update:
                all_wav_files = glob.glob('%s/*.wav' % dirname)
                for i, wav in enumerate(all_wav_files):
                    db.insert(wav, 'false', 'false')
                    if i % 50 == 0:
                        logging.info('creating index %s %d', dirname, i)
Ejemplo n.º 2
0
 def get(self):
     text = self.get_argument('input', '', strip=True)
     filed = None
     keyword = ''
     arr = text.split(':')
     if len(arr) == 2:
         filed = arr[0].strip()
         keyword = arr[1].strip()
     else:
         keyword = text.strip()
     page = int(self.get_argument('page', 0, strip=True))
     page_items = int(self.get_argument('page_items', 10, strip=True))
     db = dbhelper.DbHelper(**self.application.db_config)
     count = db.find_count(keyword, filed)
     num_pages = int((count - 1) / page_items) + 1
     if page < 0: page = 0
     if page >= num_pages: page = num_pages - 1
     start_page = page - 5
     if start_page < 0: start_page = 0
     end_page = page + 5
     if end_page > num_pages: end_page = num_pages - 1
     history = db.find(keyword, filed)
     self.render("static/history.html",
                 history=history,
                 page_items=page_items,
                 page=page,
                 num_pages=num_pages,
                 start_page=start_page,
                 end_page=end_page)
Ejemplo n.º 3
0
 def init_db(self):
     self.use_db = self.config["use_db"]
     if self.use_db:
         self.db_config = self.config["db"]
         db = dbhelper.DbHelper(**self.db_config)
         if not db.asr_table_exist():
             db.create_asr_table()
Ejemplo n.º 4
0
 def get(self):
     db = dbhelper.DbHelper(**self.application.db_config)
     review_count = db.get_review_count()
     items = db.get_all_review(0, review_count)
     self.set_header('Content-Type','text/plain')
     self.set_header('content-Disposition','attachment; filename=export.txt')  
     for item in items:
         if item[2] == 1 and item[3] == 1:
             self.write(item[1] + '\n')
             yield self.flush()
Ejemplo n.º 5
0
 def on_close(self):
     time_str = str(int(time.time()))
     m = hashlib.md5()
     m.update(time_str.encode('utf8'))
     wav_name = time_str + '_' + m.hexdigest()[:10] + '.wav'
     wav_path = self.application.wav_dir + '/' + wav_name
     self.write_wav_file(wav_path, self.wav_data)
     all_result = '\n'.join(self.results)
     if self.application.use_db:
         db = dbhelper.DbHelper(**self.application.db_config)
         db.insert(wav_path, all_result, self.client_info)
Ejemplo n.º 6
0
    def get(self):
        page = int(self.get_argument('page', 0, strip=True))
        page_items = int(self.get_argument('page_items', 10, strip=True))
        db = dbhelper.DbHelper(**self.application.db_config)
        history_count = db.get_history_count()
        num_pages = int((history_count - 1) / page_items) + 1
        if page < 0: page = 0
        if page >= num_pages: page = num_pages - 1
        start_page = page - 5
        if start_page < 0: start_page = 0
        end_page = page + 5
        if end_page > num_pages: end_page = num_pages - 1

        history = db.get_all_history(page * page_items, page_items)
        self.render("static/history.html",
                    history=history,
                    page_items=page_items,
                    page=page,
                    num_pages=num_pages,
                    start_page=start_page,
                    end_page=end_page)
Ejemplo n.º 7
0
    def get(self):
        page = int(self.get_argument('page', 0, strip=True))
        page_items = int(self.get_argument('page_items', 10, strip=True))
        db = dbhelper.DbHelper(**self.application.db_config)
        review_count = db.get_review_count()
        num_pages = int((review_count - 1) / page_items) + 1;
        if page < 0: page = 0
        if page >= num_pages: page = num_pages - 1
        start_page = page - 5
        if start_page < 0: start_page = 0
        end_page = page + 5
        if end_page > num_pages: end_page = num_pages - 1

        items = db.get_all_review(page * page_items, page_items)
        self.render("static/admin.html", items = items,
                                         page_items = page_items,
                                         page = page,
                                         num_pages = num_pages,
                                         start_page = start_page,
                                         end_page = end_page,
                                         total = review_count)
Ejemplo n.º 8
0
 def get(self):
     id = int(self.get_argument('id', 0, strip=True));
     operation = self.get_argument('operation', 'true', strip=True);
     db = dbhelper.DbHelper(**self.application.db_config)
     db.update(id, operation, 'true')
)
cnx.commit()
cnx.close()

# The trafikverkt api
url = "https://api.trafikinfo.trafikverket.se/v1/data.json"

# @ToDo i am looking for specific site. Hard code it now. Move it as program arg later. Compose header and body

flow = tverketheader.TrafficFlowCompose(regionid=4, county=1, site=914)
data_to_send = flow.compose_body()
headers = flow.compose_header()

# Run this in an infinite loop and poll every hour
while True:

    # Send HTTP POST request and get response
    response = requests.post(url, data=data_to_send, headers=headers)
    json_response = response.json()
    print(json_response)
    # Frame response object
    response = tverketresponse.TrafficFlowResponse(json_response)

    # push to db
    dbobj = dbhelper.DbHelper(response)
    dbobj.push_data()
    # Sometimes data may not be updated for long time which will cause redundant entries.
    dbobj.remove_duplicates()
    # sleep
    time.sleep(60)