|
| 1 | +import json |
| 2 | +import requests |
| 3 | +import urllib.parse |
| 4 | + |
| 5 | +from django.http import HttpResponse |
| 6 | +from django.shortcuts import render |
| 7 | + |
| 8 | + |
| 9 | +def user_revisions_analysis(request): |
| 10 | + """ |
| 11 | + Display analysis of last 5 edits made by user. |
| 12 | + """ |
| 13 | + if request.method=='POST': |
| 14 | + # Here, we get the username |
| 15 | + username = request.POST['username'] |
| 16 | + # if username is submitted blank |
| 17 | + if not username: |
| 18 | + return render(request, "App/task4.html", {"error": "Please enter a username"}) |
| 19 | + |
| 20 | + # if username is not blank |
| 21 | + details = list() |
| 22 | + parameters = {'action':'query', |
| 23 | + 'format':'json', |
| 24 | + 'list':'usercontribs', |
| 25 | + 'uclimit':'5', |
| 26 | + 'ucuser':username} |
| 27 | + url_parameters = urllib.parse.urlencode(parameters) |
| 28 | + url1 = 'https://en.wikipedia.org/w/api.php?{}'.format(url_parameters) |
| 29 | + |
| 30 | + # Recieved data in json-format |
| 31 | + response = requests.get(url1) |
| 32 | + |
| 33 | + if response.status_code == 200: |
| 34 | + lst = json.loads(response.text) |
| 35 | + try: |
| 36 | + # if username contains invalid value |
| 37 | + if lst['error']: |
| 38 | + return render(request, |
| 39 | + 'App/task4.html', |
| 40 | + {"error":lst['error']['info']} |
| 41 | + ) |
| 42 | + except: |
| 43 | + # Edit data extracted |
| 44 | + edits_data = json.loads(response.text) |
| 45 | + contributions = [i for i in edits_data['query']['usercontribs']] |
| 46 | + # if user has no edits |
| 47 | + if len(contributions) == 0: |
| 48 | + return render(request, |
| 49 | + 'App/task4.html', |
| 50 | + {"error": "0 edits found for username {}".format(username)}) |
| 51 | + |
| 52 | + for articles in contributions: |
| 53 | + url2 = "https://ores.wikimedia.org/v3/scores/enwiki/" + str(articles['revid']) |
| 54 | + # recieve ORES response |
| 55 | + response2 = requests.get(url2) |
| 56 | + if response2.status_code != 200: |
| 57 | + return render(request, |
| 58 | + 'App/tesk4.html', |
| 59 | + {"error": "ORES failed to return response"}) |
| 60 | + |
| 61 | + # if successfull response from ORES |
| 62 | + data = json.loads(response2.text) |
| 63 | + analysis = data['enwiki']['scores'][str(articles['revid'])] |
| 64 | + predictions = {'goodfaith': analysis['goodfaith']['score']['prediction'], |
| 65 | + 'reverted': analysis['reverted']['score']['prediction'], |
| 66 | + 'damaging': analysis['damaging']['score']['prediction'], |
| 67 | + 'draftquality': analysis['draftquality']['score']['prediction'], |
| 68 | + 'quality': analysis['wp10']['score']['prediction'], |
| 69 | + 'title': articles['title'], |
| 70 | + 'comment': articles['comment'], |
| 71 | + 'timestamp': articles['timestamp'], |
| 72 | + 'revid': articles['revid'], |
| 73 | + 'user': articles['user']} |
| 74 | + details.append(predictions) |
| 75 | + context = {'articles': details} |
| 76 | + return render(request,'App/task4.html',context) |
| 77 | + |
| 78 | + else: |
| 79 | + return render(request,'App/task4.html',{}) |
0 commit comments