Skip to content
This repository has been archived by the owner on Jun 10, 2022. It is now read-only.

Commit

Permalink
generating rss of recent firstFT articles
Browse files Browse the repository at this point in the history
  • Loading branch information
railsagainstignorance committed Feb 27, 2017
1 parent 4c1ef03 commit 4970a1d
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 40 deletions.
113 changes: 113 additions & 0 deletions firstft/firstft.go
@@ -0,0 +1,113 @@
package firstft

import (
"fmt"
"os"
// "regexp"
// "strings"
"time"
// "encoding/json"
"github.com/joho/godotenv"
. "github.com/gorilla/feeds"
"github.com/railsagainstignorance/alignment/content"
)

func getEnvParam(key string, defaultValue string) string {
godotenv.Load()
value := os.Getenv(key)

if value == "" {
value = defaultValue
}

return value
}

func getFirstFTArticles(maxArticles int) *[]*content.Article {

sRequest := &content.SearchRequest{
QueryType: "brand",
QueryText: "FirstFT",
MaxArticles: maxArticles,
MaxDurationMillis: 1000,
SearchOnly: false, // i.e. return full article details too from CAPI
}

fmt.Println("getFirstFTArticles: sRequest=", sRequest, ", maxArticles=", maxArticles)

sapiResult := content.Search(sRequest)

articles := []*content.Article {}

for i, article := range *(sapiResult.Articles) {
fmt.Println("getFirstFTArticles: ", i, ") ", article.Title)
articles = append( articles, article )
}

return &articles
}

// type (content.)Article struct {
// SiteUrl string
// Uuid string
// Title string
// Author string
// Excerpt string
// Body string
// PubDateString string
// PubDate *time.Time
// ImageUrl string
// ImageWidth int
// ImageHeight int
// NonPromoImageUrl string
// NonPromoImageWidth int
// NonPromoImageHeight int
// PromoImageUrl string
// PromoImageWidth int
// PromoImageHeight int
// PullQuoteAssets *[]PullQuoteAsset
// }

func articlesToRss(articles *[]*content.Article) *string {
const siteUrl = "http://www.ft.com/"
now := time.Now()

feed := &Feed{
Title: "Articles for narration from the Financial Times: FT Labs experiment",
Link: &Link{Href: siteUrl},
Description: "Exploring the text to speech possibilities.",
Author: &Author{Name: "Chris Gathercole", Email: "chris.gathercole@ft.com"},
Created: now,
Copyright: "This work is copyright © Financial Times",
}

feed.Items = []*Item{}

for _, article := range *articles {

created, _ := time.Parse("2006-01-02", article.PubDateString)

feed.Items = append(feed.Items, &Item{
Title: article.Title,
Link: &Link{Href: article.SiteUrl},
Description: article.Body,
Created: created,
Id: article.Uuid,
})

}

rss, _ := feed.ToRss()
return &rss
}

func GenerateRss(maxArticles int) *string {
articles := getFirstFTArticles( maxArticles )
rssString := articlesToRss( articles )
return rssString
}

func main() {
godotenv.Load()
// ...
}
83 changes: 45 additions & 38 deletions templates/align.html
Expand Up @@ -36,18 +36,18 @@ <h2>align on matching phrase</h2>
<tr>
<td><input type="text" name="text" value="has its own"></td>
<td>source:</td>
<td><input type="radio" name="source" value="any" checked> any
<td><input type="radio" name="source" value="any" checked> any
<br><input type="radio" name="source" value="title-only"> title-only</td>
<td><input type="submit" value="search and align on matching phrase"></td>
</tr>
</table>

</form>

