Camamba Users Search Library

fetches Users

当前为 2022-08-21 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/446634/1083913/Camamba%20Users%20Search%20Library.js

  1. // ==UserScript==
  2. // @name Camamba Users Search Library
  3. // @namespace hoehleg.userscripts.private
  4. // @version 0.0.5
  5. // @description fetches Users
  6. // @author Gerrit Höhle
  7. // @license MIT
  8. //
  9. // @require https://greasyfork.org/scripts/405144-httprequest/code/HttpRequest.js?version=1063408
  10. //
  11. // @grant GM_xmlhttpRequest
  12. // ==/UserScript==
  13.  
  14. // https://greasyfork.org/scripts/446634-camamba-users-search-library/
  15.  
  16. /* jslint esversion: 9 */
  17.  
  18. /**
  19. * @typedef {object} UserParams
  20. * @property {string} name
  21. * @property {uid} [number]
  22. * @property {'male'|'female'|'couple'?} [gender]
  23. * @property {number} [age]
  24. * @property {number} [level]
  25. * @property {number} [longitude]
  26. * @property {number} [latitude]
  27. * @property {string} [location]
  28. * @property {number} [distanceKM]
  29. * @property {boolean} [isReal]
  30. * @property {boolean} [hasPremium]
  31. * @property {boolean} [hasSuper]
  32. * @property {boolean} [isPerma]
  33. * @property {boolean} [isOnline]
  34. * @property {string} [room]
  35. * @property {Date} [lastSeen]
  36. * @property {Date} [regDate]
  37. * @property {string[]} [ipList]
  38. * @property {Date} [scorePassword]
  39. * @property {Date} [scoreFinal]
  40. * @property {(date: Date) => string} [dateToHumanReadable]
  41. */
  42.  
  43. class GuessLogSearch extends HttpRequestHtml {
  44.  
  45. constructor(name) {
  46. /**
  47. * @param {string} labelText
  48. * @param {string} textContent
  49. * @returns {number}
  50. */
  51. const matchScore = (labelText, textContent) => {
  52. const regexLookBehind = new RegExp("(?<=" + labelText + ":\\s)");
  53. const regexFloat = /\d{1,2}\.?\d{0,20}/;
  54. const regexLookAhead = /(?=\spoints)/;
  55.  
  56. for (const regexesToJoin of [
  57. [regexLookBehind, regexFloat, regexLookAhead],
  58. [regexLookBehind, regexFloat]
  59. ]) {
  60. const regexAsString = regexesToJoin.map(re => re.source).join("");
  61. const matcher = new RegExp(regexAsString, "i").exec(textContent);
  62. if (matcher != null) {
  63. return Number.parseFloat(matcher[0]);
  64. }
  65. }
  66. };
  67.  
  68. /**
  69. * @param {RegExp} regex
  70. * @param {string} textContent
  71. * @returns {Array<String>}
  72. */
  73. const matchList = (regex, textContent) => {
  74. const results = [...textContent.matchAll(regex)].reduce((a, b) => [...a, ...b], []);
  75. if (results.length) {
  76. const resultsDistinct = [...new Set(results)];
  77. return resultsDistinct;
  78. }
  79. };
  80.  
  81. super({
  82. url: 'https://www.camamba.com/guesslog.php',
  83. params: { name },
  84. resultTransformer: (resp) => {
  85. const textContent = resp.html.body.textContent;
  86.  
  87. const ipList = matchList(/(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])/g, textContent);
  88. const prints = matchList(/(?<=Print\d{0,2}\schecked\sis\s)[0-9a-f]+/g, textContent);
  89.  
  90. const scorePassword = matchScore("password check", textContent);
  91. const scoreFinal = matchScore("final score", textContent);
  92.  
  93. return { userName: name, ipList, prints, scorePassword, scoreFinal };
  94. }
  95. });
  96. }
  97.  
  98. /** @returns {Promise<GuessLog>} */
  99. async send() {
  100. return await super.send();
  101. }
  102.  
  103. /**
  104. * @param {string} name
  105. * @returns {Promise<GuessLog>}
  106. */
  107. static async send(name) {
  108. return await new GuessLogSearch(name).send();
  109. }
  110. }
  111.  
  112. /**
  113. * @typedef {Object} BanLog
  114. * @property {string} moderator - user or moderator who triggered the log
  115. * @property {string} user - user who is subject
  116. * @property {Date} date - date of this log
  117. * @property {string} reason - content
  118. */
  119.  
  120. class BannLogSearch extends HttpRequestHtml {
  121. /**
  122. * @param {number} uid
  123. */
  124. constructor(uid = null) {
  125. super({
  126. url: 'https://www.camamba.com/banlog.php',
  127. params: uid ? { admin: uid } : {},
  128. resultTransformer: (response, _request) => {
  129. const results = [];
  130. const xPathExpr = "//tr" + ['User', 'Moderator', 'Date', 'Reason'].map(hdrText => `[td[span[text()='${hdrText}']]]`).join("");
  131. let tr = (response.html.evaluate(xPathExpr, response.html.body, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue || {}).nextElementSibling;
  132.  
  133. while (tr) {
  134. const tds = tr.querySelectorAll('td');
  135. const user = tds[0].querySelector("a") || tds[0].textContent;
  136. const moderator = tds[1].textContent;
  137.  
  138. let date;
  139. const dateMatch = /(\d{2}).(\d{2}).(\d{4})<br>(\d{1,2}):(\d{2}):(\d{2})/.exec(tds[2].innerHTML);
  140. if (dateMatch) {
  141. const day = dateMatch[1];
  142. const month = dateMatch[2];
  143. const year = dateMatch[3];
  144. const hour = dateMatch[4];
  145. const minute = dateMatch[5];
  146. const second = dateMatch[6];
  147. date = new Date(year, month - 1, day, hour, minute, second);
  148. }
  149.  
  150. const reason = tds[3].textContent;
  151. results.push({ user, moderator, date, reason });
  152.  
  153. tr = tr.nextElementSibling;
  154. }
  155.  
  156. return results;
  157. }
  158. });
  159. }
  160.  
  161. /**
  162. * @param {number} uid
  163. * @returns {Promise<BanLog[]>}
  164. */
  165. static async send(uid) {
  166. return await new BannLogSearch(uid).send();
  167. }
  168. }
  169.  
  170. class GalleryImage {
  171. constructor({ dataURI, href }) {
  172. /** @type {string} */
  173. this.dataURI = dataURI;
  174. /** @type {string} */
  175. this.href = href;
  176. }
  177. }
  178.  
  179.  
  180. class UserLevel {
  181. constructor({ level, uid = null, name = null, timeStamp = null }) {
  182.  
  183. /** @type {number} */
  184. this.level = level !== null ? Number.parseInt(level) : null;
  185.  
  186. /** @type {number} */
  187. this.uid = uid !== null ? Number.parseInt(uid) : null;
  188.  
  189. /** @type {string} */
  190. this.name = name;
  191.  
  192. /** @type {number} */
  193. this.timeStamp = timeStamp !== null ? Number.parseInt(timeStamp) : null;
  194. }
  195. }
  196.  
  197. class UserLevelSearch extends HttpRequestHtml {
  198. constructor(uid) {
  199. super({
  200. url: 'https://www.camamba.com/user_level.php',
  201. params: { uid },
  202. resultTransformer: (response, request) => {
  203. const html = response.html;
  204.  
  205. let name = null, level = null;
  206.  
  207. const nameElement = html.querySelector('b');
  208. if (nameElement) {
  209. name = nameElement.textContent;
  210. }
  211.  
  212. const levelElement = html.querySelector('font.xxltext');
  213. if (levelElement) {
  214. const levelMatch = /\d{1,3}/.exec(levelElement.textContent);
  215. if (levelMatch) {
  216. level = Number.parseInt(levelMatch);
  217. }
  218. }
  219.  
  220. return new UserLevel({ uid: request.params.uid, name, level, timeStamp: new Date().getTime() });
  221. }
  222. });
  223. }
  224.  
  225. /**
  226. * @returns {Promise<UserLevel>}
  227. */
  228. async send() {
  229. return await super.send();
  230. }
  231.  
  232. /**
  233. * @param {number} uid
  234. * @returns {Promise<UserLevel>}
  235. */
  236. static async send(uid) {
  237. return await new UserLevelSearch(uid).send();
  238. }
  239. }
  240.  
  241. class User {
  242. /** @param {UserParams} param0 */
  243. constructor({
  244. name, uid = 0, gender = null, age = null,
  245. longitude = null, latitude = null, location = null, distanceKM = null,
  246. isReal = null, hasPremium = null, hasSuper = null, isPerma = null,
  247. isOnline = null, room = null, lastSeen = null, regDate = null,
  248. dateToHumanReadable = (date) => date ?
  249. date.toLocaleString('de-DE', { timeStyle: "medium", dateStyle: "short", timeZone: 'CET' }) : '',
  250. }) {
  251. /** @type {string} */
  252. this.name = String(name);
  253. /** @type {number?} */
  254. this.uid = uid;
  255. /** @type {'male'|'female'|'couple'?} */
  256. this.gender = gender;
  257. /** @type {number?} */
  258. this.age = age;
  259.  
  260. /** @type {number?} */
  261. this.longitude = longitude;
  262. /** @type {number?} */
  263. this.latitude = latitude;
  264. /** @type {string?} */
  265. this.location = location;
  266. /** @type {number?} */
  267. this.distanceKM = distanceKM;
  268.  
  269. /** @type {boolean?} */
  270. this.isReal = isReal;
  271. /** @type {boolean?} */
  272. this.hasPremium = hasPremium;
  273. /** @type {boolean?} */
  274. this.hasSuper = hasSuper;
  275. /** @type {boolean?} */
  276. this.isPerma = isPerma;
  277.  
  278. /** @type {boolean?} */
  279. this.isOnline = isOnline;
  280. /** @type {string?} */
  281. this.room = room;
  282. /** @type {Date?} */
  283. this.lastSeen = lastSeen;
  284. /** @type {Date?} */
  285. this.regDate = regDate;
  286.  
  287. /** @type {string[]} */
  288. this.prints = [];
  289. /** @type {string[]} */
  290. this.ipList = [];
  291. /** @type {number?} */
  292. this.scorePassword = null;
  293. /** @type {number?} */
  294. this.scoreFinal = null;
  295. /** @type {number} */
  296. this.guessLogTS = null;
  297.  
  298. /** @type {(date: Date) => string} */
  299. this.dateToHumanReadable = dateToHumanReadable;
  300.  
  301. /** @type {number?} */
  302. this.level = null;
  303. /** @type {number} */
  304. this.levelTS = null;
  305.  
  306. /** @type {string[]} */
  307. this.galleryData = [];
  308. /** @type {number} */
  309. this.galleryDataTS = null;
  310. }
  311.  
  312. /** @type {string} @readonly */
  313. get lastSeenHumanReadable() {
  314. return this.dateToHumanReadable(this.lastSeen);
  315. }
  316.  
  317. /** @type {string} @readonly */
  318. get regDateHumanReadable() {
  319. return this.dateToHumanReadable(this.regDate);
  320. }
  321.  
  322. get galleryAsImgElements() {
  323. if (!this.galleryData) {
  324. return [];
  325. }
  326.  
  327. return this.galleryData.map(data => Object.assign(document.createElement('img'), {
  328. src: data.dataURI
  329. }));
  330. }
  331.  
  332. async updateGalleryHref() {
  333. const pictureLinks = (await HttpRequestHtml.send({
  334. url: "https://www.camamba.com/profile_view.php",
  335. params: Object.assign(
  336. { m: 'gallery' },
  337. this.uid ? { uid: this.uid } : { user: this.name }
  338. ),
  339.  
  340. pageNr: 0,
  341. pagesMaxCount: 500,
  342.  
  343. resultTransformer: (response) => {
  344. const hrefList = [...response.html.querySelectorAll("img.picborder")].map(img => img.src);
  345. return hrefList.map(href => href.slice(0, 0 - ".s.jpg".length) + ".l.jpg");
  346. },
  347. hasNextPage: (_resp, _httpRequestHtml, lastResult) => {
  348. return lastResult.length >= 15;
  349. },
  350. paramsConfiguratorForPageNr: (params, pageNr) => ({ ...params, page: pageNr }),
  351. })).flat();
  352.  
  353. this.galleryData = pictureLinks.map(href => ({ href }));
  354. this.galleryDataTS = new Date().getTime();
  355. }
  356.  
  357. async updateGalleryData(includeUpdateOfHref = true) {
  358. if (includeUpdateOfHref) {
  359. await this.updateGalleryHref();
  360. }
  361.  
  362. const readGalleryData = this.galleryData.map(({ href }) => (async () => {
  363. const dataURI = await HttpRequestBlob.send({ url: href });
  364. return new GalleryImage({ dataURI, href });
  365. })());
  366.  
  367. this.galleryData = await Promise.all(readGalleryData);
  368. this.galleryDataTS = new Date().getTime();
  369. }
  370.  
  371. async updateLevel() {
  372. const { level, timeStamp, name } = await UserLevelSearch.send(this.uid);
  373. this.level = level;
  374. this.levelTS = timeStamp;
  375. this.name = name;
  376. }
  377.  
  378. async updateGuessLog() {
  379. /** @type {GuessLog} */
  380. const guessLog = await GuessLogSearch.send(this.name);
  381. this.guessLogTS = new Date().getTime();
  382.  
  383. this.ipList = guessLog.ipList;
  384. this.prints = guessLog.prints;
  385. this.scorePassword = guessLog.scorePassword;
  386. this.scoreFinal = guessLog.scoreFinal;
  387. }
  388.  
  389. async addNote(text) {
  390. return new Promise((res, rej) => {
  391. GM_xmlhttpRequest({
  392. url: 'https://www.camamba.com/profile_view.php',
  393. method: 'POST',
  394. data: `uid=1268162&modnote=${encodeURIComponent(text)}&m=admin&nomen=1`,
  395. headers: {
  396. "Content-Type": "application/x-www-form-urlencoded"
  397. },
  398. onload: (xhr) => {
  399. res(xhr.responseText);
  400. },
  401. onerror: (xhr) => rej({
  402. status: xhr.status,
  403. statusText: xhr.statusText
  404. }),
  405. });
  406.  
  407. const data = new FormData();
  408. data.append('uid', this.uid);
  409. data.append('modnote', text);
  410. data.append('m', 'admin');
  411. data.append('nomen', '1');
  412.  
  413. const xhr = new XMLHttpRequest();
  414. xhr.open('POST', 'https://www.camamba.com/profile_view.php', true);
  415. xhr.onload = () => res(xhr.responseText);
  416. xhr.onerror = () => rej({
  417. status: xhr.status,
  418. statusText: xhr.statusText
  419. });
  420. xhr.send(data);
  421. });
  422. }
  423. }
  424.  
  425. class UserSearch extends HttpRequestHtml {
  426. /** @param {{
  427. * name: string?,
  428. * uid: number?,
  429. * gender: ('any' | 'male' | 'female' |'couple')?,
  430. * isOnline: boolean?, hasReal: boolean?, hasPremium: boolean?, hasSuper: boolean?, hasPicture: boolean?,
  431. * isSortByRegDate: boolean?,
  432. * isSortByDistance: boolean?,
  433. * isShowAll: boolean?,
  434. * pageNr: number?,
  435. * pagesMaxCount: number?,
  436. * keepInCacheTimoutMs: number?
  437. * }} param0 */
  438. constructor({
  439. name = null,
  440. uid = 0,
  441. gender = 'any',
  442. isOnline = null,
  443. hasReal = null,
  444. hasPremium = null,
  445. hasSuper = null,
  446. hasPicture = null,
  447. isSortByRegDate = null,
  448. isSortByDistance = null,
  449. isShowAll = null,
  450. pageNr = 1,
  451. pagesMaxCount = 1,
  452. keepInCacheTimoutMs
  453. } = {}) {
  454. let params = Object.assign(
  455. (name ? {
  456. nick: name
  457. } : {}),
  458. {
  459. gender: gender.toLowerCase(),
  460. },
  461. Object.fromEntries(Object.entries({
  462. online: isOnline,
  463. isreal: hasReal,
  464. isprem: hasPremium,
  465. issuper: hasSuper,
  466. picture: hasPicture,
  467. sortreg: isSortByRegDate,
  468. byDistance: isSortByDistance,
  469. showall: isShowAll,
  470. })
  471. .filter(([_k, v]) => typeof v !== 'undefined' && v !== null)
  472. .map(([k, v]) => ([[k], v ? 1 : 0])))
  473. );
  474.  
  475. params = Object.entries(params).map(([key, value]) => key + '=' + value).join('&');
  476.  
  477. if (params.length) {
  478. params += "&";
  479. }
  480. params += `page=${Math.max(pageNr - 1, 0)}`;
  481.  
  482. super({
  483. url: 'https://www.camamba.com/search.php',
  484. params,
  485. pageNr: Math.max(pageNr, 1),
  486. pagesMaxCount: Math.max(pagesMaxCount, 1),
  487. keepInCacheTimoutMs,
  488.  
  489. resultTransformer: (response) => {
  490. /** @type {Array<User>} */
  491. const users = [];
  492.  
  493. for (const tdNode of response.html.querySelectorAll('.searchSuper td:nth-child(2), .searchNormal td:nth-child(2)')) {
  494. const innerHTML = tdNode.innerHTML;
  495. const uidMatch = /<a\s+?href=["']javascript:sendMail\(["'](\d{1,8})["']\)/.exec(innerHTML);
  496. const nameMatch = /<a\s+?href=["']javascript:openProfile\(["'](.+?)["']\)/.exec(innerHTML);
  497. if (!uidMatch || !nameMatch) {
  498. break;
  499. }
  500.  
  501. const user = new User({
  502. name: nameMatch[1],
  503. uid: Number.parseInt(uidMatch[1]),
  504. isReal: /<img src="\/gfx\/real.png"/.test(innerHTML),
  505. hasPremium: /<a href="\/premium.php">/.test(innerHTML),
  506. hasSuper: /<img src="\/gfx\/super_premium.png"/.test(innerHTML),
  507. isOnline: /Online\snow(\s\in|,\snot in chat)/.test(innerHTML),
  508. });
  509.  
  510. // Längengrad, Breitengrad, Ortsname
  511. const locationMatch = /<a\s+?href="javascript:openMap\((-?\d{1,3}\.\d{8}),(-?\d{1,3}\.\d{8})\);">(.+?)<\/a>/.exec(innerHTML);
  512. if (locationMatch) {
  513. user.longitude = Number.parseFloat(locationMatch[1]);
  514. user.latitude = Number.parseFloat(locationMatch[2]);
  515. user.location = locationMatch[3];
  516. }
  517.  
  518. // Entfernung in km
  519. const distanceMatch = /(\d{1,5})\skm\sfrom\syou/.exec(innerHTML);
  520. if (distanceMatch) {
  521. user.distanceKM = parseInt(distanceMatch[1]);
  522. }
  523.  
  524. // Geschlecht und Alter
  525. const genderAgeMatch = /(male|female|couple),\s(\d{1,4})(?:<br>){2}Online/.exec(innerHTML);
  526. if (genderAgeMatch) {
  527. user.gender = genderAgeMatch[1];
  528. user.age = genderAgeMatch[2];
  529. }
  530.  
  531. // zuletzt Online
  532. if (user.isOnline) {
  533. user.lastSeen = new Date();
  534. } else {
  535. const lastSeenMatch = /(\d{1,4})\s(minutes|hours|days)\sago/.exec(innerHTML);
  536. if (lastSeenMatch) {
  537. const value = parseInt(lastSeenMatch[1]);
  538.  
  539. const factorToMillis = {
  540. 'minutes': 1000 * 60,
  541. 'hours': 1000 * 60 * 60,
  542. 'days': 1000 * 60 * 60 * 24,
  543. }[lastSeenMatch[2]];
  544.  
  545. user.lastSeen = new Date(Date.now() - value * factorToMillis);
  546. }
  547. }
  548.  
  549. // Raumname
  550. const roomMatch = /(?:ago|now)\sin\s([\w\s]+?|p\d{1,8})<br>/.exec(innerHTML);
  551. if (roomMatch) {
  552. user.room = roomMatch[1];
  553. }
  554.  
  555. // regDate
  556. const regDateMatch = /(\d{2}).(\d{2}).(\d{4})\s(\d{1,2}):(\d{2}):(\d{2})/.exec(innerHTML);
  557. if (regDateMatch) {
  558. const regDateDay = regDateMatch[1];
  559. const regDateMonth = regDateMatch[2];
  560. const regDateYear = regDateMatch[3];
  561. const regDateHour = regDateMatch[4];
  562. const regDateMinute = regDateMatch[5];
  563. const regDateSecond = regDateMatch[6];
  564. user.regDate = new Date(regDateYear, regDateMonth - 1, regDateDay, regDateHour, regDateMinute, regDateSecond);
  565. }
  566.  
  567. users.push(user);
  568. }
  569.  
  570. return users;
  571. },
  572.  
  573. hasNextPage: (_resp, _httpRequestHtml, lastResult) => {
  574. return lastResult.length >= 50;
  575. },
  576.  
  577. paramsConfiguratorForPageNr: (params, pageNr) => {
  578. return params.replace(/page=\d+(?:$)/, `page=${pageNr - 1}`);
  579. },
  580. });
  581. this.uid = uid || null;
  582. }
  583.  
  584. /** @returns {Promise<User[]>} */
  585. async send() {
  586. if (this.uid) {
  587. const user = new User({ uid: this.uid });
  588. await user.updateLevel();
  589.  
  590. if (!user.name || user.level) {
  591. return [];
  592. }
  593. if (this.params.nick) {
  594. const unameURIencoded = encodeURIComponent(user.name.toLowerCase());
  595. const unameFromSearchParam = encodeURIComponent(this.params.nick.toLowerCase()).trim();
  596. if (unameURIencoded.includes(unameFromSearchParam)) {
  597. return [];
  598. }
  599. }
  600.  
  601. this.params.nick = user.name;
  602. const result = (await super.send()).flat().find(u => u.uid == this.uid);
  603. if (!result) {
  604. return [];
  605. }
  606.  
  607. return [Object.assign(user, result)];
  608. }
  609.  
  610. return (await super.send()).flat();
  611. }
  612.  
  613. /**
  614. * @param {{
  615. * name: string,
  616. * uid: number?,
  617. * gender: 'any' | 'male' | 'female' |'couple',
  618. * isOnline: boolean,hasReal: boolean, hasPremium: boolean, hasSuper: boolean, hasPicture: boolean,
  619. * isSortByRegDate: boolean,
  620. * isSortByDistance: boolean,
  621. * isShowAll: boolean,
  622. * pageNr: number,
  623. * pagesMaxCount: number,
  624. * keepInCacheTimoutMs: number
  625. * }} param0
  626. * @returns {Promise<User[]>}
  627. */
  628. static async send(param0) {
  629. return await new UserSearch(param0).send();
  630. }
  631. }