Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions App/templates/App/task5.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{% load static %}
<!DOCTYPE html>
<html>
<head>
<title>
Microtask 5
</title>
<link type="text/css" rel="stylesheet" href="https://tools-static.wmflabs.org/cdnjs/ajax/libs/materialize/0.100.2/css/materialize.css" ></link>
<link type="text/css" rel="stylesheet" href={% static "style.css" %} ></link>
</head>
<body>
<div class="row">
<nav>
<div class="nav-wrapper pd-left-20">
<a href="{% url 'App:get_article_view_count' %}" class="brand-logo">Microtask 5: View Count of articles in current year</a>
<ul id="nav-mobile" class="right hide-on-med-and-down">
<li><a href="{% url 'App:get_the_user_percentile' %}">Microtask1</a></li>
<li><a href="{% url 'App:get_the_user_percentile' %}">Microtask2</a></li>
<li><a href="{% url 'App:index' %}">Home</a></li>
</ul>
</div>
</nav>
</div>
<center><h4>Tool to display the view count of articles in current year</h4></center>
<div class="row">
<form class="col s12" method="post">
{% csrf_token %}
<div class="row">
<div class="input-field col s6 m6 offset-m3">
<input id="username" name="username" type="text">
<label for="username">Username</label>
</div>
</div>
<div class="row">
<div class="col s6 m6 offset-m3">
<button class="btn waves-effect waves-light blue-color" type="submit" name="action">Search
</button> E.g. username = WilliamJE
</div>
</div>
</form>
</div>
{% if error %}
<div class="row">
<div class="col s12 m6 offset-m3">
<div class="card blue lighten-2">
<div class="card-content white-text">
{{error}}
</div>
</div>
</div>
</div>
{% endif %}
<!-- loop -->
{% for x in articles %}

<div class="row">
<div class="col s12 m6 offset-m3">
<div class="card blue lighten-2">
<div class="card-content white-text">
<ul>
<li>Title = {{x.Title}} </li>
<li>Viewcount = {{x.Views}} </li>
</ul>
</div>
</div>
</div>
</div>

{% endfor %}
</body>
</html>
3 changes: 3 additions & 0 deletions App/urls.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
from django.conf.urls import url

urlpatterns = [
url(r'Microtask5/', views.get_article_view_count, name='get_article_view_count'),
]
66 changes: 66 additions & 0 deletions App/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import json
import requests
import urllib.parse

from django.http import HttpResponse
from django.shortcuts import render
from django.utils.http import urlquote


def get_article_view_count(request):
if request.method=='POST':
username = request.POST['username']
try:
if not username:
raise Error("Please enter a username")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be Exception, not Error (I probably remembered wrong). Or you can define your own error type (that has the benefit that you can let other types bubble up to the framework and it will probably show a more developer-friendly error message, with stack traces and whatnot).

params = {'action':'query',
'format':'json',
'list':'usercontribs',
'uclimit':'5',
'ucuser':username }
url_base = 'https://en.wikipedia.org/w/api.php?'
# Recieved data in json-format
response = requests.get(url_base, params = params)
if response.status_code == 200:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use response.raise_for_status() and let exception handling deal with non-200 responses.

edits_data = json.loads(response.text)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC you can just call response.json()

if 'error' in edits_data:
raise Error(edits_data['error']['info'])
title_list = [i['title'] for i in edits_data['query']['usercontribs']]
if len(title_list) == 0:
raise Error("No edits found for user")
details = list()
for title in title_list:
if title:
project = 'en.wikipedia.org'
access = 'all-access'
agent = 'all-agents'
article = urlquote(title)
granularity = 'monthly'
start = '20170101'
end = '20171106'
url_viewcount = "https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/{}/{}/{}/{}/{}/{}/{}".format(
project,
access,
agent,
article,
granularity,
start,
end)
view_response = requests.get(url_viewcount)
if view_response.status_code != 200:
raise Error('Exit code {}'.format(str(response.status_code)))
view_data = json.loads(view_response.text)
total_view_count = 0
for i in view_data['items']:
total_view_count += i['views']
display_dict = {'Title': title,
'Views': total_view_count}
details.append(display_dict)
context = {'articles': details}
return render(request, 'App/task5.html', context)

except Exception as err:
return render(request, 'App/task5.html', {'error': err})

else:
return(render(request,'App/task5.html',{}))