Skip to content

Commit

Permalink
Merge branch 'master' into changing_modules
Browse files Browse the repository at this point in the history
  • Loading branch information
shivanshbindal9 committed Mar 4, 2019
2 parents a070275 + 1761dad commit cc044da
Show file tree
Hide file tree
Showing 24 changed files with 624 additions and 504 deletions.
19 changes: 15 additions & 4 deletions README.md
Expand Up @@ -22,6 +22,8 @@ Code snippets in Python demonstrating how to use various modules of the [MediaWi
* [API:Watchlist feed](https://www.mediawiki.org/wiki/API:Watchlist_feed)
* [get_my_watchlist_feed](python/get_my_watchlist_feed.py): access an RSS feed of your own watchlist
* [get_user_watchlist_feed](python/get_user_watchlist_feed.py): access an RSS feed of another user's watchlist
* [API:Blocks](https://www.mediawiki.org/wiki/API:Blocks)
* [get_blocked_users.py](python/blocks.py): get information about recent blocked users

### Page Operations
* [API:Parse](https://www.mediawiki.org/wiki/API:Parse)
Expand All @@ -45,6 +47,7 @@ Code snippets in Python demonstrating how to use various modules of the [MediaWi
* [get_filtered_page_revisions.py](python/get_filtered_page_revisions.py): get revision data of a page filtered by date and user
* [API:Links](https://www.mediawiki.org/wiki/API:Links)
* [get_links.py](python/get_links.py): get links embedded on a page
* [get_red_links.py](python/get_red_links.py): get the first twenty red links in a page
* [API:Info](https://www.mediawiki.org/wiki/API:Info)
* [get_info.py](python/get_info.py): get basic information about a page
* [API:Allpages](https://www.mediawiki.org/wiki/API:Allpages)
Expand Down Expand Up @@ -73,7 +76,13 @@ Code snippets in Python demonstrating how to use various modules of the [MediaWi
* [watch.py](python/watch.py): add a page to your watchlist
* [API:Alllinks](https://www.mediawiki.org/wiki/API:Alllinks)
* [get_alllinks.py](python/get_alllinks.py): list links to a namespace

* [API:RecentChanges](https://www.mediawiki.org/wiki/API:RecentChanges)
* [get_recent_changes.py](python/get_recent_changes.py): get the three most recent changes with sizes and flags
* [API:Querypage](https://www.mediawiki.org/wiki/API:Querypage)
* [get_querypage_list.py](python/get_querypage_list.py): List first 10 pages which are uncategorized
* [API:SetPageLanguage](https://www.mediawiki.org/wiki/API:SetPageLanguage)
* [set_page_language.py](python/set_page_language.py): change page language

### Search
* [API:Search](https://www.mediawiki.org/wiki/API:Search)
* [search.py](python/search.py): search for a title or a text
Expand All @@ -93,10 +102,12 @@ Code snippets in Python demonstrating how to use various modules of the [MediaWi
* [paraminfo.py](python/paraminfo.py): get information about another action API module and its parameters

### Demo apps
* [Article suggestion](python/demos/article%20suggestion):
A sample app that uses MediaWiki Action API:Search allows you to pick a category and suggest articles to write on that don't yet exist on English Wikipedia. This app uses Flask and WTForms for rendering form.
* [Article ideas generator](python/demos/article%20ideas%20generator):
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.
* [Nearby places viewer](python/demos/nearby%20places%20viewer):
Demo of geo search for wiki pages near a location using the [Geolocation API](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API) and MediaWiki Action API's [Geosearch](https://www.mediawiki.org/wiki/API:Geosearch) module.
Demo of geo search for wiki pages near a location using the [Geolocation API](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API) and MediaWiki Action API's [Geosearch](https://www.mediawiki.org/wiki/API:Geosearch) module.
* [Picture of the day viewer](python/demos/picture%20of%20the%20day%20viewer):
Demo app that uses [prop=images](https://www.mediawiki.org/wiki/API:Images) module to fetch Wikipedia's Picture of the Day (POTD) from a template page and displays it on a webpage. The app also allows users to go backward or forward a date to view other POTD.

### Installation
```
Expand Down
2 changes: 1 addition & 1 deletion modules.json
Expand Up @@ -367,4 +367,4 @@
"ucuser": "Jimbo Wales"
}
}
]
]
27 changes: 27 additions & 0 deletions python/demos/article ideas generator/README.md
@@ -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>
102 changes: 102 additions & 0 deletions python/demos/article ideas generator/articles.py
@@ -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()
39 changes: 39 additions & 0 deletions python/demos/article ideas generator/static/style.css
@@ -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 python/demos/article ideas generator/templates/articles.html
@@ -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>
23 changes: 0 additions & 23 deletions python/demos/article suggestion/README.md

This file was deleted.

117 changes: 0 additions & 117 deletions python/demos/article suggestion/app.py

This file was deleted.

0 comments on commit cc044da

Please sign in to comment.