-
Notifications
You must be signed in to change notification settings - Fork 0
Microtask 5 #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yashasingh
wants to merge
1
commit into
master
Choose a base branch
from
devtask5
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Microtask 5 #5
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,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> |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,2 +1,5 @@ | ||
| from django.conf.urls import url | ||
|
|
||
| urlpatterns = [ | ||
| url(r'Microtask5/', views.get_article_view_count, name='get_article_view_count'), | ||
| ] |
This file contains hidden or 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,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") | ||
| 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: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use |
||
| edits_data = json.loads(response.text) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIRC you can just call |
||
| 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',{})) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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).