Skip to content

Commit

Permalink
Added background thread support
Browse files Browse the repository at this point in the history
  • Loading branch information
udaya2899 committed Jul 27, 2020
1 parent 6da4fd8 commit e61fce3
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import threading

from flask import Flask
from flask import request
from flask import jsonify

from bs4 import BeautifulSoup
Expand All @@ -26,12 +25,18 @@
5: "total_confirmed"
}

# Initialisations
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)
app = Flask(__name__)

# config
FETCH_INTERVAL = 1800

# global response variable, to be overwritten later
last_extracted_content = {"data": "fetching.. please try again in a minute"}

# scrapes table from the given url
def get_table_from_web():
url = "http://mohfw.gov.in"
page = requests.get(url)
Expand All @@ -40,6 +45,8 @@ def get_table_from_web():
table = div.find('table', class_='table')
return table

# When provided with rows of a table, returns state_data, after cleaning
# Assumed that state data will be for first 35 rows in body of table
def get_state_wise_data(rows):
rows = rows[:36]
state_data = []
Expand All @@ -53,6 +60,7 @@ def get_state_wise_data(rows):
state_data.append(items)
return state_data

# When provided with rows of a table, returns total_data, assuming that 36th row is total_data
def get_total_data(rows):
total = rows[36].find_all("strong")
total_data = {}
Expand All @@ -61,6 +69,7 @@ def get_total_data(rows):
total_data[headers[index]] = total[index-1].text
return total_data

# get_data combines all the information given extracted table content
def get_data(content, time, indent=None):
rows = content.find_all("tr")

Expand All @@ -78,7 +87,7 @@ def get_data(content, time, indent=None):

return response


# parent function that calls the scraping function and get_data function
def data_extract():
table = get_table_from_web()
logging.info("Table fetched. \n Fetching state wise data from table...\n")
Expand All @@ -105,7 +114,7 @@ def api():
return jsonify(last_extracted_content)

def start_thread():
threading.Timer(10, data_extract, ()).start()
threading.Timer(FETCH_INTERVAL, data_extract, ()).start()

if __name__ == "__main__":
logging.info("****** COVID-INDIA-API *******")
Expand Down

0 comments on commit e61fce3

Please sign in to comment.