Created
September 26, 2017 05:21
-
-
Save hondajojo/d9425cb2394a882da7d567f2ec802cad to your computer and use it in GitHub Desktop.
简书rss
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"time" | |
"fmt" | |
"crypto/md5" | |
"encoding/hex" | |
"net/url" | |
"net/http" | |
"io/ioutil" | |
"encoding/json" | |
"github.com/gorilla/feeds" | |
) | |
type Item struct { | |
Id int `json:"id"` | |
Title string `json:"title"` | |
Slug string `json:"slug"` | |
Desc string `json:"desc"` | |
LastCompiledAt int `json:"last_compiled_at"` | |
ListImage string `json:"list_image"` | |
Notebook NoteBookItem `json:"notebook"` | |
} | |
type NoteBookItem struct { | |
User UserItem `json:"user"` | |
} | |
type UserItem struct { | |
NickName string `json:"nickname"` | |
} | |
func getDataNew(userid string) []byte { | |
md5String, timestampStr := generateMd5() | |
params := map[string]string{ | |
"order_by": "shared_at", | |
"app[name]": "haruki", | |
"app[version]": "1.9.1", | |
"device[guid]": "867979022057373", | |
"count": "15", | |
} | |
paramsValues := url.Values{} | |
for paramsKey, paramsValue := range params { | |
paramsValues.Add(paramsKey, paramsValue) | |
} | |
baseUrl := fmt.Sprintf("http://api.jianshu.io/v2/users/%s/notes?", userid) | |
finalUrl := baseUrl + paramsValues.Encode() | |
request, err := http.NewRequest("GET", finalUrl, nil) | |
if err != nil { | |
fmt.Println(err) | |
} | |
request.Header.Set("X-Auth-1", md5String) | |
request.Header.Set("X-Timestamp", timestampStr) | |
request.Header.Set("User-Agent", "Dalvik/2.1.0 (Linux; U; Android 7.1.2; Nexus 6P Build/N2G47O)") | |
client := &http.Client{} | |
resp, err := client.Do(request) | |
if err != nil { | |
panic(err) | |
} | |
defer resp.Body.Close() | |
body, _ := ioutil.ReadAll(resp.Body) | |
return body | |
} | |
func web(w http.ResponseWriter, r *http.Request) { | |
userid := r.URL.Query().Get("userid") | |
if userid == "" { | |
w.WriteHeader(http.StatusNotFound) | |
w.Write([]byte("缺少用户id")) | |
return | |
} | |
body := getDataNew(userid) | |
rss, err := generateRSS(body) | |
if err != nil { | |
w.WriteHeader(http.StatusNotFound) | |
w.Write([]byte("error")) | |
} | |
w.Header().Set("Content-Type", "application/rss+xml; charset=utf-8") | |
w.Write([]byte(rss)) | |
} | |
func main() { | |
http.HandleFunc("/", web) | |
http.ListenAndServe(":8002", nil) | |
} | |
func generateMd5() (string, string) { | |
timestamp := time.Now().Unix() | |
t := fmt.Sprintf("5c7ade4876ed26f839357948c5cc8051%d", timestamp) | |
h := md5.New() | |
h.Write([]byte(t)) | |
timestampStr := fmt.Sprintf("%d", timestamp) | |
return hex.EncodeToString(h.Sum(nil)), timestampStr | |
} | |
func generateRSS(body []byte) (string, error) { | |
now := time.Now() | |
//fmt.Println(now.String()) | |
//fmt.Println(now.Format("2006-01-02 15:04:05")) | |
local, _ := time.LoadLocation("Asia/Chongqing") | |
var item []Item | |
err := json.Unmarshal(body, &item) | |
if err != nil { | |
return "", err | |
} | |
author := item[0].Notebook.User.NickName | |
feed := &feeds.Feed{ | |
Title: fmt.Sprintf("简书-%s", author), | |
Link: &feeds.Link{Href: "http://www.jianshu.com"}, | |
Created: now.In(local), | |
Author: &feeds.Author{Name: author}, | |
} | |
feed.Items = []*feeds.Item{} | |
for _, v := range item { | |
t := time.Unix(int64(v.LastCompiledAt), 0) | |
local, _ := time.LoadLocation("Asia/Chongqing") | |
var img string | |
if v.ListImage != "" { | |
img = fmt.Sprintf("<img src='%s' />", v.ListImage) | |
} else { | |
img = "" | |
} | |
desc := v.Desc + img | |
a := &feeds.Item{ | |
Title: v.Title, | |
Link: &feeds.Link{Href: fmt.Sprintf("http://www.jianshu.com/p/%s", v.Slug)}, | |
Description: desc, | |
Author: &feeds.Author{Name: v.Notebook.User.NickName}, | |
Created: t.In(local), | |
} | |
feed.Items = append(feed.Items, a) | |
} | |
rss, err := feed.ToRss() | |
if err != nil { | |
return "", err | |
} | |
return rss, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment