Esempio n. 1
0
def new_story(request,
              text,
              type="system",
              entry=None,
              project=None,
              person=None):
    if not project:
        project = entry.project
    if not person:
        person = request.person
    story = models.Story(text=text,
                         entry=entry,
                         project=project,
                         person=person,
                         type=type)
    if hasattr(request,
               "person"):  # Sometimes stories are generated by system.
        story.created_by = request.person
    elif person:
        story.created_by = person
    story.put()
    if story.entry:
        story.entry.mailed = False
        story.entry.calendar_synced = False
        story.entry.put()
    if story.project:
        story.project.mailed = False
        story.project.put()
Esempio n. 2
0
def create_story():
    if not session['is_logged_in']:
        return redirect('/login')
    user = models.User.objects.get(username=session['username'])
    if request.method == 'GET':
        # show them the story creation form
    else:
        # save the information to the database
        title = request.form.get('title', None)
        if title is None:
            flash.message = "Title is required"
            return redirect('/story/create')
        story = models.Story()
        story.title = title
        story.genre = request.form.get('genre', None)
        story.summary = request.form.get('summary', None)
        story.status = request.form.get('status', None)
        wordcount_text = request.form.get('wordcount', None)
        if wordcount_text is not None:
            wc_entry = models.WordCountEntry()
            wc_entry.wordcount = int(wordcount_text)
            story.wordcounts.append(wc_entry)
        story.save()
        user.stories.append(story)
        user.save()
        # send the user to the story page
        return redirect('/story/%s' % story.id)
Esempio n. 3
0
def _importStoryFromAE(request, response, url, publication, overwrite=False):
    logging.info('trying to get story from: {0}'.format(url))

    r1 = requests.get(url)
    r1.encoding = 'UTF-8'
    soup = BeautifulSoup(r1.text, "html5lib")

    title = soup.find('title').get_text().strip()
    creator = [soup.find("span", class_="author").get_text().strip()]

    s = None
    q = m.Story.query().filter(m.Story.title == title,
                               m.Story.firstPub.publication == publication)
    existingStory = q.get()

    if not existingStory or overwrite:
        #TODO - filter out image and separate out author info (last p, after hr)
        storyText = ''
        textContainer = soup.find('td', class_='mainarticle')

        for t in textContainer.find_all(
            ['p', 'h1', 'h2', 'h3', 'center', '<blockquote>']):
            storyText += t

        wordCount = len(
            storyText.replace('<p>', '').replace('</p>', '').split(None))

        firstPub = m.FirstPub(url=url, publication=publication, comments=0)
        s = m.Story(category='fiction',
                    genre='sci-fi',
                    language='english',
                    title=title,
                    creator=creator,
                    text=storyText,
                    wordCount=wordCount,
                    firstPub=firstPub)
    else:
        response.out.write(
            'skipping story, already exists: {0} - {1} <br/>'.format(
                title, publication))
    return s
Esempio n. 4
0
    def parseMdToStory(self, storyfile, rank):
        # TODO: handle bad inputs gracefully.

        story_obj = models.Story()

        story_obj.rank = rank

        lines = []
        with open(storyfile) as f:
            for line in f:
                lines.append(line.rstrip())

        line_num = 0

        # title
        for i in range(len(lines)):
            line = lines[i]
            if "===" not in line:
                story_obj.title += ' '.join(line.split())
            else:
                line_num = i + 1
                break

        # various tags
        for i in range(line_num, len(lines)):
            line = lines[i]
            if line:
                line_split = line.split()

                # @tags
                if line_split[0][0] == "@":
                    # author
                    if line_split[0] == "@author" and len(line_split) > 1:
                        story_obj.byline = ' '.join(line_split[1:])
                    # date
                    elif line_split[0] == "@date" and len(line_split) > 1:
                        if line_split[1] == "today":
                            story_obj.story_date = timezone.now()
                        else:
                            date_list = line_split[1:]
                            naive_date_str = ' '.join(date_list[:-1])
                            tz = date_list[-1]
                            naive_date = datetime.datetime.strptime(
                                naive_date_str, "%a, %d %b %Y %H:%M:%S")
                            new_date_str = "{}-{}-{}T{}:{}:{}{}".format(
                                naive_date.year, naive_date.month,
                                naive_date.day, naive_date.hour,
                                naive_date.minute, naive_date.second, tz)
                            story_obj.story_date = parse_datetime(new_date_str)
                    # outlet
                    elif line_split[0] == "@outlet" and len(line_split) > 1:
                        story_obj.outlet = ' '.join(line_split[1:])
                    # local images
                    elif line_split[0] == "@localimages" and len(
                            line_split) > 1:
                        imgs = ''.join(line_split[1:]).split(',')
                        story_obj.images = json.dumps(imgs)
                    # Django tags
                    elif line_split[0] == "@djangotags" and len(
                            line_split) > 1:
                        django_tags = ''.join(line_split[1:]).split(',')

                # [[[ teaser ]]]
                elif line_split[0][0:3] == "[[[":
                    if ' '.join(line_split).count("]]]") > 0:
                        line_text = ' '.join(line_split).split("[[[",
                                                               1)[1].split(
                                                                   "]]]", 1)[0]
                        #story_obj.teaser = markdown.markdown(line_text)
                        story_obj.teaser = line_text
                    else:
                        teaser = ' '.join(line_split).split("[[[", 1)[1]
                        for j in range(i + 1, len(lines)):
                            teaser_line = lines[j]
                            if teaser_line.count("]]]") > 0:
                                teaser = ' '.join(
                                    [teaser,
                                     teaser_line.split("]]]", 1)[0]])
                                #story_obj.teaser = markdown.markdown(teaser)
                                story_obj.teaser = teaser
                                line_num = j + 1
                                break
                            else:
                                teaser = ' '.join([teaser, teaser_line])

        # main text
        djtags = ["{{% load {} %}}".format(t) for t in django_tags]
        story_obj.text = os.linesep.join(djtags)
        #story_obj.text += markdown.markdown(os.linesep.join(lines[line_num:]))
        story_obj.text += os.linesep.join(lines[line_num:])

        # slug
        def isSlugChar(c):
            try:
                return c in string.letters or c in string.whitespace or int(
                    c) in range(10)
            except ValueError:
                pass
            return False

        slug_chars = ''.join([c for c in story_obj.title if isSlugChar(c)])
        story_obj.slug = '-'.join(slug_chars.split()).lower()

        return story_obj
