| author | ymh <ymh.work@gmail.com> |
| Tue, 12 Sep 2017 18:49:02 +0200 | |
| changeset 683 | 59d49ab04ded |
| parent 571 | d9642be7c937 |
| child 693 | 09e00f38d177 |
| permissions | -rw-r--r-- |
| 204 | 1 |
# -*- coding: utf-8 -*- |
2 |
''' |
|
3 |
Created on July 2, 2012 |
|
4 |
||
5 |
@author: raphv |
|
6 |
''' |
|
7 |
||
8 |
from django.conf import settings |
|
9 |
from django.core.management.base import NoArgsCommand |
|
10 |
from django.core.management.color import no_style |
|
11 |
from hdalab.models import WpCategory, WpCategoryInclusion |
|
12 |
from optparse import make_option |
|
13 |
from wikitools import api,wiki |
|
14 |
import sys |
|
15 |
import re |
|
16 |
import itertools |
|
17 |
from hdabo import utils |
|
18 |
from django.db.models import Count |
|
19 |
from django.db import transaction |
|
20 |
||
21 |
CATEGORY_PREFIX = u'Catégorie:' |
|
22 |
||
23 |
class Command(NoArgsCommand): |
|
24 |
''' |
|
25 |
query and update wikipedia for tag title. |
|
26 |
''' |
|
27 |
options = '' |
|
28 |
help = """query and update wikipedia for tag title.""" |
|
29 |
||
30 |
option_list = NoArgsCommand.option_list + ( |
|
31 |
make_option('--all', |
|
32 |
action='store_true', |
|
33 |
dest='all', |
|
34 |
default=False, |
|
35 |
help='force all categories to be updated, not only those not yet processed'), |
|
36 |
make_option('--force', |
|
37 |
action='store_true', |
|
38 |
dest='force', |
|
39 |
default=False, |
|
40 |
help='ask no questions'), |
|
41 |
make_option('--site', |
|
42 |
action='store', |
|
43 |
type='string', |
|
44 |
dest='site_url', |
|
|
683
59d49ab04ded
use https for wikipedia api endpoint
ymh <ymh.work@gmail.com>
parents:
571
diff
changeset
|
45 |
default="https://fr.wikipedia.org/w/api.php", |
| 204 | 46 |
help='the url for the wikipedia site'), |
47 |
make_option('--limit', |
|
48 |
action='store', |
|
49 |
type='int', |
|
50 |
dest='limit', |
|
51 |
default= -1, |
|
52 |
help='number of categories to process'), |
|
53 |
make_option('--start', |
|
54 |
action='store', |
|
55 |
type='int', |
|
56 |
dest='start', |
|
57 |
default=0, |
|
58 |
help='number of categories to ignore'), |
|
59 |
make_option('--category', |
|
60 |
action='append', |
|
61 |
dest='category', |
|
62 |
type='string', |
|
63 |
default=[], |
|
64 |
help='the categories to query'), |
|
65 |
||
66 |
) |
|
67 |
||
68 |
||
69 |
def query_all_categories(self, category_title, site): |
|
70 |
||
71 |
params = {'action':'query', 'cmtitle':category_title, 'list':'categorymembers', 'cmlimit': 'max'} |
|
72 |
||
73 |
res = [] |
|
74 |
||
75 |
wpquery = api.APIRequest(site, params) #@UndefinedVariable |
|
76 |
response = wpquery.query() |
|
77 |
||
78 |
if self.verbosity > 1: |
|
79 |
print "Query category : " + repr(wpquery.request.get_full_url()+"?"+wpquery.request.get_data()) |
|
80 |
print repr(response) |
|
81 |
||
82 |
members = response.get('query', {}).get('categorymembers', []) |
|
83 |
||
84 |
for member in members: |
|
85 |
title = member.get('title',"") |
|
86 |
if re.match(CATEGORY_PREFIX, title): |
|
87 |
res.append(re.sub(CATEGORY_PREFIX, "", title)) |
|
88 |
||
89 |
if self.verbosity > 1: |
|
90 |
print "Query categories result: " |
|
91 |
print repr(res) |
|
92 |
||
93 |
return res |
|
94 |
||
95 |
def process_categories(self, cat_list, parent_cat): |
|
96 |
for cat in cat_list: |
|
97 |
child_cat,created = WpCategory.objects.get_or_create(label=cat) #@UnusedVariable |
|
98 |
WpCategoryInclusion.objects.get_or_create(parent_category=parent_cat, child_category=child_cat) |
|
99 |
||
100 |
def handle_noargs(self, **options): |
|
101 |
||
102 |
self.style = no_style() |
|
103 |
||
104 |
interactive = options.get('interactive', True) |
|
105 |
||
106 |
self.verbosity = int(options.get('verbosity', '1')) |
|
107 |
||
108 |
force = options.get('force', False) |
|
109 |
||
110 |
limit = options.get("limit", -1) |
|
111 |
start = options.get("start", 0) |
|
112 |
||
113 |
site_url = options.get('site_url', settings.WIKIPEDIA_API_URL) |
|
114 |
||
115 |
types_mask = 0 |
|
116 |
||
117 |
if self.verbosity > 2: |
|
118 |
print "option passed : " + repr(options) |
|
119 |
||
| 206 | 120 |
queryset = WpCategory.objects.filter(tags__hidden = False).distinct() |
| 204 | 121 |
|
122 |
cat_list = options.get("category", []); |
|
123 |
||
124 |
if cat_list: |
|
125 |
queryset = queryset.filter(label__in=cat_list) |
|
|
216
c4953332bc52
correct all category query condition
ymh <ymh.work@gmail.com>
parents:
206
diff
changeset
|
126 |
elif options.get('all',False): |
| 206 | 127 |
queryset = queryset.annotate(wpc=Count('child_categories')).filter(wpc = 0) |
| 204 | 128 |
|
| 206 | 129 |
queryset = queryset.order_by("label") |
| 204 | 130 |
|
131 |
if limit >= 0: |
|
132 |
queryset = queryset[start:limit] |
|
133 |
elif start > 0: |
|
134 |
queryset = queryset[start:] |
|
135 |
||
136 |
if self.verbosity > 2 : |
|
137 |
print "Category Query is %s" % (queryset.query) |
|
138 |
||
139 |
site = wiki.Wiki(site_url) #@UndefinedVariable |
|
140 |
||
141 |
||
142 |
count = queryset.count() |
|
143 |
if self.verbosity > 1: |
|
144 |
print "Processing %d categories" % (count) |
|
145 |
||
146 |
if not force and interactive: |
|
147 |
confirm = raw_input("You have requested to query and replace the wikipedia information for %d categories.\n Are you sure you want to do this? \nType 'yes' to continue, or 'no' to cancel: " % (count)) |
|
148 |
else: |
|
149 |
confirm = 'yes' |
|
150 |
||
151 |
if confirm != "yes": |
|
152 |
print "wikipedia query cancelled" |
|
153 |
return |
|
154 |
||
155 |
for i, category in enumerate(queryset): |
|
156 |
||
157 |
if self.verbosity > 1: |
|
158 |
print "processing category %s (%d/%d)" % (category.label, i + 1, count) |
|
159 |
else: |
|
160 |
utils.show_progress(i + 1, count, category.label, 60) |
|
161 |
||
162 |
title = CATEGORY_PREFIX + category.label |
|
163 |
# query categories |
|
164 |
||
|
571
d9642be7c937
replace commit_on_success with atomic
ymh <ymh.work@gmail.com>
parents:
271
diff
changeset
|
165 |
with transaction.atomic(): |
| 204 | 166 |
res = self.query_all_categories(title, site) |
167 |
self.process_categories(res, category) |