twitter-snap 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # Getting Started with Create React App
2
+
3
+ This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
4
+
5
+ ## Available Scripts
6
+
7
+ In the project directory, you can run:
8
+
9
+ ### `npm start`
10
+
11
+ Runs the app in the development mode.\
12
+ Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
13
+
14
+ The page will reload if you make edits.\
15
+ You will also see any lint errors in the console.
16
+
17
+ ### `npm test`
18
+
19
+ Launches the test runner in the interactive watch mode.\
20
+ See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
21
+
22
+ ### `npm run build`
23
+
24
+ Builds the app for production to the `build` folder.\
25
+ It correctly bundles React in production mode and optimizes the build for the best performance.
26
+
27
+ The build is minified and the filenames include the hashes.\
28
+ Your app is ready to be deployed!
29
+
30
+ See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
31
+
32
+ ### `npm run eject`
33
+
34
+ **Note: this is a one-way operation. Once you `eject`, you can’t go back!**
35
+
36
+ If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
37
+
38
+ Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
39
+
40
+ You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
41
+
42
+ ## Learn More
43
+
44
+ You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
45
+
46
+ To learn React, check out the [React documentation](https://reactjs.org/).
package/assets/README ADDED
@@ -0,0 +1,11 @@
1
+ This package is part of the noto project. Visit
2
+ google.com/get/noto for more information.
3
+
4
+ Built on 2017-10-24 from the following noto repository:
5
+ -----
6
+ Repo: noto-cjk
7
+ Tag: v2017-06-01-serif-cjk-1-1
8
+ Date: 2017-09-20 09:49:40 GMT
9
+ Commit: 32a5844539f2e348ed36b44e990f9b06d7fb89fe
10
+
11
+ Update serif CJK to 1.1.
package/init.ps1 ADDED
@@ -0,0 +1,12 @@
1
+ aria2c -x4 https://noto-website-2.storage.googleapis.com/pkgs/NotoSansCJKjp-hinted.zip
2
+ Expand-Archive NotoSansCJKjp-hinted.zip -DestinationPath ./assets
3
+ rm NotoSansCJKjp-hinted.zip
4
+ rm assets/NotoSansCJKjp-Black.otf
5
+ # rm assets/NotoSansCJKjp-Bold.otf
6
+ rm assets/NotoSansCJKjp-DemiLight.otf
7
+ rm assets/NotoSansCJKjp-Light.otf
8
+ rm assets/NotoSansCJKjp-Medium.otf
9
+ # rm assets/NotoSansCJKjp-Regular.otf
10
+ rm assets/NotoSansCJKjp-Thin.otf
11
+ rm assets/NotoSansMonoCJKjp-Bold.otf
12
+ rm assets/NotoSansMonoCJKjp-Regular.otf
package/output.png ADDED
Binary file
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "twitter-snap",
3
+ "version": "0.0.1",
4
+ "scripts": {
5
+ "dev:cli": "npx tsx src/cli.ts 1720560121485595093",
6
+ "dev:style": "react-scripts start",
7
+ "start": "node dist/cli.js 1720560121485595093",
8
+ "build": "npx tsc && npx cpx2 assets/* dist/assets"
9
+ },
10
+ "bin": {
11
+ "twitter-snap": "cli.js"
12
+ },
13
+ "dependencies": {
14
+ "@napi-rs/image": "^1.7.0",
15
+ "commander": "^11.1.0",
16
+ "satori": "^0.10.9",
17
+ "twitter-openapi-typescript": "^0.0.19",
18
+ "typescript": "^4.9.5"
19
+ },
20
+ "devDependencies": {
21
+ "@types/jest": "^27.5.2",
22
+ "@types/node": "^16.18.60",
23
+ "@types/react": "^18.2.34",
24
+ "@types/react-dom": "^18.0.11",
25
+ "react": "^18.2.0",
26
+ "react-dom": "^18.2.0",
27
+ "react-scripts": "^5.0.1"
28
+ },
29
+ "eslintConfig": {
30
+ "extends": [
31
+ "react-app",
32
+ "react-app/jest"
33
+ ]
34
+ },
35
+ "browserslist": {
36
+ "production": [
37
+ ">0.2%",
38
+ "not dead",
39
+ "not op_mini all"
40
+ ],
41
+ "development": [
42
+ "last 1 chrome version",
43
+ "last 1 firefox version",
44
+ "last 1 safari version"
45
+ ]
46
+ }
47
+ }
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html lang="ja">
3
+
4
+ <head>
5
+ <meta charset="UTF-8">
6
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
7
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
8
+ <title>Document</title>
9
+ </head>
10
+
11
+ <body style="margin: 0;">
12
+ <div id="root"></div>
13
+ </body>
14
+
15
+ </html>
package/src/cli.ts ADDED
@@ -0,0 +1,30 @@
1
+ import { TwitterSnap } from "./core/twitterSnap";
2
+ import { Command } from "commander";
3
+
4
+ const version = "0.0.1";
5
+ const font = "assets/NotoSansCJKjp-Regular.otf";
6
+ const boldFont = "assets/NotoSansCJKjp-Bold.otf";
7
+
8
+ const program = new Command();
9
+ program.name("twitter-snap").version(version);
10
+
11
+ program
12
+ .argument("<string>", "tweet url or tweet id")
13
+ .option("-o, --output <path>", "output file path", "output.png")
14
+ .option("-w, --width <number>", "image width", "600")
15
+ .option("-h, --height <number>", "image height", "400")
16
+ .option("--font <path>", "font path", font)
17
+ .option("--bold-font <path>", "bold font path", boldFont)
18
+ .action(async (text, { output, width, height, font, boldFont }) => {
19
+ const id = isNaN(text) ? text.split("/").pop() : text;
20
+ const twitterSnap = new TwitterSnap({
21
+ width: parseInt(width),
22
+ height: parseInt(height),
23
+ font: font,
24
+ boldFont: boldFont,
25
+ });
26
+ const writer = await twitterSnap.render(id);
27
+ await writer.save(output);
28
+ process.exit(0);
29
+ });
30
+ program.parse(process.argv);
@@ -0,0 +1,183 @@
1
+ import satori, { SatoriOptions } from "satori";
2
+ import { Transformer } from "@napi-rs/image";
3
+ import Normal from "./../style/normal";
4
+ import * as fs from "fs";
5
+
6
+ import { TwitterOpenApi } from "twitter-openapi-typescript";
7
+
8
+ export type StyleComponent = (props: {
9
+ name: string;
10
+ id: string;
11
+ icon: string;
12
+ text: string;
13
+ }) => React.ReactElement;
14
+
15
+ type TwitterSnapParams = {
16
+ width: number;
17
+ height: number;
18
+ font: string;
19
+ boldFont: string;
20
+ lang?: string;
21
+ };
22
+
23
+ export class TwitterSnap {
24
+ width!: number;
25
+ height!: number;
26
+ option!: SatoriOptions;
27
+
28
+ constructor(param: TwitterSnapParams) {
29
+ this.width = param.width;
30
+ this.height = param.height;
31
+
32
+ this.option = {
33
+ width: this.width,
34
+ height: this.height,
35
+ fonts: [
36
+ {
37
+ name: "Regular",
38
+ data: fs.readFileSync(param.font),
39
+ weight: 400,
40
+ style: "normal",
41
+ lang: param.lang,
42
+ },
43
+ {
44
+ name: "Bold",
45
+ data: fs.readFileSync(param.boldFont),
46
+ weight: 700,
47
+ style: "normal",
48
+ lang: param.lang,
49
+ },
50
+ ],
51
+ };
52
+ }
53
+
54
+ render = async (id: string) => {
55
+ const client = await new TwitterOpenApi().getGuestClient();
56
+ const tweet = await client.getDefaultApi().getTweetResultByRestId({
57
+ tweetId: id,
58
+ });
59
+ const data = {
60
+ name: tweet!.data!.user!.legacy.name,
61
+ id: tweet!.data!.user!.legacy.screenName,
62
+ icon: tweet.data!.user.legacy.profileImageUrlHttps,
63
+ text: tweet.data!.tweet.legacy!.fullText,
64
+ };
65
+
66
+ const svg = await satori(Normal(data), this.option);
67
+ return new TwitterSnapWriter({
68
+ width: this.width,
69
+ height: this.height,
70
+ svg: svg,
71
+ });
72
+ };
73
+ }
74
+
75
+ type TwitterSnapWriterParams = {
76
+ width: number;
77
+ height: number;
78
+ svg: string;
79
+ };
80
+
81
+ export class TwitterSnapWriter {
82
+ width!: number;
83
+ height!: number;
84
+ data!: string;
85
+ _trasformer: Transformer | undefined;
86
+
87
+ constructor(param: TwitterSnapWriterParams) {
88
+ this.width = param.width;
89
+ this.height = param.height;
90
+ this.data = param.svg;
91
+ }
92
+
93
+ get trasformer() {
94
+ if (!this._trasformer) {
95
+ this._trasformer = Transformer.fromSvg(this.data);
96
+ this._trasformer.crop(0, 0, this.width, this.height);
97
+ }
98
+ return this._trasformer;
99
+ }
100
+
101
+ save = async (output: string) => {
102
+ const ext = output.split(".").pop();
103
+ const exts: {
104
+ [key: string]: (output: string) => Promise<void>;
105
+ } = {
106
+ raw: this.raw,
107
+ svg: this.svg,
108
+ png: this.png,
109
+ jpeg: this.jpeg,
110
+ jpg: this.jpeg,
111
+ jpe: this.jpeg,
112
+ bmp: this.bmp,
113
+ dib: this.bmp,
114
+ rle: this.bmp,
115
+ "2bp": this.bmp,
116
+ "2pbb": this.bmp,
117
+ ico: this.ico,
118
+ cur: this.ico,
119
+ tif: this.tiff,
120
+ webp: this.webp,
121
+ avif: this.avif,
122
+ pnm: this.pnm,
123
+ tga: this.tga,
124
+ tpic: this.tga,
125
+ ff: this.farbfeld,
126
+ };
127
+ const method = exts[ext!];
128
+ if (!method) {
129
+ throw new Error("Unsupported file format");
130
+ }
131
+ await method(output);
132
+ };
133
+
134
+ svg = async (output: string) => {
135
+ await fs.promises.writeFile(output, this.data);
136
+ };
137
+ raw = async (output: string) => {
138
+ await fs.promises.writeFile(output, this.data);
139
+ };
140
+ jpeg = async (output: string) => {
141
+ const data = await this.trasformer.jpeg();
142
+ await fs.promises.writeFile(output, data);
143
+ };
144
+
145
+ png = async (output: string) => {
146
+ const data = await this.trasformer.png();
147
+ await fs.promises.writeFile(output, data);
148
+ };
149
+
150
+ bmp = async (output: string) => {
151
+ const data = await this.trasformer.bmp();
152
+ await fs.promises.writeFile(output, data);
153
+ };
154
+
155
+ ico = async (output: string) => {
156
+ const data = await this.trasformer.ico();
157
+ await fs.promises.writeFile(output, data);
158
+ };
159
+ tiff = async (output: string) => {
160
+ const data = await this.trasformer.tiff();
161
+ await fs.promises.writeFile(output, data);
162
+ };
163
+ webp = async (output: string) => {
164
+ const data = await this.trasformer.webp();
165
+ await fs.promises.writeFile(output, data);
166
+ };
167
+ avif = async (output: string) => {
168
+ const data = await this.trasformer.avif();
169
+ await fs.promises.writeFile(output, data);
170
+ };
171
+ pnm = async (output: string) => {
172
+ const data = await this.trasformer.pnm();
173
+ await fs.promises.writeFile(output, data);
174
+ };
175
+ tga = async (output: string) => {
176
+ const data = await this.trasformer.tga();
177
+ await fs.promises.writeFile(output, data);
178
+ };
179
+ farbfeld = async (output: string) => {
180
+ const data = await this.trasformer.farbfeld();
181
+ await fs.promises.writeFile(output, data);
182
+ };
183
+ }
package/src/index.tsx ADDED
@@ -0,0 +1,12 @@
1
+ import React from "react";
2
+ import ReactDOM from "react-dom/client";
3
+ import Normal from "./style/normal";
4
+ import example from "./utils/example";
5
+
6
+ ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
7
+ <React.StrictMode>
8
+ <div style={{ width: "100svw", height: "100vh" }}>
9
+ <Normal {...example} />
10
+ </div>
11
+ </React.StrictMode>
12
+ );
@@ -0,0 +1,84 @@
1
+ import { StyleComponent } from "./../core/twitterSnap";
2
+
3
+ const Normal: StyleComponent = ({ name, id, icon, text }) => {
4
+ return (
5
+ <div
6
+ style={{
7
+ display: "flex",
8
+ justifyContent: "center",
9
+ alignItems: "center",
10
+ width: "100%",
11
+ height: "100%",
12
+ background:
13
+ "linear-gradient(-45deg, #0077F2ee 0%, #1DA1F2ee 50%,#4CFFE2ee 100%)",
14
+ }}
15
+ >
16
+ <div
17
+ style={{
18
+ width: "550px",
19
+ background: "white",
20
+ display: "flex",
21
+ flexDirection: "column",
22
+ borderRadius: "10px",
23
+ padding: "12px",
24
+ }}
25
+ >
26
+ <div
27
+ style={{
28
+ display: "flex",
29
+ }}
30
+ >
31
+ <img
32
+ alt="icon"
33
+ style={{
34
+ width: "40px",
35
+ height: "40px",
36
+ borderRadius: "50%",
37
+ marginRight: "12px",
38
+ }}
39
+ src={icon}
40
+ />
41
+ <div
42
+ style={{
43
+ display: "flex",
44
+ flexDirection: "column",
45
+ }}
46
+ >
47
+ <p
48
+ style={{
49
+ fontSize: "15px",
50
+ fontWeight: "bold",
51
+ margin: "0px",
52
+ }}
53
+ >
54
+ {name}
55
+ </p>
56
+ <p
57
+ style={{
58
+ fontSize: "15px",
59
+ margin: "0px",
60
+ color: "#536471",
61
+ }}
62
+ >
63
+ {id}
64
+ </p>
65
+ </div>
66
+ </div>
67
+ {text.split("\n").map((t, key) => (
68
+ <p
69
+ key={key}
70
+ style={{
71
+ fontSize: "17px",
72
+ margin: "0px",
73
+ marginTop: "12px",
74
+ }}
75
+ >
76
+ {t}
77
+ </p>
78
+ ))}
79
+ </div>
80
+ </div>
81
+ );
82
+ };
83
+
84
+ export default Normal;
@@ -0,0 +1,8 @@
1
+ const example = {
2
+ name: "Twitter",
3
+ id: "@Twitter",
4
+ icon: "https://abs.twimg.com/icons/apple-touch-icon-192x192.png",
5
+ text: "aaaaa",
6
+ };
7
+
8
+ export default example;
package/tsconfig.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es5",
4
+ "lib": ["dom", "dom.iterable", "esnext"],
5
+ "allowSyntheticDefaultImports": true,
6
+ "strict": true,
7
+ "module": "commonjs",
8
+ "moduleResolution": "node",
9
+ "declaration": true,
10
+ "declarationMap": true,
11
+ "sourceMap": true,
12
+ "jsx": "react-jsx",
13
+ "outDir": "dist",
14
+ "baseUrl": "./src"
15
+ },
16
+ "include": ["src"]
17
+ }