source: redis2orga.py @ 38:8a694e2a6847

Revision 38:8a694e2a6847, 3.5 KB checked in by hagenbruch@phoibe.ub.rub.de, 6 years ago (diff)

Version 0.3

Line 
1#!/usr/bin/env python
2# encoding: utf-8
3
4#  The MIT License
5#
6#  Copyright 2011 Andre Hagenbruch <andre.hagenbruch@ruhr-uni-bochum.de>.
7#
8#  Permission is hereby granted, free of charge, to any person obtaining a copy
9#  of this software and associated documentation files (the "Software"), to deal
10#  in the Software without restriction, including without limitation the rights
11#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12#  copies of the Software, and to permit persons to whom the Software is
13#  furnished to do so, subject to the following conditions:
14#
15#  The above copyright notice and this permission notice shall be included in
16#  all copies or substantial portions of the Software.
17#
18#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23#  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24#  THE SOFTWARE.
25
26__author__ = 'hagenbruch'
27__date__ = '10.08.11 08:58'
28
29import logging
30import redis
31import simplejson as json
32import urllib
33
34SOLR = 'http://134.147.243.89:8990/solr/select/?'
35
36logging.basicConfig (level=logging.DEBUG,
37                     format='%(asctime)s %(levelname)-4s %(message)s',
38                     datefmt='%a, %d %b %Y %H:%M:%S',
39                     )
40
41def _pairs(mylist):
42    mynext = iter(mylist).next
43    while True:
44        yield mynext(), mynext()
45
46def read_redis(redis_handle):
47    pnds = redis_handle.hgetall('pndorga')
48    tree = {}
49    for pnd in pnds:
50        orgas = eval(redis_handle.hget('pndorga', pnd))
51        for orga in orgas:
52            tree.setdefault(orga.get('institut'), {}).setdefault(orga.get('fachgebiet'), orga.get('fakultaet'))
53
54    return tree
55
56def solr_test(tree):
57    params = 'q=*:*&rows=0&facet=true&facet.field=fakultaet&facet.field=fach&facet.field=institut&facet.mincount=1&facet.limit=50000&wt=python&json.nl=arrarr&indent=true&omitHeader=true'
58    result = eval(urllib.urlopen(SOLR, params).read())
59
60    fak_list = result.get('facet_counts').get('facet_fields').get('fakultaet')
61    fac_list = result.get('facet_counts').get('facet_fields').get('fach')
62    inst_list = result.get('facet_counts').get('facet_fields').get('institut')
63
64    fak_dict = {}
65    for fakultaet in fak_list:
66        fak_dict.update(_pairs(fakultaet))
67
68    fac_dict = {}
69    for fach in fac_list:
70        fac_dict.update(_pairs(fach))
71
72    facets = {}
73    for institut in inst_list:
74        dasfach = tree.get(institut[0]).keys()[0]
75        diefakultaet = tree.get(institut[0]).values()[0]
76        fakultaet_anzahl = fak_dict.get(diefakultaet)
77        fach_anzahl = fac_dict.get(dasfach)
78        facets.setdefault(diefakultaet, {}).setdefault('count', fakultaet_anzahl)
79
80        if dasfach == '':
81            facets.setdefault(diefakultaet, {}).setdefault('orga', {}).setdefault('', []).append('%s#%d' % (institut[0], institut[1]))
82        else:
83            facets.setdefault(diefakultaet, {}).setdefault('orga', {}).setdefault('%s#%d' % (dasfach, fach_anzahl), []).append('%s#%d' % (institut[0], institut[1]))
84
85    logging.info(json.dumps(facets, indent=4))
86   
87
88def main():
89    r = redis.Redis(host='localhost', port=6379, db=0)
90    orga = read_redis(r)
91    #solr_test(orga)
92    logging.fatal(orga)
93
94if __name__ == '__main__':
95    main()
Note: See TracBrowser for help on using the repository browser.