Skip to content

Instantly share code, notes, and snippets.

@Hasnayeen
Last active April 15, 2018 08:20
Show Gist options
  • Save Hasnayeen/4e7b41926d8379b0edf3107895412af3 to your computer and use it in GitHub Desktop.
Save Hasnayeen/4e7b41926d8379b0edf3107895412af3 to your computer and use it in GitHub Desktop.
Get Medium Posts via AWS lambda
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
type Response struct {
Payload struct {
References struct {
Post map[string]Postdata
} `json:"references"`
} `json:"payload"`
}
type Postdata struct {
ID string `json:"id"`
Title string `json:"title"`
FirstPublishedAt int64 `json:"firstPublishedAt"`
UniqueSlug string `json:"uniqueSlug"`
}
func handler(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
fmt.Printf("Processing request data for request %s.\n", request.RequestContext.RequestID)
resp, err := http.Get("https://medium.com/@searching.nehal/latest?format=json")
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
body, err2 := ioutil.ReadAll(resp.Body)
if err2 != nil {
fmt.Println(err2)
}
data := string(body)
convertedData := strings.Replace(data, "])}while(1);</x>", "", 1)
var response Response
json.Unmarshal([]byte(convertedData), &response)
result, _ := json.Marshal(response.Payload.References.Post)
return events.APIGatewayProxyResponse{Body: string(result), StatusCode: 200}, nil
}
func main() {
lambda.Start(handler)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment