|
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('--random', |
|
|
42 |
action='store_true', |
|
|
43 |
dest='random', |
|
|
44 |
default=False, |
|
|
45 |
help='randomize query on categories'), |
|
|
46 |
make_option('--site', |
|
|
47 |
action='store', |
|
|
48 |
type='string', |
|
|
49 |
dest='site_url', |
|
|
50 |
default="http://fr.wikipedia.org/w/api.php", |
|
|
51 |
help='the url for the wikipedia site'), |
|
|
52 |
make_option('--limit', |
|
|
53 |
action='store', |
|
|
54 |
type='int', |
|
|
55 |
dest='limit', |
|
|
56 |
default= -1, |
|
|
57 |
help='number of categories to process'), |
|
|
58 |
make_option('--start', |
|
|
59 |
action='store', |
|
|
60 |
type='int', |
|
|
61 |
dest='start', |
|
|
62 |
default=0, |
|
|
63 |
help='number of categories to ignore'), |
|
|
64 |
make_option('--category', |
|
|
65 |
action='append', |
|
|
66 |
dest='category', |
|
|
67 |
type='string', |
|
|
68 |
default=[], |
|
|
69 |
help='the categories to query'), |
|
|
70 |
|
|
|
71 |
) |
|
|
72 |
|
|
|
73 |
|
|
|
74 |
def query_all_categories(self, category_title, site): |
|
|
75 |
|
|
|
76 |
params = {'action':'query', 'cmtitle':category_title, 'list':'categorymembers', 'cmlimit': 'max'} |
|
|
77 |
|
|
|
78 |
res = [] |
|
|
79 |
|
|
|
80 |
wpquery = api.APIRequest(site, params) #@UndefinedVariable |
|
|
81 |
response = wpquery.query() |
|
|
82 |
|
|
|
83 |
if self.verbosity > 1: |
|
|
84 |
print "Query category : " + repr(wpquery.request.get_full_url()+"?"+wpquery.request.get_data()) |
|
|
85 |
print repr(response) |
|
|
86 |
|
|
|
87 |
members = response.get('query', {}).get('categorymembers', []) |
|
|
88 |
|
|
|
89 |
for member in members: |
|
|
90 |
title = member.get('title',"") |
|
|
91 |
if re.match(CATEGORY_PREFIX, title): |
|
|
92 |
res.append(re.sub(CATEGORY_PREFIX, "", title)) |
|
|
93 |
|
|
|
94 |
if self.verbosity > 1: |
|
|
95 |
print "Query categories result: " |
|
|
96 |
print repr(res) |
|
|
97 |
|
|
|
98 |
return res |
|
|
99 |
|
|
|
100 |
def process_categories(self, cat_list, parent_cat): |
|
|
101 |
for cat in cat_list: |
|
|
102 |
child_cat,created = WpCategory.objects.get_or_create(label=cat) #@UnusedVariable |
|
|
103 |
WpCategoryInclusion.objects.get_or_create(parent_category=parent_cat, child_category=child_cat) |
|
|
104 |
|
|
|
105 |
def handle_noargs(self, **options): |
|
|
106 |
|
|
|
107 |
self.style = no_style() |
|
|
108 |
|
|
|
109 |
interactive = options.get('interactive', True) |
|
|
110 |
|
|
|
111 |
self.verbosity = int(options.get('verbosity', '1')) |
|
|
112 |
|
|
|
113 |
force = options.get('force', False) |
|
|
114 |
|
|
|
115 |
limit = options.get("limit", -1) |
|
|
116 |
start = options.get("start", 0) |
|
|
117 |
|
|
|
118 |
site_url = options.get('site_url', settings.WIKIPEDIA_API_URL) |
|
|
119 |
|
|
|
120 |
random = options.get('random', False) |
|
|
121 |
|
|
|
122 |
types_mask = 0 |
|
|
123 |
|
|
|
124 |
if self.verbosity > 2: |
|
|
125 |
print "option passed : " + repr(options) |
|
|
126 |
|
|
|
127 |
queryset = WpCategory.objects |
|
|
128 |
|
|
|
129 |
cat_list = options.get("category", []); |
|
|
130 |
|
|
|
131 |
if cat_list: |
|
|
132 |
queryset = queryset.filter(label__in=cat_list) |
|
|
133 |
elif not options.get('all',False): |
|
|
134 |
queryset = queryset.annotate(wpc=Count('child_categories')).filter(wpc = 0) |
|
|
135 |
#else: |
|
|
136 |
# queryset = Tag.objects.filter(url_status=None) |
|
|
137 |
|
|
|
138 |
if random: |
|
|
139 |
queryset = queryset.order_by("?") |
|
|
140 |
else: |
|
|
141 |
queryset = queryset.order_by("label") |
|
|
142 |
|
|
|
143 |
if limit >= 0: |
|
|
144 |
queryset = queryset[start:limit] |
|
|
145 |
elif start > 0: |
|
|
146 |
queryset = queryset[start:] |
|
|
147 |
|
|
|
148 |
if self.verbosity > 2 : |
|
|
149 |
print "Category Query is %s" % (queryset.query) |
|
|
150 |
|
|
|
151 |
site = wiki.Wiki(site_url) #@UndefinedVariable |
|
|
152 |
|
|
|
153 |
|
|
|
154 |
count = queryset.count() |
|
|
155 |
if self.verbosity > 1: |
|
|
156 |
print "Processing %d categories" % (count) |
|
|
157 |
|
|
|
158 |
if not force and interactive: |
|
|
159 |
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)) |
|
|
160 |
else: |
|
|
161 |
confirm = 'yes' |
|
|
162 |
|
|
|
163 |
if confirm != "yes": |
|
|
164 |
print "wikipedia query cancelled" |
|
|
165 |
return |
|
|
166 |
|
|
|
167 |
for i, category in enumerate(queryset): |
|
|
168 |
|
|
|
169 |
if self.verbosity > 1: |
|
|
170 |
print "processing category %s (%d/%d)" % (category.label, i + 1, count) |
|
|
171 |
else: |
|
|
172 |
utils.show_progress(i + 1, count, category.label, 60) |
|
|
173 |
|
|
|
174 |
title = CATEGORY_PREFIX + category.label |
|
|
175 |
# query categories |
|
|
176 |
|
|
|
177 |
with transaction.commit_on_success(): |
|
|
178 |
res = self.query_all_categories(title, site) |
|
|
179 |
self.process_categories(res, category) |