Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into changing_modules
- Loading branch information
Showing
24 changed files
with
624 additions
and
504 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -367,4 +367,4 @@ | |
"ucuser": "Jimbo Wales" | ||
} | ||
} | ||
] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Article ideas generator | ||
Demo app that suggests articles from various categories that don't yet exist on English Wikipedia. The app uses [Parse](https://www.mediawiki.org/wiki/API:Parse) and [Links](https://www.mediawiki.org/wiki/API:Links) module and fetches data from this resource: https://en.wikipedia.org/wiki/Wikipedia:Requested_articles | ||
|
||
Install | ||
------- | ||
|
||
``` | ||
$ git clone https://github.com/wikimedia/MediaWiki-Action-API-Code-Samples | ||
$ cd MediaWiki-Action-API-Code-Samples/python/demos/article ideas generator | ||
$ pip install flask | ||
Install the necessary python modules with pip | ||
$ python3 articles.py | ||
``` | ||
|
||
Screenshots | ||
----------- | ||
<table> | ||
<tr> | ||
<td><img src="http://upload.wikimedia.org/wikipedia/commons/b/bf/Article_ideas_generator_demo_app_screenshot_%282%29.png" width="300"> | ||
<td><img src="http://upload.wikimedia.org/wikipedia/commons/f/f7/Article_ideas_generator_demo_app_screenshot_%283%29.png" width="300"> | ||
<td><img src="http://upload.wikimedia.org/wikipedia/commons/7/7e/Article_ideas_generator_demo_app_screenshot_%284%29.png" width="300"> | ||
|
||
<tr> | ||
<td>1. Choose a category | ||
<td>2. Choose a subcategory | ||
<td>3. View missing links | ||
</table> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#!/usr/bin/python3 | ||
|
||
""" | ||
articles.py | ||
MediaWiki Action API Code Samples | ||
Article ideas generator app: suggests articles from various categories | ||
that don't yet exist on English Wikipedia. The app uses action=parse module | ||
and prop=links module as a generator. | ||
MIT license | ||
""" | ||
|
||
from flask import Flask, request, render_template | ||
import requests | ||
|
||
APP = Flask(__name__) | ||
SESSION = requests.Session() | ||
API_ENDPOINT = 'https://en.wikipedia.org/w/api.php' | ||
PAGE = {} | ||
|
||
|
||
@APP.route('/', methods=['GET', 'POST']) | ||
def index(): | ||
""" Displays the index page accessible at '/' | ||
""" | ||
global PAGE | ||
results = [] | ||
|
||
if request.method == 'POST': | ||
if 'category' in request.form: | ||
PAGE['name'] = PAGE['name'] + '/' + \ | ||
request.form.to_dict()['category'] | ||
PAGE['type'] = 'subcategory' | ||
results = get_page_sections(PAGE['name']) | ||
elif 'subcategory' in request.form: | ||
PAGE['name'] = PAGE['name'] + '#' + \ | ||
request.form.to_dict()['subcategory'] | ||
PAGE['type'] = 'links' | ||
results = get_red_links(PAGE['name']) | ||
else: | ||
PAGE = {'name': 'Wikipedia:Requested_articles', 'type': 'category'} | ||
results = get_page_sections(PAGE['name']) | ||
|
||
return render_template( | ||
"articles.html", | ||
results=results, | ||
pagetype=PAGE['type']) | ||
|
||
|
||
def get_page_sections(page): | ||
""" Get page sections | ||
""" | ||
params = { | ||
"action": "parse", | ||
"page": page, | ||
"prop": "sections", | ||
"format": "json" | ||
} | ||
|
||
res = SESSION.get(url=API_ENDPOINT, params=params) | ||
data = res.json() | ||
|
||
if 'error' in data: | ||
return | ||
|
||
parsed_sections = data and data['parse'] and data['parse']['sections'] | ||
sections = [] | ||
|
||
for section in parsed_sections: | ||
if section['toclevel'] == 1: | ||
sections.append(section['line']) | ||
|
||
return sections | ||
|
||
|
||
def get_red_links(title): | ||
""" Get missing links on a page | ||
""" | ||
params = { | ||
"action": "query", | ||
"titles": title, | ||
"generator": "links", | ||
"gpllimit": 20, | ||
"format": "json" | ||
} | ||
|
||
res = SESSION.get(url=API_ENDPOINT, params=params) | ||
data = res.json() | ||
pages = data and data['query'] and data['query']['pages'] | ||
links = [] | ||
|
||
for page in pages.values(): | ||
if 'missing' in page: | ||
links.append(page['title']) | ||
|
||
return links | ||
|
||
|
||
if __name__ == '__main__': | ||
APP.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
h1 { | ||
color: black; | ||
font-family: 'Amatic SC', cursive; | ||
font-size: 4.5em; | ||
font-weight: normal; | ||
} | ||
|
||
div { | ||
left: 10%; | ||
position: absolute; | ||
right: 10%; | ||
text-align: center; | ||
top: 5%; | ||
} | ||
|
||
p { | ||
font-family: 'Josefin Sans', sans-serif; | ||
font-size: 1.4em; | ||
} | ||
|
||
button { | ||
background-color: #06b6c9; | ||
border: none; | ||
border-radius: 5px; | ||
color: white; | ||
font-size: 1.2em; | ||
margin: 5px; | ||
padding: 20px; | ||
} | ||
|
||
.subcategory { | ||
background-color: #EE6352; | ||
} | ||
|
||
a { | ||
color: red; | ||
font-size: 1.2em; | ||
line-height: 1.4em; | ||
} |
30 changes: 30 additions & 0 deletions
30
python/demos/article ideas generator/templates/articles.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<title>Article ideas generator</title> | ||
|
||
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Amatic+SC:700"> | ||
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Josefin+Sans"> | ||
<link rel="stylesheet" href="/static/style.css"> | ||
|
||
<div> | ||
<h1>Article ideas generator</h1> | ||
{% if 'links' in pagetype %} | ||
<p>Some ideas for topics to write articles on:</p> | ||
{% for link in results %} | ||
<a href="//en.wikipedia.org/w/index.php?title={{ link }}&action=edit&redlink=1">{{ link }} | ||
</a> | ||
<br> | ||
{% endfor %} | ||
<button onclick="location.href='/'">Take me to the homepage</button> | ||
{% else %} | ||
{% if results %} | ||
<p>Choose a {{ pagetype }}</p> | ||
<form method="POST"> | ||
{% for pagename in results %} | ||
<button name="{{ pagetype }}" class="{{ pagetype }}" value="{{ pagename }}">{{ pagename }}</button> | ||
{% endfor %} | ||
{% else %} | ||
<p>Ooooops! We couldn't find any results.</p> | ||
<button onclick="location.href='/'">Start over</button> | ||
</form> | ||
{% endif %} | ||
{% endif %} | ||
</div> |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.