<h2>align an article's sentences matching meter</h2>
<p>
You identify an article (with a UUID from the url, by hand, sorry) and specify a meter.
<br>If any of the sentences within that article body contain text conforming to that meter, they are aligned accordingly.
You identify an article (with a UUID from the url, by hand, sorry) and specify a meter.
<br>If any of the sentences within that article body contain text conforming to that meter, they are aligned accordingly.
<br>Meter, you ask? See <a href="http://examples.yourdictionary.com/examples-of-iambic-pentameter.html">the internet</a> for some examples.
<br>You specify the meter as a sequence of 0s and 1s, where 1 is the emphasised beat.
<br>You can anchor the meter to the ^start or end$ of the text, or leave it free to match anywhere.
Expand All @@ -56,21 +56,21 @@ <h2>align an article's sentences matching meter</h2>
<table>
<tr>
<td style="text-align:right;">
<input type="hidden" name="ontology" value="id">
<br>uuid&nbsp;<input type="text" name="value" value="b57fee24-cb3c-11e5-be0b-b7ece4e953a0">
<input type="hidden" name="ontology" value="id">
<br>uuid&nbsp;<input type="text" name="value" value="b57fee24-cb3c-11e5-be0b-b7ece4e953a0">
<br>meter&nbsp;<input type="text" name="meter" value="01010101">
</td>
<td>
<br><input type="submit" value="get article and align on matching sentences">
</td>
</tr>
</table>
</table>
</form>

<h2>find author's recent articles' phrases matching the meter</h2>
<p>
You identify an author and specify a meter.
<br>If any of the sentences within recent articles by that author contain text conforming to that meter, they are aligned accordingly.
You identify an author and specify a meter.
<br>If any of the sentences within recent articles by that author contain text conforming to that meter, they are aligned accordingly.
<br>Meter, you ask? See <a href="http://examples.yourdictionary.com/examples-of-iambic-pentameter.html">the internet</a> for some examples.
<br>You specify the meter as a sequence of 0s and 1s, where 1 is the emphasised beat.
<br>You can anchor the meter to the ^start or end$ of the text, or leave it free to match anywhere.
Expand All @@ -79,20 +79,20 @@ <h2>find author's recent articles' phrases matching the meter</h2>
<table>
<tr>
<td style="text-align:right;">
<input type="hidden" name="ontology" value="authors">
<br>author&nbsp;<input type="text" name="value" value="Lucy Kellaway">
<input type="hidden" name="ontology" value="authors">
<br>author&nbsp;<input type="text" name="value" value="Lucy Kellaway">
<br>meter&nbsp;<input type="text" name="meter" value="0101010101">
</td>
<td>max&nbsp;<input type="text" name="max" value="10">
<br><input type="submit" value="search for articles and align on matching meter"> (NB: there will be a bit of a delay)</td>
</tr>
</table>
</table>
</form>

<h2>find author's recent articles' phrases matching the haiku meter, 5-7-5</h2>
<p>
You identify an author.
<br>If any of the sentences within recent articles by that author contain text conforming to the haiku meter, 5-7-5, they are displayed.
You identify an author.
<br>If any of the sentences within recent articles by that author contain text conforming to the haiku meter, 5-7-5, they are displayed.
<br>Meter, you ask? See <a href="http://examples.yourdictionary.com/examples-of-iambic-pentameter.html">the internet</a> for some examples.
<br>You specify the meter as a sequence of 0s and 1s, where 1 is the emphasised beat.
<br>You can anchor the meter to the ^start or end$ of the text, or leave it free to match anywhere.
Expand All @@ -101,20 +101,20 @@ <h2>find author's recent articles' phrases matching the haiku meter, 5-7-5</h2>
<table>
<tr>
<td style="text-align:right;">
<input type="hidden" name="ontology" value="authors">
<br>author&nbsp;<input type="text" name="value" value="Lucy Kellaway">
<input type="hidden" name="ontology" value="authors">
<br>author&nbsp;<input type="text" name="value" value="Lucy Kellaway">
<input type="hidden" name="meter" value="..... ....... .....">
</td>
<td>max&nbsp;<input type="text" name="max" value="10">
<br><input type="submit" value="search for articles and align on matching meter"> (NB: there will be a bit of a delay)</td>
</tr>
</table>
</table>
</form>

