Exemple #1
0
def add_from_pressurenet(request):
    """
    Data is incoming from pressureNET.
    Authenticate and add it to the database.
    """
    start = time.time()
    # get <-> post with urlencode
    result = urllib2.urlopen('http://ec2-174-129-98-143.compute-1.amazonaws.com:8080/BarometerNetworkServer-3.1/BarometerServlet?pndv=buffer')
    content = result.read()
    readings_list = content.split(';')
    count = 0
    for reading in readings_list:
        raw_location_accuracy = 0
        raw_reading_accuracy = 0
        reading_data = reading.split('|')
        if reading_data[0] == '':
            continue
        raw_latitude = float(reading_data[0])
        raw_longitude = float(reading_data[1])
        raw_reading = float(reading_data[2])
        raw_daterecorded = int(float(reading_data[3]))
        raw_tzoffset = int(float(reading_data[4]))
        raw_user_id = reading_data[5]
        raw_sharing = reading_data[6]
        raw_client_key = reading_data[7]
        try:
            raw_location_accuracy = reading_data[8]
            raw_reading_accuracy = reading_data[9]
        except:
            pass
        this_reading = Reading(
            latitude=raw_latitude,
            longitude=raw_longitude,
            reading=raw_reading,
            daterecorded=raw_daterecorded,
            tzoffset=raw_tzoffset,
            user_id=raw_user_id,
            sharing=raw_sharing,
            client_key=raw_client_key,
            location_accuracy=raw_location_accuracy,
            reading_accuracy=raw_reading_accuracy,
        )

        try:
            this_reading.save()
            count += 1
        except:
            continue

    processing_time = time.time() - start
    ReadingSync.objects.create(readings=count, processing_time=processing_time)
    return HttpResponse('okay go, count ' + str(count))
Exemple #2
0
def new_bookmarklet(request):
    """Create new reading from bookmarklet."""
    if request.method == "POST":
        content = request.POST.get("content", "")
        image = request.POST.get("image", "")
        link = request.POST.get("link", "")
        titl = request.POST.get("title", "")[:80]
        if request.user.is_staff:
            user_pk = request.POST.get("user")
            if user_pk:
                try:
                    user = User.objects.get(pk=int(user_pk))
                except ObjectDoesNotExist:
                    user = request.user
            else:
                user = request.user
        else:
            user = request.user
        # if there is a link and title, create reading
        if link and titl:
            try:
                # if reading with link exists, add notes to that reading
                reading = Reading.objects.get(link=link)
                reading_exists = True
            except ObjectDoesNotExist:
                reading_exists = False
                titles = Reading.objects.filter(title=titl)
                # if there is a reading with the title
                if titles:
                    titl = "%s-%s" % (titl, str(titles.count()))
                reading = Reading(image=image, link=link, title=titl, user=user)
                reading.save()
                # create vote for reading
                reading.vote_set.create(user=user, value=1)
            # add tag
            name = request.POST.get("tag_name")
            # if user added tag
            if name:
                try:
                    # check to see if user already tied a tag to this reading
                    existing_tie = reading.tie_set.get(user=user)
                # if user has not added a tag to this reading
                except ObjectDoesNotExist:
                    name = name.lower()
                    pattern = only_letters()
                    # If name contains only letters
                    if re.search(pattern, name):
                        # If name does not contain any banned words
                        blacklist = banned_words()
                        if not re.search(blacklist, name):
                            try:
                                # If tag exists, get tag
                                tag = Tag.objects.get(name=name)
                            except ObjectDoesNotExist:
                                # If tag does not exist, create tag
                                tag = Tag(name=name, user=user)
                                tag.slug = slugify(tag.name)
                                tag.save()
                            tie = user.tie_set.create(reading=reading, tag=tag)
                            # add rep
                            add_rep(request, t=tie)
            # if user did not add a tag, auto tag
            else:
                auto_tag(request, reading)
            # add rep
            add_rep(request, rd=reading)
            # if there is content, create note
            if content.strip():
                note = Note(content=content, reading=reading, user=user)
                note.save()
                # create first vote for note
                user.vote_set.create(note=note, value=1)
                if reading_exists:
                    facebook_graph_add_note(note.user, note.reading)
            if not reading_exists:
                facebook_graph_add_reading(reading.user, reading)
            if request.user.is_staff:
                # auto create votes for reading and reading.notes
                auto_vote(request, reading)
            data = {"success": 1}
            return HttpResponse(json.dumps(data), mimetype="application/json")
    content = request.GET.get("note", "").lstrip(" ")
    link = request.GET.get("link", "")
    # split title
    html_title = split_title(request.GET.get("title", ""))
    if request.user.is_staff:
        if request.user.pk == 2:
            users = admin_david_list()
        else:
            users = admin_user_list()
    else:
        users = []
    d = {"content": content, "link": link, "titl": html_title, "title": "Add Reading", "users": users}
    return render_to_response(
        "readings/new_bookmarklet.html", add_csrf(request, d), context_instance=RequestContext(request)
    )
Exemple #3
0
        )
        article = soup.select('#dede_content > div')
        max_cnt -= 1
        time.sleep(random.uniform(1.5, 2.5))
    if (not head) or len(head) == 0:
        print("{}=>{}获取失败".format(index, eid))
        fail.append(index)
    else:
        # 处理标题
        head = re.sub(r'</{0,1}\w.*?>', "", str(head[0]))
        with open("../media/readings/" + eid, 'w', encoding='utf8') as f:
            # 处理文章的逐段内容,并写入文件
            for d in article:
                # 匹配HTML标签并替换空串以将其删除
                d = re.sub(r'(<a href="#_w_\d+">\d+</a>)|(</{0,1}\w.*?>)', "",
                           str(d))
                # 去除首尾空白(因为可能有大量空白)
                d = d.strip()
                if len(d) > 0:
                    f.write(d)
                    f.write("\n\n")
        # 持久化到数据库
        reading = Reading()
        reading.name = head[:30]
        reading.content = "readings/" + eid
        reading.source_id = 4
        reading.save()
        print("[写入]{}=>{}".format(index, eid) + "=>" + head)

print(fail)