|
| 1 | +from typing import Any, Optional, Tuple, List, Dict |
| 2 | +import traceback |
| 3 | +import time |
| 4 | + |
| 5 | +import requests |
| 6 | +from bs4 import BeautifulSoup, NavigableString |
| 7 | +from flask import request |
| 8 | + |
| 9 | +from src.helpers.get_version import get_poetry_version |
| 10 | + |
| 11 | +from src.models.exceptions import MissingInformationError, WikipediaApiFetchError |
| 12 | +from src.models.wikimedia.enums import RequestMethods |
| 13 | +from src.views.v2.statistics import StatisticsViewV2 |
| 14 | + |
| 15 | +from src.models.v2.job.insights_tarb_job_v2 import InsightsTarbJobV2 |
| 16 | +from src.models.v2.schema.insights_tarb_schema_v2 import InsightsTarbSchemaV2 |
| 17 | + |
| 18 | + |
| 19 | +class InsightsTarbV2(StatisticsViewV2): |
| 20 | + |
| 21 | + """ |
| 22 | + returns IABot statistical data |
| 23 | + """ |
| 24 | + |
| 25 | + schema = InsightsTarbSchemaV2() # Defines expected parameters; Overrides StatisticsViewV2's "schema" property |
| 26 | + job: InsightsTarbJobV2 # Holds usable variables, seeded from schema. Overrides StatisticsViewV2's "job" |
| 27 | + |
| 28 | + return_data: Dict[str, Any] = {} # holds parsed data from data processing |
| 29 | + execution_errors: List[Dict[str, Any]] = None |
| 30 | + |
| 31 | + def get(self): |
| 32 | + """ |
| 33 | + flask entrypoint for GET |
| 34 | + must return a tuple: (Any, response_code) |
| 35 | + """ |
| 36 | + from src import app |
| 37 | + app.logger.debug(f"==> InsightsTarbV2::get") |
| 38 | + |
| 39 | + return self.__process_request__(method=RequestMethods.get) |
| 40 | + |
| 41 | + |
| 42 | + def __process_request__(self, method=RequestMethods.post): # default to POST method |
| 43 | + |
| 44 | + from src import app |
| 45 | + app.logger.debug(f"==> InsightsTarbV2::__process_request__, method = {method}") |
| 46 | + |
| 47 | + # Start the timer |
| 48 | + start_time = time.time() |
| 49 | + |
| 50 | + # fetch the insight data |
| 51 | + try: |
| 52 | + |
| 53 | + # validate and setup params |
| 54 | + self.__validate_and_get_job__(method) # inherited/subclassed from StatisticsViewV2 |
| 55 | + |
| 56 | + # fetch the data, parse and return summary |
| 57 | + insight_data = self.__get_insight_data__() |
| 58 | + |
| 59 | + # Stop the timer and calculate execution time |
| 60 | + end_time = time.time() |
| 61 | + execution_time = end_time - start_time |
| 62 | + |
| 63 | + self.return_data = { |
| 64 | + "iari_version": get_poetry_version("pyproject.toml"), |
| 65 | + "iari_command": "tarb_insights", |
| 66 | + "endpoint": request.endpoint, |
| 67 | + "execution_time": f"{execution_time:.4f} seconds", |
| 68 | + "execution_errors": self.execution_errors, |
| 69 | + } |
| 70 | + |
| 71 | + self.return_data.update(insight_data) |
| 72 | + |
| 73 | + return self.return_data, 200 |
| 74 | + |
| 75 | + |
| 76 | + except MissingInformationError as e: |
| 77 | + traceback.print_exc() |
| 78 | + return {"error": f"Missing Information Error: {str(e)}"}, 500 |
| 79 | + |
| 80 | + except Exception as e: |
| 81 | + traceback.print_exc() |
| 82 | + return {"error": f"General Error: {str(e)}"}, 500 |
| 83 | + |
| 84 | + |
| 85 | + def __get_insight_data__(self): |
| 86 | + """ |
| 87 | + grabs appropriate data regarding media updates |
| 88 | + """ |
| 89 | + |
| 90 | + # soup = self.__get_stats_soup__() |
| 91 | + # |
| 92 | + # table_names = self.__get_table_names__(soup) |
| 93 | + # table_list = self.__get_all_tables__(soup, table_names) |
| 94 | + # table_totals = self.__get_table_totals__(table_list) |
| 95 | + # |
| 96 | + # return { |
| 97 | + # "table_names": table_names, |
| 98 | + # "table_totals": table_totals, |
| 99 | + # "tables": table_list |
| 100 | + # } |
| 101 | + |
| 102 | + |
| 103 | + """ |
| 104 | + format of each returned record: |
| 105 | + |
| 106 | + { |
| 107 | + "Wiki": "afwiki", |
| 108 | + "Timestamp": "2021-08-14 00:00:00", |
| 109 | + "TotalEdits": 2709, |
| 110 | + "TotalLinks": 3543, |
| 111 | + "ReactiveEdits": 1620, |
| 112 | + "ProactiveEdits": 90, |
| 113 | + "DeadEdits": 278, |
| 114 | + "UnknownEdits": 721, |
| 115 | + "LiveLinks": 163, |
| 116 | + "DeadLinks": 2640, |
| 117 | + "TagLinks": 740, |
| 118 | + "UnknownLinks": 0 |
| 119 | + }, |
| 120 | + """ |
| 121 | + |
| 122 | + tarb_api_url = "https://iabot.wmcloud.org/api.php?action=statistics&format=flat" |
| 123 | + |
| 124 | + r = requests.get(url_stats_yearly) |
| 125 | + # TODO use a try/catch here |
| 126 | + |
| 127 | + if r.status_code != 200: |
| 128 | + return None |
| 129 | + |
| 130 | + return { |
| 131 | + "test_value_1": 1, |
| 132 | + "test_value_2": 2, |
| 133 | + "test_value_3": 3, |
| 134 | + } |
| 135 | + |
| 136 | + |
0 commit comments