Featured image of post 【Telegraph】搭建静态博客/图床

【Telegraph】搭建静态博客/图床

这是简介

前排提示,本文使用方法搭建出来的静态博客需要科学上网才能访问。

开始

我们先来看看 Telegraph 是个啥子东东,这是官网的介绍

Telegraph 并不强调内容管理方这一概念,真正做到了「人人都是媒体」。通过 Telegraph 发布的文章,理论上来说不会存在删除的危险,并且由于会产生一个独立的网址链接,所以我们发布的内容可以完全公开在万维网上。我们可以随时随地、匿名发布任何消息,一切不会受到任何监管。「匿名、不受监管」,这应该就是 Telegraph 所强调的自由。

简单来说,只要进入 https://telegra.ph 并编辑本章,点击 Publish ,你的文章就会被匿名地公开到万维网,并且长期保存。 Telegraph 支持上传图片、MarkDown格式。这些操作并不需要你注册 Telegram 账户。

值得注意的是,一旦浏览器清空缓存后,文章将不可再编辑。

接口发布

博文

发布文章的方式一共有两种,一种就是上面提到的直接在网站中编辑内容。另一种就是通过接口的方式发布。

接口文档:https://telegra.ph/api

创建文章接口

参数名 类型 是否必须
access_token String Required
title String Required
author_name String Optional
author_url String Optional
content Array of Node Required
return_content Boolean, default = false Optional

其中 access_token 通过 createAccount 接口注册获得。

author_url 可以指向任何外链。

content 中的 Node 为类似 HTML 的结构实体组成,如下:

public class HtmlNode {
    private String tag;
    private Map<String,String> attrs;
    private List<Object> children;
}

构造 Map ,发送 Http 请求

public String publicBlog(List<String> imgSrc, String topic) {
        List<HtmlNode> nodes = new ArrayList<>();
        HtmlNode imgNode = new HtmlNode();
        Map<String, Object> map = new HashMap<>();

        for (String s : imgSrc) {
            Map<String, String> attrsMap = new HashMap<>();
            imgNode.setTag("img");
            attrsMap.put("src", s);
            imgNode.setAttrs(attrsMap);
            if (s.contains(".jpg")) nodes.add(imgNode);
        }

        HtmlNode topicNode = new HtmlNode();
        topicNode.setTag("h3");
        List<String> topicChild = new ArrayList<>();
        topicChild.add(topic);
        topicNode.setChildren(Arrays.asList(topicChild.toArray()));
        nodes.add(topicNode);

        map.put("access_token", "1sad2910nc90cn13caw7d2e211dcsa212a");
        map.put("title", "PH Second Blog Page");
        map.put("author_name", "Philomel");
        map.put("author_url", "https://t.me/xxx");
        map.put("content", JSON.toJSONString(nodes));
        map.put("return_content", false);
        String resp = HttpRequest.post("https://api.telegra.ph/createPage")
                .useProxy(PROXY_IP, PROXY_PORT)
                .form(map)
                .body();
        return JSON.parseObject(resp).getJSONObject("result").get("url").toString();
    }

接下来访问回调参数中的 url 即可。

图床

图床接口使用:https://telegra.ph/upload

直接构造一个带有 file 的 Http请求 即可:

public String uploadTgraph(File file,Proxy proxy) {
  return HttpUtil.createPost("https://telegra.ph/upload")
  			.setProxy(proxy)
            		.form("file", file)
                    	.execute()
                        .body();

从回调参数获取 url

文章示例

BlogPage