示例#1
0
文件: views.py 项目: fhim50/smsAPI
def sms(request):
    """
    Handles both the GET and the POST
    
    first thing is checks to make sure that the incoming message
    has the right secret device key
    
    POST:
    use the post data to create a SMS, and add it to the database
    will return empty 200 if success, or 500/400 with an {'error': <error message>} json body
    
    GET:
    gets up to max_sms sms, and returns them in a json list
    as well as a sms_count
    """   
    
    attrs = ('to_number','from_number','body')
    
    if request.method == "POST":
        """
        Handles an incoming SMS
        """
        device = authorize(request.POST.get('key'))
        if device is None:
            return HttpResponseForbidden(str(device))
        
        sms_dict = {}
        for attr in attrs:
            post_val = request.POST.get(attr)
            if post_val is None:
                return HttpResponseBadRequest("POST must have attribute %s" % attr)
            sms_dict[attr] = post_val
        new_sms = SMS(**sms_dict)
        
        sms_handlers = []
        sms_handler_tuple = getattr(settings,'SMS_HANDLERS',[])
        for sms_handler_string in sms_handler_tuple:
            sms_handlers.append(get_callable(sms_handler_string))
        
        # call the handlers? is this the best way?
        for sms_handler in sms_handlers:
            retval = sms_handler(new_sms)
            if retval is False:
                break
                
        return HttpResponse()
        
    elif request.method == "GET":
        """
        Remove this section if you will not be using
        The database as a queue for SMS sending-consumers
        """        
        device = authorize(request.GET.get('key'))
        if device is None:
            return HttpResponseForbidden(str(device))
        
        try:
            return dj_simple_sms.SMS_SENDER.respond_to_get(request)
        except NotImplementedError:
            return HttpResponseNotAllowed('GET')
示例#2
0
    count_exceptions = 0
    for log in reddit.subreddit(SUBREDDIT).mod.log(limit=10000000):
        if count_exceptions > 10:
            print('too many exceptions, terminating')
            return
        mod = log.mod
        if mod.name == 'AutoModerator':
            continue
        action_created = dt.datetime.fromtimestamp(log.created_utc)
        action = log.action
        if action in COMMENT_ACTIONS:
            cid = log.target_fullname.split('_')[-1]
            try:
                persist_comment(reddit, log, cid)
            except Exception as e:
                count_exceptions += 1
                print('mod action already in db')
                print(e)
        elif action in LINK_ACTIONS:
            try:
                lid = log.target_fullname.split('_')[-1]
                persist_link(reddit, log, lid)
            except Exception as e:
                print('mod action already in db')
                print(e)


if __name__ == "__main__":
    reddit_all = authorize()
    persist_modlog(reddit_all)
示例#3
0
def sms(request):
    """
    Handles both the get and the post
    
    first thing is checks to make sure that the incoming message
    has the right secret device key
    
    POST:
    use the post data to create a SMS, and add it to the database
    will return empty 200 if success, or 500/400 with an {'error': <error message>} json body
    
    GET:
    gets up to max_sms sms, and returns them in a json list
    as well as a sms_count
    """   
    
    attrs = ('to_number','from_number','body')
    
    if request.method == "POST":
        
        device = authorize(request.POST.get('key'))
        if device is None:
            return HttpResponseForbidden()
        
        sms_dict = {}
        for attr in attrs:
            
            post_val = request.POST.get(attr)
            if post_val is None:
                return HttpResponseBadRequest("POST must have attribute %s" % attr)
            
            sms_dict[attr] = post_val
        
        new_sms = SMS(**sms_dict)
        
        sms_handlers = []
        sms_handler_tuple = getattr(settings,'SMS_HANDLERS',[])
        for sms_handler_string in sms_handler_tuple:
            sms_handlers.append(get_callable(sms_handler_string))
        
        # call the handlers? is this the best way?
        for sms_handler in sms_handlers:
            retval = sms_handler(new_sms)
            if retval is False:
                break
                
        return HttpResponse()
        
    elif request.method == "GET":
        """
        Remove this section if you will not be using
        The database as a queue for SMS sending-consumers
        """
        
        
        device = authorize(request.GET.get('key'))
        if device is None:
            return HttpResponseForbidden()
        
        max_sms = request.GET.get('max_sms',getattr(settings,'SMS_MAX_SMS_GET',10))
        
        # ok, get that many!
        if max_sms is None:
            sms_set = SMS.objects.all().order_by('datetime')
        else:
            sms_set = SMS.objects.all().order_by('datetime')[:max_sms]

            
        sms_list = list(sms_set.values(*attrs))
        
        count = len(sms_list)
        
        data_out = {'sms_count':count,'sms':sms_list}
        
        for sms in sms_set:
            sms.delete()
        
        return HttpResponse(json.dumps(data_out))
        
        

        
        
        
