def scrape(request): """Scrape url for all images.""" url = request.GET.get("url") pattern = re.compile(r"^http[\W\w]+.(biz|com|co|edu|gov|info|net|org)+") result = re.search(pattern, url) if result: host = url[result.start() : result.end()] else: host = "" req = urllib2.Request(url, headers={"User-Agent": "Magic Browser"}) con = urllib2.urlopen(req) html = con.read() soup = BeautifulSoup(html) all_imgs = soup.find_all("img") imgs = [] for img in all_imgs: src = img.get("src") if src: # does the src url have a relative path match = re.search(pattern, src) if not match: # if so, add the host in front of the src path src = host + src imgs.append(src) # split title html_title = split_title(soup.find("title").string) t = loader.get_template("readings/images.html") c = Context({"imgs": imgs}) data = {"imgs": t.render(c), "html_title": html_title} return HttpResponse(json.dumps(data), mimetype="application/json")
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) )