Example #1
0
    def handle(self, *args, **options):
        first_row = True
        with open(args[0], 'rb') as csvfile:
            for row in csv.reader(csvfile, delimiter=','):
                if row[0] and not first_row:
                    location = row[1]+","+row[0]
                    needs = row[2:5]
                    for need in needs:
                        if need:
                            data = dict()
                            data['from_num'] = '1111'
                            _type = sms._find_type(needs[0])
                            if _type:
                                data['type'] = sms._find_type(needs[0])
                            data['_id'] = uid_gen()
                            data['words'] = map(lambda w: w.lower().strip(), need.split(' '))
                            data['loc_input'] = location
                            res = geocode(data['loc_input'])

                            if res:
                                place, loc, score = res
                                data.update({'loc':loc, 'loc_place':place, 'loc_score':score})                            

                            data['created'] = 1333425600
                            
                            db['needs'].save(data)
                            
                first_row = False
Example #2
0
def _chart_data():
    # this map/reduce groups needs by month
    mapper = Code(
        """
        function() {
            var d = new Date(this.created*1000);
            var key = new Date(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), 0, 0, 0).getTime();
            emit(key, 1);
        }
    """
    )

    reducer = Code(
        """
        function(key, values) {
            var sum = 0;
            for (var i = 0; i < values.length; i++) {
                sum += values[i];
            }
            return sum;
        }
    """
    )

    data = []
    res = db["needs"].map_reduce(mapper, reducer, "tmp_%s" % uid_gen())
    for doc in res.find():
        data.append([int(doc["_id"]), int(doc["value"])])

    res.drop()

    return data
Example #3
0
def loc_data(request):
    req = json.loads(request.raw_post_data)
    loc_places = req["loc_places"]
    start = int(req.get("start", 0))
    end = int(req.get("end", time.time()))
    words = set(req.get("words", "").split(","))
    if "" in words:
        words.remove("")

    mapper = Code(
        """
        function() {
            emit(this.type, 1);
        }
    """
    )

    reducer = Code(
        """
        function(key, values) {
            var sum = 0;
            for (var i = 0; i < values.length; i++) {
                sum += values[i];
            }
            return sum;
        }
    """
    )

    condition = {"loc_place": {"$in": loc_places}, "created": {"$lte": end, "$gte": start}}
    if words:
        condition["words"] = {"$in": map(lambda word: word.lower().strip(), words)}
    else:
        condition["type"] = {"$exists": 1}

    res = db["needs"].map_reduce(mapper, reducer, "tmp_%s" % uid_gen(), query=condition)

    data = []
    for doc in res.find():
        if not doc["_id"]:
            if words:
                data.append(dict(type="Other", sum=int(doc["value"])))
        else:
            data.append(dict(type=doc["_id"], sum=int(doc["value"])))

    res.drop()
    data.sort(lambda a, b: b["sum"] - a["sum"])

    return HttpResponse(json.dumps(data), content_type="application/json")
Example #4
0
def _new(request):

    r = twiml.Response()

    _type = _find_type(request.REQUEST.get('Body', ''))

    data = dict()

    if _type:
        data['type'] = _type

    data['body'] = request.REQUEST.get('Body', '')

    number = _number(request)

    data['from_num'] =  number
    data['session'] = number

    data['full'] = str(request.REQUEST)
    data['_id'] = uid_gen()
    data['created'] = time.time()

    data['country'] = request.REQUEST.get('FromCountry', None)

    words = set(map(lambda w: w.lower().strip(), request.REQUEST.get('Body', '').split(' ')))
    for exclude_word in EXCLUDE_WORDS:
        if exclude_word in words:
            words.remove(exclude_word)

    data['words'] = list(words)

    uid = db['needs'].save(data)

    if uid:
        if data['country']=='US':
            if _type:
                r.sms('Thanks for sending in your needs for %s. Where are you located? Please only enter city, state E.g. elmhurst, ny' % _type)
            else:
                r.sms('Thanks for sending in your need. Where are you located? Please only enter city, state E.g. elmhurst, ny')
        else:
            r.sms('Thanks for sending in your needs for %s. Where are you located? Please only enter city, provience E.g. Montreal, Quebec' % _type)

    return HttpResponse(str(r))
Example #5
0
def map_data(request):
    req = json.loads(request.raw_post_data)
    start = int(req.get("start", 0))
    end = int(req.get("end", time.time()))
    words = set(req.get("words", "").split(","))
    if "" in words:
        words.remove("")

    if abs(time.time() - end) < 24 * 3600:
        end = time.time() + 3600

    if abs(time.time() - start) < 24 * 3600:
        start = start - 24 * 3600

    _types = req.get("types", None)
    condition = {"loc": {"$exists": True}, "created": {"$lte": end, "$gte": start}}
    if words:
        condition["words"] = {"$in": map(lambda word: word.lower().strip(), words)}
    elif _types:
        condition["type"] = {"$in": _types}
    else:
        condition["type"] = {"$exists": 1}

    # this map/reduce groups needs by location
    mapper = Code(
        """
        function() {
            var key = this.type+this.loc_place;
            emit(key, {sum:1, type:this.type, loc:this.loc, loc_place:this.loc_place, country: this.country});
        }
    """
    )

    reducer = Code(
        """
        function(key, values) {
            var sum = 0.0;
            for (var i = 0; i < values.length; i++) {
                sum += values[i].sum;
            }
            return {sum: sum, type: values[0].type, loc: values[0].loc, loc_place: values[0].loc_place, country: values[0].country}
        }
    """
    )

    # this map/reduce picks the highest needs for a location
    mapper2 = Code(
        """
        function() {
            var key = this.value.loc_place;
            emit(this.value.loc_place, this.value);
        }
    """
    )

    reducer2 = Code(
        """
        function(key, values) {
            var max = values[0];
            for (var i=1; i<values.length; ++i) {
                if (max.sum < values[i].sum) {
                    max = values[i];
                } 
            }
            return max;
        }
    """
    )

    tmp = db["needs"].map_reduce(mapper, reducer, "tmp_%s" % uid_gen(), query=condition)
    res = tmp.map_reduce(mapper2, reducer2, "tmp_%s" % uid_gen())
    locs = []
    for doc in res.find():
        locs.append(doc["value"])

    data = {}
    for loc in locs:
        value = int(math.log(loc["sum"], 10)) + 1
        if value > 5:
            value = 5
        loc["value"] = value

        if "type" in loc and loc["type"]:
            _type = loc["type"].title()
        else:
            _type = "Other"
            loc["type"] = "Other"

        if _type not in data:
            data[_type] = list()
        data[_type].append(loc)

    tmp.drop()
    res.drop()

    return HttpResponse(json.dumps(dict(types=get_types(), data=data)), content_type="application/json")