<h2>find ontology's recent articles' phrases matching the meter</h2>
<p>
You identify an ontology and value, and a meter.
<br>If any of the sentences within the relevant recent articles contain text conforming to that meter, they are aligned accordingly.
You identify an ontology and value, and a meter.
<br>If any of the sentences within the relevant recent articles contain text conforming to that meter, they are aligned accordingly.
<br>Meter, you ask? See <a href="http://examples.yourdictionary.com/examples-of-iambic-pentameter.html">the internet</a> for some examples.
<br>You specify the meter as a sequence of 0s and 1s, where 1 is the emphasised beat.
<br>You can anchor the meter to the ^start or end$ of the text, or leave it free to match anywhere.
Expand All @@ -123,20 +123,20 @@ <h2>find ontology's recent articles' phrases matching the meter</h2>
<table>
<tr>
<td style="text-align:right;">
ontology&nbsp;<input type="text" name="ontology" value="brand">
<br>value&nbsp;<input type="text" name="value" value="Lunch with the FT">
ontology&nbsp;<input type="text" name="ontology" value="brand">
<br>value&nbsp;<input type="text" name="value" value="Lunch with the FT">
<br>meter&nbsp;<input type="text" name="meter" value="0101010101">
</td>
<td>max&nbsp;<input type="text" name="max" value="10">
<br><input type="submit" value="search for articles and align on matching meter"> (NB: there will be a bit of a delay)</td>
</tr>
</table>
</table>
</form>

<h2>find ontology's recent articles' phrases matching the haiku meter, 5-7-5</h2>
<p>
You specify an ontology and value.
<br>If any of the sentences within the relevant recent articles contain text conforming to the haiku meter, 5-7-5, they are displayed.
You specify an ontology and value.
<br>If any of the sentences within the relevant recent articles contain text conforming to the haiku meter, 5-7-5, they are displayed.
<br>Meter, you ask? See <a href="http://examples.yourdictionary.com/examples-of-iambic-pentameter.html">the internet</a> for some examples.
<br>You specify the meter as a sequence of 0s and 1s, where 1 is the emphasised beat.
<br>You can anchor the meter to the ^start or end$ of the text, or leave it free to match anywhere.
Expand All @@ -145,20 +145,20 @@ <h2>find ontology's recent articles' phrases matching the haiku meter, 5-7-5</h2
<table>
<tr>
<td style="text-align:right;">
ontology&nbsp;<input type="text" name="ontology" value="brand">
<br>value&nbsp;<input type="text" name="value" value="Lunch with the FT">
ontology&nbsp;<input type="text" name="ontology" value="brand">
<br>value&nbsp;<input type="text" name="value" value="Lunch with the FT">
<br><input type="hidden" name="meter" value="..... ....... .....">
</td>
<td>max&nbsp;<input type="text" name="max" value="10">
<br><input type="submit" value="search for articles and align on matching meter"> (NB: there will be a bit of a delay)</td>
</tr>
</table>
</table>
</form>

<h2>find page's current articles' phrases matching the haiku meter, 5-7-5</h2>
<p>
You specify a sitepage.
<br>If any of the sentences within the relevant articles contain text conforming to the haiku meter, 5-7-5, they are displayed.
You specify a sitepage.
<br>If any of the sentences within the relevant articles contain text conforming to the haiku meter, 5-7-5, they are displayed.
<br>Meter, you ask? See <a href="http://examples.yourdictionary.com/examples-of-iambic-pentameter.html">the internet</a> for some examples.
<br>You specify the meter as a sequence of 0s and 1s, where 1 is the emphasised beat.
<br>You can anchor the meter to the ^start or end$ of the text, or leave it free to match anywhere.
Expand All @@ -167,18 +167,18 @@ <h2>find page's current articles' phrases matching the haiku meter, 5-7-5</h2>
<table>
<tr>
<td style="text-align:right;">
<input type="hidden" name="ontology" value="pages">
page&nbsp;<input type="text" name="value" value="http://www.ft.com/home/uk">
<input type="hidden" name="ontology" value="pages">
page&nbsp;<input type="text" name="value" value="http://www.ft.com/home/uk">
<br><input type="hidden" name="meter" value="..... ....... .....">
</td>
<td>
<input type="hidden" name="max" value="20">
<input type="submit" value="search for the page's articles and align on matching meter"> (NB: there will be a bit of a delay)
</td>
</tr>
</table>
</table>
</form>

<h2>for articles before the date, find phrases matching the haiku meter, 5-7-5</h2>
<p>
You identify an date.
Expand All @@ -203,10 +203,10 @@ <h2>for articles before the date, find phrases matching the haiku meter, 5-7-5</