示例#4
0
                    'body': comment.body,
                    'edited': comment.edited,
                    'ups': comment.ups,
                    'reports': reports,
                    'author': auth_name,
                    'removal_reason': comment.removal_reason,
                    'comment_obj': comment
                }

            comment_removal_idx[cid]['mod_actions'].append(
                {log.mod.name: (action, action_created)})
    return comment_removal_idx


def get_comment_user_report_stats(reddit):
    comment_actions = get_comment_actions(reddit)
    reason_counts_by_comment = {}
    for cid, action_meta in comment_actions.items():
        for r in action_meta['reports']:
            print(r)
            reason = r[0]
            if reason not in reason_counts_by_comment:
                reason_counts_by_comment[reason] = 0
            reason_counts_by_comment[reason] += 1
    print(reason_counts_by_comment)


if __name__ == "__main__":
    reddit = authorize()
    get_comment_user_report_stats(reddit)
示例#5
0
文件: views.py 项目: dakbill/Proshop
def sms(request):
    """
    Handles both the get and the post
    
    first thing is checks to make sure that the incoming message
    has the right secret device key
    
    POST:
    use the post data to create a SMS, and add it to the database
    will return empty 200 if success, or 500/400 with an {'error': <error message>} json body
    
    GET:
    gets up to max_sms sms, and returns them in a json list
    as well as a sms_count
    """

    attrs = ('to_number', 'from_number', 'body')

    if request.method == "POST":

        device = authorize(request.POST.get('key'))
        if device is None:
            return HttpResponseForbidden()

        sms_dict = {}
        for attr in attrs:

            post_val = request.POST.get(attr)
            if post_val is None:
                return HttpResponseBadRequest("POST must have attribute %s" %
                                              attr)

            sms_dict[attr] = post_val

        new_sms = SMS(**sms_dict)

        sms_handlers = []
        sms_handler_tuple = getattr(settings, 'SMS_HANDLERS', [])
        for sms_handler_string in sms_handler_tuple:
            sms_handlers.append(get_callable(sms_handler_string))

        # call the handlers? is this the best way?
        for sms_handler in sms_handlers:
            retval = sms_handler(new_sms)
            if retval is False:
                break

        return HttpResponse()

    elif request.method == "GET":
        """
        Remove this section if you will not be using
        The database as a queue for SMS sending-consumers
        """

        device = authorize(request.GET.get('key'))
        if device is None:
            return HttpResponseForbidden()

        max_sms = request.GET.get('max_sms',
                                  getattr(settings, 'SMS_MAX_SMS_GET', 10))

        # ok, get that many!
        if max_sms is None:
            sms_set = SMS.objects.all().order_by('datetime')
        else:
            sms_set = SMS.objects.all().order_by('datetime')[:max_sms]

        sms_list = list(sms_set.values(*attrs))

        count = len(sms_list)

        data_out = {'sms_count': count, 'sms': sms_list}

        for sms in sms_set:
            sms.delete()

        return HttpResponse(json.dumps(data_out))
示例#6
0
def oauth_autorization(request):
    http_auth = util.authorize(request.GET['code'])
    request.session['credentials'] = http_auth.to_json()

    return HttpResponseRedirect(reverse('auth:home'))
示例#7
0
文件: views.py 项目: kannor/smsAPI
def sms(request):
    """
    Handles both the GET and the POST
    
    first thing is checks to make sure that the incoming message
    has the right secret device key
    
    POST:
    use the post data to create a SMS, and add it to the database
    will return empty 200 if success, or 500/400 with an {'error': <error message>} json body
    
    GET:
    gets up to max_sms sms, and returns them in a json list
    as well as a sms_count
    """

    attrs = ('to_number', 'from_number', 'body')

    if request.method == "POST":
        """
        Handles an incoming SMS
        """
        device = authorize(request.POST.get('key'))
        if device is None:
            return HttpResponseForbidden(str(device))

        sms_dict = {}
        for attr in attrs:
            post_val = request.POST.get(attr)
            if post_val is None:
                return HttpResponseBadRequest("POST must have attribute %s" %
                                              attr)
            sms_dict[attr] = post_val
        new_sms = SMS(**sms_dict)

        sms_handlers = []
        sms_handler_tuple = getattr(settings, 'SMS_HANDLERS', [])
        for sms_handler_string in sms_handler_tuple:
            sms_handlers.append(get_callable(sms_handler_string))

        # call the handlers? is this the best way?
        for sms_handler in sms_handlers:
            retval = sms_handler(new_sms)
            if retval is False:
                break

        return HttpResponse()

    elif request.method == "GET":
        """
        Remove this section if you will not be using
        The database as a queue for SMS sending-consumers
        """
        device = authorize(request.GET.get('key'))
        if device is None:
            return HttpResponseForbidden(str(device))

        try:
            return dj_simple_sms.SMS_SENDER.respond_to_get(request)
        except NotImplementedError:
            return HttpResponseNotAllowed('GET')