Пример #1
0
    def post(self):
        import os, static, hashlib
        from models import StoryAuthor, Story, StoryDocument
        from django.template.defaultfilters import slugify
        from google.appengine.runtime.apiproxy_errors import RequestTooLargeError
        
        title = self.request.get('title')
        content = self.request.get('content')
        full_name = self.request.get('full_name')
        mobile_number = self.request.get('mobile_number')
        email = self.request.get('email')

        author = StoryAuthor(full_name=full_name, email=email, mobile_number=mobile_number)
        author.put()
    
        story = Story(title=title)
        if content:
            story.content = content
        story.author = author
        story.put()

        try:
            request_document = self.request.get('document')
            if request_document:
                document_file = self.request.POST['document']        
                document_body = document_file.value
                document_digest = hashlib.sha1(document_body).hexdigest()
                split_name = os.path.splitext(os.path.basename(document_file.filename))
                filename = slugify(split_name[0]) or document_digest
                document_name = filename + split_name[1]
        
                document_path = '/story/%d/document/%s/%s' % (story.key().id(), document_digest, document_name)
                logging.info(document_path)
                story_document = StoryDocument(story=story, path=document_path, name=document_name)
                story_document.put()
                document = static.set(document_path, document_body, document_file.type, name=document_name)
                self.render_to_response("thanks/story.html", document=story_document, story=story)
            else:
                self.render_to_response("thanks/story.html", story=story)
                
        except RequestTooLargeError, message:
            from api_preferences import facebook as fb_prefs, google_friend_connect as gfc
            self.render_to_response("start.html",
                                       FACEBOOK_API_KEY=fb_prefs.get('api_key'),
                                       FACEBOOK_CROSS_DOMAIN_RECEIVER_URL=fb_prefs.get('cross_domain_receiver_url'),
                                       GOOGLE_FRIEND_CONNECT_SITE_ID=gfc.get('site_id'),
                                       request_too_large_error=True,
                                       title=title,
                                       content=content,
                                       full_name=full_name,
                                       email=email,
                                       mobile_number=mobile_number)
Пример #2
0
def create_story(request):
	token = getToken(request.META)
	user = getUser(token)

	if(not user):
		return Response(status=status.HTTP_403_FORBIDDEN)

	serialized = StorySerializer(data=request.DATA)

	if(serialized.is_valid):
		story = Story()
		story.title = serialized.initial_data['title']
		story.content = serialized.initial_data['content']
		story.save()

		return Response(status=status.HTTP_201_CREATED)
	else:
		return Response(serialized._errors, status=status.HTTP_400_BAD_REQUEST)
def newspaperize(article_url):
    """Takes a string url that contains an article. Returns a Story object from 
    models.py containing information scraped from the article located at the url."""

    article = Article(article_url)  # create Article object

    print("Downloading:", article_url)

    try:  # returns None if url fails to download
        article.download()
    except:
        print("Failed to download url:", article_url)
        return None

    try:  # returns None if url cannot be parsed
        article.parse()
    except:
        print("Failed to parse url:", article_url)
        return None

    article.nlp()

    # variables to hold values for Story attributes
    headline = article.title
    imageurl = article.top_image
    timestamp = article.publish_date
    content = article.text
    keywords = article.keywords
    summary = article.summary
    description = article.meta_description
    clickbait = -1  # placeholder for clickbait label

    # populates keyword object with article.keywords
    list_of_keyword_obj = []
    for word in keywords:
        if word not in stopword:  # prevents stopwords from being keywords
            k = Keyword()
            k.keyword = word
            list_of_keyword_obj.append(k)

    s = Story()  # create Story object

    # set attributes
    s.name = headline
    s.imageurl = imageurl
    s.url = article_url
    current_time = datetime.datetime.now()

    if timestamp is not None:
        s.timestamp = timestamp.isoformat()
    else:  # generate timestamp if none found
        s.timestamp = current_time

    s.description = description
    s.keywords = list_of_keyword_obj
    s.summary = summary
    s.content = content
    s.clickbait = clickbait
    s.createtime = current_time

    return s