Year category intersection implemented

master
Castro0o 9 years ago
parent 6d421ccfc8
commit 8f3d66d8ac

@ -5,4 +5,4 @@ The process of creating a Website for MMD&C on the backend entails 4 steps:
* create JSON dictionary
* create index
* create pages
* (parse pages)
* (parse pages

File diff suppressed because one or more lines are too long

@ -74,17 +74,37 @@ def parse_work_page(title, content):
print 'THUMB:', thumburl
work_dict[key]=val
return work_dict
def api_PageCategories(pageid):
'''Find all the categories, and their parent category of a page '''
query = 'action=query&pageids={}&prop=categories'.format(pageid)
url = endpoint + query
request = urllib2.urlopen(url)
jsonp = json.loads(request.read())
json_dic = jsonp['query']['pages']
page_id = json_dic.keys()[0]
page_categories = json_dic[page_id][u'categories']
all_cats = [ entry[u'title'].encode('utf-8') for entry in page_categories ] #.replace('Category:', '')
return all_cats
def api_category(category, year): #Find all pages incategory and add to allworks dictionary
''' TODO: category intersection; With SirrusSearch'''
category = category.replace(' ', '_')
if year:
api_url = endpoint + 'action=query&list=categorymembers&cmlimit=500&cmtitle=Category:{}&cmtitle=Category:{}'.format(category, year)
else:
api_url = endpoint + 'action=query&list=categorymembers&cmlimit=500&cmtitle=Category:{}'.format(category)
request = urllib2.urlopen(api_url)
apiCatMembers = endpoint + 'action=query&list=categorymembers&cmlimit=1000&cmtitle=Category:{}'.format(category)
request = urllib2.urlopen(apiCatMembers)
jsonp = json.loads(request.read())
for page in jsonp['query']['categorymembers']:
Graduation_work_Members = jsonp['query']['categorymembers']
intersectCatMembers = []
if year:
for member in Graduation_work_Members:
page_cats = api_PageCategories(member['pageid'])
if ('Category:{}'.format(year)) in page_cats:
print year, 'in', page_cats
intersectCatMembers.append(member)# add member to intersectCatMembers
else:
intersectCatMembers = Graduation_work_Members
for page in intersectCatMembers:
title = ((page['title']).encode('utf-8') ).replace(" ", "_") #snakecase for page titles
pageid = page['pageid']
article = api_page(pageid, 'content')

Loading…
Cancel
Save