dms-scrape 0.2.1 → 0.3.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/.gitattributes CHANGED
@@ -1,2 +1,2 @@
1
- # Auto detect text files and perform LF normalization
2
- * text=auto
1
+ # Auto detect text files and perform LF normalization
2
+ * text=auto
package/index.mjs CHANGED
@@ -1,9 +1,8 @@
1
- import { fetchAndScrape } from "./lib/fetchAndScrape.mjs";
2
- import { justScrape } from "./lib/justScrape.mjs";
1
+ import { processHTML } from "./lib/processHTML.mjs";
3
2
  export async function dmsScrape(type, link, html) {
4
3
  return type == "link"
5
- ? await fetchAndScrape(link)
4
+ ? processHTML(link, await (await fetch(link)).text())
6
5
  : type == "html"
7
- ? justScrape(link, html)
8
- : "Must specify type";
6
+ ? processHTML(link, html)
7
+ : { error: "Must specify type", url: link };
9
8
  }
@@ -15,13 +15,12 @@ import { XF } from "../sources/T5/XF.mjs";
15
15
  import { TAISOUNDS } from "../sources/T4/TAISOUNDS.mjs";
16
16
  import { TECHNEWS } from "../sources/T5/TECHNEWS.mjs";
17
17
  import { CNA } from "../sources/T4/CNA.mjs";
18
- import { handleUnsupported } from "../sources/handleUnsupported.mjs";
19
- export function determineSource(link) {
20
- // if (link.includes("3c") == undefined) {
21
- // return handleUnsupported;
22
- // } else
23
- if (link.includes("3c.ltn")) {
24
- return LTN3C; // Return the corresponding function
18
+ import { v4 as uuid } from "uuid";
19
+ import { filterContent } from "./filterContent.mjs";
20
+ import { determineCategory } from "./determineCategory.mjs";
21
+ function determineSource(link) {
22
+ if (link.includes("3c.ltn")) {
23
+ return LTN3C;
25
24
  } else if (link.includes("ec.ltn")) {
26
25
  return LTNEC;
27
26
  } else if (link.includes("money.udn")) {
@@ -53,13 +52,32 @@ export function determineSource(link) {
53
52
  } else if (link.includes("cna")) {
54
53
  return CNA;
55
54
  } else {
56
- return handleUnsupported(link);
55
+ return "unsupported";
57
56
  }
58
57
  }
59
58
 
60
- // export function processHTML(link, html) {
61
- // const handlerFunction = determineSource(link);
62
- // return handlerFunction == handleUnsupported
63
- // ? handlerFunction(cheerio.load(html))
64
- // : handlerFunction(link);
65
- // }
59
+ export function processHTML(link, html) {
60
+ const handlerFunction = determineSource(link);
61
+ let scrapedContent;
62
+ handlerFunction != "unsupported"
63
+ ? ((scrapedContent = handlerFunction(cheerio.load(html))),
64
+ (scrapedContent["content"] = scrapedContent["content"]
65
+ ? filterContent(scrapedContent["content"]).join("\n\n")
66
+ : scrapedContent["content"]),
67
+ (scrapedContent["author"] = Array.isArray(scrapedContent["author"])
68
+ ? scrapedContent["author"].toString()
69
+ : scrapedContent["author"]),
70
+ (scrapedContent["date"] = Array.isArray(scrapedContent["date"])
71
+ ? scrapedContent["date"].toString()
72
+ : scrapedContent["date"]),
73
+ (scrapedContent["category"] = determineCategory(scrapedContent["title"])),
74
+ (scrapedContent["url"] = link),
75
+ (scrapedContent["content"] = scrapedContent["content"]))
76
+ : (scrapedContent = {
77
+ error: handlerFunction,
78
+ url: link,
79
+ });
80
+
81
+ scrapedContent["id"] = uuid();
82
+ return scrapedContent
83
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dms-scrape",
3
- "version": "0.2.1",
3
+ "version": "0.3.1",
4
4
  "main": "index.mjs",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -13,6 +13,7 @@
13
13
  "esbuild": "^0.23.1"
14
14
  },
15
15
  "dependencies": {
16
- "cheerio": "^1.0.0"
16
+ "cheerio": "^1.0.0",
17
+ "uuid": "^10.0.0"
17
18
  }
18
19
  }
@@ -23,16 +23,16 @@ export function UDNMoney($) {
23
23
  {
24
24
  selector: "section.article-body__editor p",
25
25
  value: (el, key) => {
26
- const sampleKey = this.findIndex(item => item.includes("延伸閱讀"))
26
+ // const sampleKey = this.findIndex(item => item.includes("延伸閱讀"))
27
27
  if (!$(el).text().trim() == "") {
28
28
  const sample = $(el)
29
29
  .text()
30
30
  .replace(/\$\(.*/s, "")
31
31
  .trim();
32
32
 
33
- if (key < sampleKey) {
33
+ // if (key < sampleKey) {
34
34
  return sample;
35
- }
35
+ // }
36
36
 
37
37
  }
38
38
  },