API:Picture of the day viewer/ru

Overview
In this tutorial, you will learn how to build a Wikipedia:Picture of the day viewer using the MediaWiki Action API. Download the code from GitHub Browse the app on Toolforge

This tutorial will teach you how to do this using:


 * Python 3 and Flask framework
 * MediaWiki Action API's and  modules

Setting up Python
This tutorial uses Python 3. You can download the latest Python version from here:


 * Python for Windows 7, 8, and 10
 * Python for Mac OS X

See the Python beginner's guide for further instructions on installing Python on different operating systems.

Setting up Flask
Pip is a package manager that you can use to install Flask:. If you don't have pip already, install it from the official Pip website.

Step 2: Create a simple Flask application
If you have everything successfully installed, place the following script in, inside your project folder:. When you run it (using  or  ), it should display "Hello world" at http://localhost:5000/ :

Step 3: Picture of the day viewer
Now that you have everything set up, you can start writing code for the Picture of the Day viewer. Picture of the day, or POTD, is a featured image displayed daily on the home page of Wikipedia. You'll be getting the picture from a wiki template that changes every day.

Getting today's date
Because POTD updates daily, you need today's date to access the archives and get at a stable version of the correct picture. To do this, import Python's class.

Next, define a new function,. will render the web page and pass along any data associated with our API calls. See for more information on the   file we'll be using as a template. For now,  should contain a variable holding the current date. We'll be using it soon to compose a query to access the POTD.

Calling the Action API
The Action API works by sending back data in response to HTTP requests. To do so, you will import the Python Requests library.

Next add two new variables:  and. You will use the  object to make requests to the   url.

In a new function,, call  to request the picture embedded within a protected POTD page (example). From this call, use the picture's filename to call, and retrieve the image's source url. In this example, the second API call is handled in the helper function,.

Wikipedia's archives lists dates by ISO standard format, for example: 2019-01-31, for 31 January, 2019. You can get the correct format by using the date method,.

Finally, alter  to call. Import  from , and make   return.

Displaying the page


Flask templates mostly contain HTML markup, but they also use Jinja to render dynamic content. Jinja markup looks like this:, and is used to inject Python variables or expressions into our basic page structure. Add some basic HTML 5 boilerplate and a few elements to. Make sure to save it to a directory within your app, named.

Making it interactive
Add a element to , and give it the following submit button inputs: Back and Next. When either button is selected, the form will submit a POST request, and the selected value will be passed back to. This will allow users to browse through the Picture of the Day archives.

Next, update  with a   function, for setting the date presented to the user. Also extend the  route to handle POST requests from the form. To allow  to read the POST request message, import Flask's   class.

The complete Python and HTML code:

Styling your app
Flask uses a directory, named static, to contain any helper files that stay the same throughout the lifecycle of the app. This is a useful place to put any stylesheets or additional scripts. Our stylesheet will be using some colors and visual motifs based on the Wikimedia Style Guide. Place a CSS file in.

Next steps

 * Contribute a demo app that you have developed using the MediaWiki API to this code samples repository.
 * Learn some ways to add descriptive text from the Picture of the day page:
 * provides  which can be used as a link preview
 * can be used to obtain the whole parsed text from articles on Wikis that have the extension installed (see example).



См. также

 * : Overview of the Action API
 * : FAQ describing the Action API in more depth
 * MediaWiki Action API Code Samples repo: Contains more demos and sample code
 * More on Wikipedia and IFTTT
 * MediaWiki Action API Code Samples repo: Contains more demos and sample code
 * More on Wikipedia and IFTTT