<h2>explore phrases matching meter</h2>
<p>
You specify a phrase and a meter.
You specify a phrase and a meter.
<br>Details extracted from the phrase and the meter are displayed, mostly for debugging purposes,
<br>but it is interesting to see how the words are fragmented into phonemes and syllables.
<br>If any parts of the phrase contain text conforming to that meter, they are aligned accordingly.
<br>If any parts of the phrase contain text conforming to that meter, they are aligned accordingly.
<br>Meter, you ask? See <a href="http://examples.yourdictionary.com/examples-of-iambic-pentameter.html">the internet</a> for some examples.
<br>You specify the meter as a sequence of 0s and 1s, where 1 is the emphasised beat.
<br>You can anchor the meter to the ^start or end$ of the text, or leave it free to match anywhere.
Expand All @@ -217,7 +217,7 @@ <h2>explore phrases matching meter</h2>
<li>Its a bug - in which case, feel free to let me know</li>
<li>The phonemes are based on American pronounciation, and they do speak funny. So, sorry, but, shrug.</li>
<li>The phonemes' emphasis points are perhaps too rigid to allow sufficient slack for the word to be spoken in a poetic context. Again, sorry, shrug.</li>
</ol>
</ol>
</p>
<form action="/detail" method="GET">
<table>
Expand All @@ -228,19 +228,26 @@ <h2>explore phrases matching meter</h2>
</td>
<td><input type="submit" value="find meter within the phrase"></td>
</tr>
</table>
</table>
</form>

<h2>Caveats</h2>
<p>
<ul>
<li>It has very little error handling</li>
<li>It can not work for an excitingly wide variety of reasons.
<li>It can not work for an excitingly wide variety of reasons.
<li>Perhaps the main reason for flakiness is that this is MyFirstGoProgram.</li>
</ul>
... so choose wisely.
</p>

<hr>
<h2>extras</h2>
<ul>
<li><a href="/pullquotes/rss">/pullquotes/rss</a></li>
<li><a href="/pullquotes/json">/pullquotes/json</a></li>
<li><a href="/firstft/rss">/firstft/rss</a></li>
</ul>
<hr>
<h2>Simply interesting</h2>
<ul>
Expand Down
21 changes: 19 additions & 2 deletions web-server.go
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/railsagainstignorance/alignment/rhyme"
"github.com/railsagainstignorance/alignment/rss"
"github.com/railsagainstignorance/alignment/pullquotes"
"github.com/railsagainstignorance/alignment/firstft"
"html/template"
"net/http"
"os"
Expand Down Expand Up @@ -141,7 +142,7 @@ func rssHandler(w http.ResponseWriter, r *http.Request) {
rssText := rss.Generate(maxItems)
w.Header().Set("Content-Type", "application/rss+xml")
w.Header().Set("Access-Control-Allow-Origin", "*")

fmt.Fprintf(w, *rssText)
}

Expand All @@ -166,6 +167,21 @@ func meditationHandler(w http.ResponseWriter, r *http.Request) {
templateExecuter(w, "meditationPage", nil)
}

func firstftRssHandler(w http.ResponseWriter, r *http.Request) {

maxArticles := 2
if r.FormValue("max") != "" {
i, err := strconv.Atoi(r.FormValue("max"))
if err == nil {
maxArticles = i
}
}

rssText := firstft.GenerateRss( maxArticles )
w.Header().Set("Content-Type", "application/rss+xml")
fmt.Fprintf(w, *rssText)
}

func log(fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
fmt.Println("REQUEST URL: ", r.URL)
Expand All @@ -189,7 +205,8 @@ func main() {
http.HandleFunc("/meditation", log(meditationHandler))
http.HandleFunc("/pullquotes/rss", log(pullquotesRssHandler))
http.HandleFunc("/pullquotes/json", log(pullquotesJsonHandler))

http.HandleFunc("/firstft/rss", log(firstftRssHandler))

http.Handle("/javascript/", http.StripPrefix("/javascript/", http.FileServer(http.Dir("./public/javascript"))))
http.Handle("/data/", http.StripPrefix("/data/", http.FileServer(http.Dir("./public/data"))))

Expand Down

0 comments on commit 4970a1d

Please sign in to comment.