Spaces:
Running
Running
:hell:
Browse files
app.py
CHANGED
|
@@ -18,61 +18,61 @@ def get_nation_issues(NATION, X_PASSWORD):
|
|
| 18 |
"q": "issues"
|
| 19 |
}
|
| 20 |
response = requests.get(url, headers=headers, params=params)
|
| 21 |
-
return response.text
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
Fetches and reformats issues from the NationStates API.
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
|
| 33 |
-
Returns:
|
| 34 |
-
dict: A dictionary containing the nation ID and a list of issue details.
|
| 35 |
-
"""
|
| 36 |
|
| 37 |
-
# Fetch raw SML/XML data
|
| 38 |
-
sml_data = get_nation_issues(nation, password)
|
| 39 |
-
|
| 40 |
-
# Parse the SML/XML
|
| 41 |
-
soup = BeautifulSoup(sml_data, "xml")
|
| 42 |
-
|
| 43 |
-
# Extract nation info
|
| 44 |
-
nation_elem = soup.find("NATION")
|
| 45 |
-
nation_id = nation_elem.get("id") if nation_elem else "Unknown"
|
| 46 |
-
|
| 47 |
-
# List to store issues
|
| 48 |
-
issues_list = []
|
| 49 |
-
|
| 50 |
-
# Extract issues
|
| 51 |
-
if nation_elem:
|
| 52 |
-
for issue in nation_elem.find_all("ISSUE"):
|
| 53 |
-
issue_data = {
|
| 54 |
-
"issue_id": issue.get("id", "Unknown"),
|
| 55 |
-
"title": issue.TITLE.get_text() if issue.TITLE else "No title available",
|
| 56 |
-
"text": issue.TEXT.get_text() if issue.TEXT else "No text available",
|
| 57 |
-
"author": issue.AUTHOR.get_text() if issue.AUTHOR else "Unknown author",
|
| 58 |
-
"editor": issue.EDITOR.get_text() if issue.EDITOR else "Unknown editor",
|
| 59 |
-
"pic1": issue.PIC1.get_text() if issue.PIC1 else "No image 1",
|
| 60 |
-
"pic2": issue.PIC2.get_text() if issue.PIC2 else "No image 2",
|
| 61 |
-
"options": []
|
| 62 |
-
}
|
| 63 |
-
|
| 64 |
-
# Extract options within the issue
|
| 65 |
-
for option in issue.find_all("OPTION"):
|
| 66 |
-
option_data = {
|
| 67 |
-
"option_id": option.get("id", "Unknown"),
|
| 68 |
-
"text": option.get_text() if option else "No option text"
|
| 69 |
-
}
|
| 70 |
-
issue_data["options"].append(option_data)
|
| 71 |
|
| 72 |
-
# Add issue data to list
|
| 73 |
-
issues_list.append(issue_data)
|
| 74 |
|
| 75 |
-
return {"nation_id": nation_id, "issues": issues_list}
|
| 76 |
def address_nation_issues(NATION, X_PASSWORD, issue_id, option_id):
|
| 77 |
"""
|
| 78 |
Address a specific issue for a nation in NationStates API.
|
|
@@ -103,12 +103,65 @@ def address_nation_issues(NATION, X_PASSWORD, issue_id, option_id):
|
|
| 103 |
|
| 104 |
response = requests.post(url, headers=headers, data=data)
|
| 105 |
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
# Create the Gradio interface
|
| 110 |
-
|
| 111 |
-
fn=
|
| 112 |
inputs=[
|
| 113 |
gr.Textbox(label="Nation Name", placeholder="Enter the nation name"),
|
| 114 |
gr.Textbox(label="API Password", placeholder="Enter your API password"),
|
|
@@ -132,7 +185,7 @@ address_nation_issues_interface = gr.Interface(
|
|
| 132 |
api_name="address_nation_issues",
|
| 133 |
)
|
| 134 |
demo = gr.TabbedInterface(
|
| 135 |
-
interface_list=[
|
| 136 |
tab_names=["Get Nation Issues JSON", "Address Nation Issues"])
|
| 137 |
# Launch the interface and MCP server
|
| 138 |
if __name__ == "__main__":
|
|
|
|
| 18 |
"q": "issues"
|
| 19 |
}
|
| 20 |
response = requests.get(url, headers=headers, params=params)
|
|
|
|
| 21 |
|
| 22 |
+
def get_nation_issues_JSON(sml_data):
|
| 23 |
+
"""
|
| 24 |
+
Fetches and reformats issues from the NationStates API.
|
| 25 |
+
|
| 26 |
+
Args:
|
| 27 |
+
nation (str): The nation name.
|
| 28 |
+
password (str): The API password.
|
| 29 |
+
|
| 30 |
+
Returns:
|
| 31 |
+
dict: A dictionary containing the nation ID and a list of issue details.
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
# Parse the SML/XML
|
| 35 |
+
soup = BeautifulSoup(sml_data, "xml")
|
| 36 |
+
|
| 37 |
+
# Extract nation info
|
| 38 |
+
nation_elem = soup.find("NATION")
|
| 39 |
+
nation_id = nation_elem.get("id") if nation_elem else "Unknown"
|
| 40 |
+
|
| 41 |
+
# List to store issues
|
| 42 |
+
issues_list = []
|
| 43 |
+
|
| 44 |
+
# Extract issues
|
| 45 |
+
if nation_elem:
|
| 46 |
+
for issue in nation_elem.find_all("ISSUE"):
|
| 47 |
+
issue_data = {
|
| 48 |
+
"issue_id": issue.get("id", "Unknown"),
|
| 49 |
+
"title": issue.TITLE.get_text() if issue.TITLE else "No title available",
|
| 50 |
+
"text": issue.TEXT.get_text() if issue.TEXT else "No text available",
|
| 51 |
+
"author": issue.AUTHOR.get_text() if issue.AUTHOR else "Unknown author",
|
| 52 |
+
"editor": issue.EDITOR.get_text() if issue.EDITOR else "Unknown editor",
|
| 53 |
+
"pic1": issue.PIC1.get_text() if issue.PIC1 else "No image 1",
|
| 54 |
+
"pic2": issue.PIC2.get_text() if issue.PIC2 else "No image 2",
|
| 55 |
+
"options": []
|
| 56 |
+
}
|
| 57 |
|
| 58 |
+
# Extract options within the issue
|
| 59 |
+
for option in issue.find_all("OPTION"):
|
| 60 |
+
option_data = {
|
| 61 |
+
"option_id": option.get("id", "Unknown"),
|
| 62 |
+
"text": option.get_text() if option else "No option text"
|
| 63 |
+
}
|
| 64 |
+
issue_data["options"].append(option_data)
|
| 65 |
|
| 66 |
+
# Add issue data to list
|
| 67 |
+
issues_list.append(issue_data)
|
|
|
|
| 68 |
|
| 69 |
+
return {"nation_id": nation_id, "issues": issues_list}
|
| 70 |
+
|
| 71 |
+
return get_nation_issues_JSON(response.text)
|
| 72 |
|
|
|
|
|
|
|
|
|
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
|
|
|
|
|
|
| 75 |
|
|
|
|
| 76 |
def address_nation_issues(NATION, X_PASSWORD, issue_id, option_id):
|
| 77 |
"""
|
| 78 |
Address a specific issue for a nation in NationStates API.
|
|
|
|
| 103 |
|
| 104 |
response = requests.post(url, headers=headers, data=data)
|
| 105 |
|
| 106 |
+
def reformat_nation_data(xml_data):
|
| 107 |
+
"""
|
| 108 |
+
Parses and reformats NationStates XML data into a structured dictionary.
|
| 109 |
+
|
| 110 |
+
Args:
|
| 111 |
+
xml_data (str): Raw XML data.
|
| 112 |
+
|
| 113 |
+
Returns:
|
| 114 |
+
dict: A dictionary containing nation details, issue info, rankings, reclassifications, and headlines.
|
| 115 |
+
"""
|
| 116 |
+
soup = BeautifulSoup(xml_data, "xml")
|
| 117 |
+
|
| 118 |
+
nation_elem = soup.find("NATION")
|
| 119 |
+
nation_id = nation_elem.get("id") if nation_elem else "Unknown"
|
| 120 |
+
|
| 121 |
+
issue_elem = nation_elem.find("ISSUE") if nation_elem else None
|
| 122 |
+
issue_data = {
|
| 123 |
+
"issue_id": issue_elem.get("id") if issue_elem else "Unknown",
|
| 124 |
+
"choice": issue_elem.get("choice") if issue_elem else "Unknown",
|
| 125 |
+
"ok": issue_elem.OK.get_text() if issue_elem and issue_elem.OK else "Unknown",
|
| 126 |
+
"description": issue_elem.DESC.get_text() if issue_elem and issue_elem.DESC else "No description available",
|
| 127 |
+
"rankings": [],
|
| 128 |
+
"reclassifications": [],
|
| 129 |
+
"headlines": []
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
# Extract rankings
|
| 133 |
+
rankings_elem = issue_elem.find("RANKINGS") if issue_elem else None
|
| 134 |
+
if rankings_elem:
|
| 135 |
+
for rank in rankings_elem.find_all("RANK"):
|
| 136 |
+
issue_data["rankings"].append({
|
| 137 |
+
"rank_id": rank.get("id", "Unknown"),
|
| 138 |
+
"score": rank.SCORE.get_text() if rank.SCORE else "Unknown",
|
| 139 |
+
"change": rank.CHANGE.get_text() if rank.CHANGE else "Unknown",
|
| 140 |
+
"pchange": rank.PCHANGE.get_text() if rank.PCHANGE else "Unknown"
|
| 141 |
+
})
|
| 142 |
+
|
| 143 |
+
# Extract reclassifications
|
| 144 |
+
reclassifications_elem = issue_elem.find("RECLASSIFICATIONS") if issue_elem else None
|
| 145 |
+
if reclassifications_elem:
|
| 146 |
+
for reclassify in reclassifications_elem.find_all("RECLASSIFY"):
|
| 147 |
+
issue_data["reclassifications"].append({
|
| 148 |
+
"type": reclassify.get("type", "Unknown"),
|
| 149 |
+
"from": reclassify.FROM.get_text() if reclassify.FROM else "Unknown",
|
| 150 |
+
"to": reclassify.TO.get_text() if reclassify.TO else "Unknown"
|
| 151 |
+
})
|
| 152 |
+
|
| 153 |
+
# Extract headlines
|
| 154 |
+
headlines_elem = issue_elem.find("HEADLINES") if issue_elem else None
|
| 155 |
+
if headlines_elem:
|
| 156 |
+
issue_data["headlines"] = [headline.get_text() for headline in headlines_elem.find_all("HEADLINE")]
|
| 157 |
+
|
| 158 |
+
return {"nation_id": nation_id, "issue": issue_data}
|
| 159 |
+
|
| 160 |
+
return reformat_nation_data(response.text)
|
| 161 |
|
| 162 |
# Create the Gradio interface
|
| 163 |
+
get_nation_issues_interface = gr.Interface(
|
| 164 |
+
fn=get_nation_issues,
|
| 165 |
inputs=[
|
| 166 |
gr.Textbox(label="Nation Name", placeholder="Enter the nation name"),
|
| 167 |
gr.Textbox(label="API Password", placeholder="Enter your API password"),
|
|
|
|
| 185 |
api_name="address_nation_issues",
|
| 186 |
)
|
| 187 |
demo = gr.TabbedInterface(
|
| 188 |
+
interface_list=[get_nation_issues_interface, address_nation_issues_interface],
|
| 189 |
tab_names=["Get Nation Issues JSON", "Address Nation Issues"])
|
| 190 |
# Launch the interface and MCP server
|
| 191 |
if __name__ == "__main__":
|