Skip to content

Commit 4b7a163

Browse files
authored
Merge pull request #29 from scramjetorg/fix/bring-back-samples
Bring back deleted samples
2 parents 90d7ecd + be3dff0 commit 4b7a163

File tree

13 files changed

+748
-0
lines changed

13 files changed

+748
-0
lines changed

samples/hello/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
## hello 🙋‍♂️
2+
3+
----
4+
Sequence that modifies incoming stream of strings by saying Hello :).
5+
6+
> :bulb: **Please note that the sample below requires some previous installations before you start running it, you will find them [here](../../README.md#3-install-scramjet-transform-hub).**
7+
8+
### Running
9+
10+
Open three terminals and run the following commands:
11+
12+
**The first terminal:**
13+
14+
```bash
15+
# start sth
16+
scramjet-transform-hub
17+
```
18+
19+
**The second terminal**
20+
21+
```bash
22+
# go to 'hello' directory
23+
cd samples/hello
24+
25+
# install dependencies
26+
npm install
27+
28+
# make a compressed package with sequence
29+
si pack . -o hello.tar.gz
30+
31+
# send sequence to transform hub, this will output Sequence ID
32+
si seq send hello.tar.gz
33+
34+
# start a sequence, this will output Instance ID
35+
si seq start <sequence-id>
36+
37+
# See output of instance process
38+
si inst output <instance-id>
39+
```
40+
41+
**The third terminal**
42+
43+
```bash
44+
# Send file to instance input steam
45+
si inst input <instance-id> name.txt
46+
# if file not given the data will be read from stdin
47+
```
48+
49+
<!-- TODO Delete when the issue is solved
50+
Issue created for reading data from stdin https://github.com/scramjetorg/transform-hub/issues/165 -------ISSUE SOLVED!!! awaits release
51+
-->
52+
53+
### Output
54+
55+
```bash
56+
# Now you should see "Hello John" in output console
57+
$ si inst output 7a1ffd59-9d1a-4e8f-a246-020124803931
58+
Request ok: http://127.0.0.1:8000/api/v1/instance/7a1ffd59-9d1a-4e8f-a246-020124803931/output status: 200 OK
59+
Hello John
60+
```

samples/hello/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const { Transform } = require("stream");
2+
3+
module.exports = function(input) {
4+
return input.pipe(new Transform({
5+
encoding: "utf-8",
6+
transform(chunk, _encoding, callback) {
7+
callback(null, `Hello ${chunk}`);
8+
}
9+
}));
10+
};

samples/hello/name.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
John

samples/hello/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "@scramjet/hello",
3+
"version": "0.13.1",
4+
"description":"Sequence that modifies incoming stream of strings by saying Hello",
5+
"main": "./index",
6+
"author": "Scramjet <[email protected]>",
7+
"license": "GPL-3.0",
8+
"repository": {
9+
"type": "git",
10+
"url": "https://github.com/scramjetorg/transform-hub.git"
11+
},
12+
"assets": ["name.txt"]
13+
}

samples/scraping/README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
## scraping
2+
3+
---
4+
This is a simple and trivial example of scraping web pages.
5+
6+
The scraper takes URL and CSS ID selector as input parameters and returns data every second.
7+
8+
To test this please use URL: <https://www.timeanddate.com/worldclock/poland> and ID: `#ct`. Scraper will connect to the website and read (scrap) the current time. Next, it returns this as a stream.
9+
As URL and ID are parametrized we can use other websites too. For example, URL: <https://time.is/> and ID: `#clock`
10+
11+
> :bulb: **Please note that the sample below requires some previous installations before you start running it, you will find them [here](../../README.md#3-install-scramjet-transform-hub).**
12+
13+
### Running
14+
15+
Open two terminals and run the following commands:
16+
17+
**The first terminal:**
18+
19+
```bash
20+
# start sth
21+
scramjet-transform-hub
22+
```
23+
24+
**The second terminal**
25+
26+
```bash
27+
# go to 'scraping' directory
28+
cd samples/scraping
29+
30+
# install dependencies
31+
npm install
32+
33+
# transpile TS->JS to dist/
34+
npm run build
35+
36+
# prepare standalone JS package
37+
cp -r node_modules package.json dist/
38+
39+
# make a compressed package with sequence
40+
si pack dist
41+
42+
# send sequence to transform hub, this will output Sequence ID
43+
si seq send dist.tar.gz
44+
45+
# start a sequence, this will output Instance ID. As the CSS ID has # (hash) sign surround it with quotes:
46+
si seq start <sequence-id> https://www.timeanddate.com/worldclock/poland '#ct'
47+
48+
# See output
49+
si inst output <instance-id>
50+
51+
# Optional commands below:
52+
53+
# Check console.log messages
54+
si inst stdout <instance-id>
55+
56+
# Check console.error messages
57+
si inst stderr <instance-id>
58+
```
59+
60+
### Output
61+
62+
```bash
63+
$ si instance output 41783884-2e97-4b78-9639-aac5d7ff8447
64+
Request ok: http://127.0.0.1:8000/api/v1/instance/41783884-2e97-4b78-9639-aac5d7ff8447/output status: 200 OK
65+
13:06:10
66+
13:06:15
67+
13:06:20
68+
13:06:25
69+
13:06:31
70+
13:06:36
71+
13:06:41
72+
13:06:46
73+
13:06:51
74+
13:06:56
75+
(...)
76+
```

samples/scraping/app.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ReadableApp } from "@scramjet/types";
2+
import { scrape } from './scrape'
3+
import { sleep } from './utils'
4+
5+
const app: ReadableApp<string, [string, string]> = async function* (
6+
_stream,
7+
url,
8+
selectorStr,
9+
interval = 5000,
10+
) {
11+
while(true) {
12+
const timer = sleep(interval);
13+
yield await scrape(url, selectorStr) + '\n'
14+
15+
await timer;
16+
}
17+
}
18+
19+
export default app;

samples/scraping/package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "@scramjet/scraping",
3+
"version": "0.13.1",
4+
"description": "This is a simple and trivial example of scraping web pages.",
5+
"main": "app.js",
6+
"scripts": {
7+
"build": "tsc -p tsconfig.json"
8+
},
9+
"author": "Scramjet <[email protected]>",
10+
"license": "GPL-3.0",
11+
"repository": {
12+
"type": "git",
13+
"url": "https://github.com/scramjetorg/transform-hub.git"
14+
},
15+
"dependencies": {
16+
"axios": "^0.21.4",
17+
"cheerio": "*",
18+
"scramjet": "^4.36.0"
19+
},
20+
"devDependencies": {
21+
"@scramjet/types": "^0.13.1",
22+
"@types/node": "15.12.5",
23+
"typescript": "^4.3.4"
24+
}
25+
}

samples/scraping/scrape.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as cheerio from 'cheerio';
2+
import axios from 'axios';
3+
4+
export async function scrape(url: string, selectorStr: string) {
5+
const html = (await axios.get(url)).data;
6+
const $ = cheerio.load(html);
7+
const selector = $(selectorStr);
8+
9+
return selector.text();
10+
}

samples/scraping/test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { scrap } from './scrap';
2+
3+
const BASE_URL = 'https://time.is/';
4+
5+
(async () => {
6+
const result = await scrap(BASE_URL, "#clock");
7+
console.log(`RESULT: ${result}`);
8+
})();

samples/scraping/tsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"outDir": "./dist",
4+
"esModuleInterop": true,
5+
"allowSyntheticDefaultImports": true
6+
},
7+
"include": [
8+
"app.ts"
9+
],
10+
}

0 commit comments

Comments
 (0)