Esempio n. 5
0
File: bot.py Progetto: teiram/z5bot
    log_dialog(message, out_message)


if __name__ == '__main__':
    with open('config.json', 'r') as f:
        config = json.load(f)

    api_key = config['api_key']
    logging.info('Logging in with api key %r.' % api_key)
    if len(sys.argv) > 1:
        logging.info('Broadcasting is available! Send /broadcast.')

    for story in config['stories']:
        models.Story(name=story['name'],
                     abbrev=story['abbrev'],
                     filename=story['filename'])

    z5bot = models.Z5Bot.get_instance_or_create()
    z5bot.set_cwd(pathlib.Path.cwd())

    p = parser.Parser()
    p.add_default(cmd_default)
    p.add_command('/start', cmd_start)
    p.add_command('/select', cmd_select)
    p.add_command('/load', cmd_load)
    p.add_command('/save', cmd_save)
    p.add_command('/clear', cmd_clear)
    p.add_command('/enter', cmd_enter)
    p.add_command('/i', cmd_ignore)
    p.add_command('/ping', cmd_ping)
Esempio n. 6
0
  def parseJsonToStoryList(self, json_obj):
    stories = json_obj['list']['story']
    story_objects = []
    for s in stories:
      story_obj = models.Story()
      try:
        title = s['title']['$text']
        story_obj.title = u"{}".format(title)
        story_obj.teaser = s['teaser']['$text']
        story_obj.creation_date = timezone.now()

        date_list = s['storyDate']['$text'].split()
        naive_date_str = ' '.join(date_list[:-1])
        tz = date_list[-1]
        naive_date = datetime.datetime.strptime(
            naive_date_str,
            "%a, %d %b %Y %H:%M:%S")
        new_date_str = "{}-{}-{}T{}:{}:{}{}".format(
            naive_date.year, naive_date.month, naive_date.day,
            naive_date.hour, naive_date.minute, naive_date.second, tz)
        story_obj.story_date = parse_datetime(new_date_str)

        try:
          story_obj.outlet = s['outlet']
        except KeyError:
          story_obj.outlet = "NPR"

        s_paragraphs = []
        for paragraph in s['textWithHtml']['paragraph']:
          s_paragraphs.append(u"<p>{}</p>".format(paragraph['$text']))
        story_obj.text = ' '.join(s_paragraphs)

      except KeyError as e:
        continue

      try:
        story_obj.byline = json.dumps(s['byline'])
      except KeyError as e:
        story_obj.byline = None

      try:
        for img in s['image']:
          new_img = models.Image()
          try:
            new_img.src = img['src']
            for crop in img['crop']:
              if crop['type'] == 'standard':
                new_img.crop_standard_src = crop['src']
              elif crop['type'] == 'square':
                new_img.crop_square_src = crop['src']
              elif crop['type'] == 'wide':
                new_img.crop_wide_src = crop['src']
              elif crop['type'] == 'enlargement':
                new_img.crop_enlargement_src = crop['src']
              elif crop['type'] == 'custom':
                new_img.crop_custom_src = crop['src']
            try:
              new_img.caption = img['caption']['$text']
              new_img.provider = img['provider']['$text']
              new_img.producer = img['producer']['$text']
              new_img.copyright = img['copyright']
            except KeyError as e:
              pass
          except KeyError as e:
            continue
          else:
            story_obj.images.append(new_img)
      except KeyError as e:
        story_obj.images = []

      try:
        for quote in s['pullQuote']:
          new_quote = models.PullQuote()
          try:
            new_quote.text = quote['text']['$text']
            new_quote.person = quote['person']['$text']
          except KeyError as e:
            continue
          else:
            story_obj.pull_quotes.append(new_quote)
      except KeyError as e:
        story_obj.pull_quotes = []
        
      story_objects.append(story_obj)

    return story_objects