Beispiel #1
0
def get_stories(request, offset):
    # We do this stupid offset thing because web requests to heroku timeout after 30s...
    # So essentially we just hit /getstories 5 times, grabbing 100 stories each time
    offset = int(offset) * 100
    top_stories = requests.get("https://hacker-news.firebaseio.com/v0/topstories.json").json()[offset: offset + 100]

    start_time = time.time()

    # Figure out which stories we don't have saved in our database yet
    new_story_ids = []
    for story_id in top_stories:
        if not Story.objects.filter(story_id=story_id).exists():
            new_story_ids.append(story_id)

    # The fun part: asynchronously hit HN api for all the info we need using grequests
    # This is ~5x faster than synchronous requests for big updates
    urls = ["https://hacker-news.firebaseio.com/v0/item/{0}.json".format(story_id) for story_id in new_story_ids]
    new_rs = (grequests.get(u, timeout=10) for u in urls)
    new_resps = grequests.map(new_rs)

    # Store all of our new stories in the database
    for resp in new_resps:
        Story.create_from_story_resp(resp)

    # Go through every organization and figure out which stories they haven't seen yet
    for posting_settings in Organization.objects.filter(uninstalled=False):
        posted = post_stories(top_stories, posting_settings)

        if posted and offset == 400:
            settings_link = request.build_absolute_uri(reverse('change_settings', args=[posting_settings.webhook_url_end]))

            resp = requests.post(posting_settings.webhook_url, data=json.dumps(
                {'text': "_<{0}|You can change your article keywords here.>_".format(settings_link)}))

    return HttpResponse("Found new stories in {0} seconds!".format(str(time.time() - start_time)))
Beispiel #2
0
def story_meta(filename, path):
  print "story_meta"
  notebook_name = path.split("/")[-1][:-9]
  dom = parse(path+"/"+filename)
  resource = dom.getElementsByTagName("resource")[0]
  story_title = resource.getElementsByTagName("dc:title")[0].firstChild.nodeValue
  raw_contrib = [x.firstChild.nodeValue
      for x in resource.getElementsByTagName("dcterms:contributor") ]
  contrib = json.dumps(raw_contrib)
  required = resource.getElementsByTagName("dcterms:requires")
  pages = len(required)
  try:
    date = resource.getElementsByTagName("dcterms:created")[0].firstChild.nodeValue
  except AttributeError:
    date = ""
  try:
    description = resource.getElementsByTagName("dc:description")[0].firstChild.nodeValue
  except AttributeError:
    description = ""
  try:
    comments = resource.getElementsByTagName("bl:comments")[0].firstChild.nodeValue
  except AttributeError:
    comments = ""
  raw_subject = [sub.firstChild.nodeValue for sub in resource.getElementsByTagName("dc:subject")]
  subject = json.dumps(raw_subject)
  raw_keywords = []
  try:
    temp = dom.getElementsByTagName("bl:keywords")[0]
    for word in temp.getElementsByTagName("bl:keyword"):
	k = word.getElementsByTagName("bl:kw")[0].firstChild.nodeValue
	subkw = word.getElementsByTagName("bl:subkeywords")[0]
	v = ", ".join(sub.firstChild.nodeValue
		for sub in subkw.getElementsByTagName("bl:subkw"))
	raw_keywords.append((k, v))
  except AttributeError:
    pass
  keyword = json.dumps(raw_keywords)
  notebook = Notebook.objects.get(short_title=notebook_name)
  
  story = Story(notebook=notebook, title=story_title, created=date, description=description, 
		comment=comments, contributor=contrib, subject=subject, keyword=keyword, pages=pages )
  story.save()
  i=1
  for page in required:
    page_path = page.firstChild.nodeValue
    page_uuid = uuid.uuid4()
    page_entry = Page(story = story, filename = page_path, uuid = page_uuid,number=i)
    i+=1
    page_entry.save()
def create_story(request):
    if request.method == 'GET':
        story_name = request.GET.get('story_name')
        description = request.GET.get('description')
        if story_name and description:
            story = Story()
            story.story_name = story_name
            story.description = description
            story.author = request.user
            story.save()
            story_id = Story.objects.all().values('id').order_by(
                'id').last()['id']
            return redirect('story_card',
                            story_name=story_name,
                            description=description,
                            story_id=story_id)
Beispiel #4
0
 def post(self, request, format=None):
     Story.create(request.data['type'], request.data['title'], request.data['content'], request.data['longitude'], request.data['latitude'], request.data['tagList'], request.data['originalPoster'])
     return Response('This is a POST request')
Beispiel #5
0
for i in range(len(story)):
    line = story[i]
    if line[0] != tab or i == len(story) - 1:
        if i == len(story) - 1: i += 1  #this is to get the last line in
        countForPrompts += 1
        if (countForPrompts - 1) % 3 == 0 and countForPrompts != 1:
            promptTexts.append(story[previousPromptIndex:i])
            previousPromptIndex = i

#print(promptTexts)

#first parse the first chunk
prompt, oldOptions = parsePrompt(promptTexts[0])

#then create a Story
story = Story(firstPrompt=prompt, vars=csvVars, name=storyName)
story.save()

for promptText in promptTexts[1:]:
    prompt, newOptions = parsePrompt(promptText)
    for option in oldOptions:
        option.nextPrompt = prompt
        option.save()
    oldOptions = newOptions
"""words to use:
tab = \t
vars = pet, food, drink
你好, I want to 买 a pet, but I won't have much time or 钱 to take care of it. Can you help me?
	1. 买狗!
	Hmmm, a 狗?Okay..
	pet := 狗