Giter Site home page Giter Site logo

ptt-client's Introduction

ptt-client

ptt-client is an unofficial client to fetch data from PTT (ptt.cc), the famous BBS in Taiwan, over WebSocket. This module works in browser and Node.js.

PTT supports connection with WebSocket by official.

Installation

npm install ptt-client

Example

import Ptt from 'ptt-client';
import {Article, Board, Mail} from 'ptt-client/sites/ptt/model';

// if you are using this module in node.js, 'ws' is required as WebSocket polyfill.
// you don't need this in modern browsers
global.WebSocket = require('ws');

(async function() {
  const ptt = new Ptt();

  ptt.once('connect', () => {

    const kickOther = true;
    if (!await ptt.login('username', 'password', kickOther))
      return;
  
    // get last 20 articles from specific board. the first one is the latest
    let query = ptt.select(Article).where('boardname', 'C_Chat');
    let article = await query.get();

    // get articles with offset 
    let offset = articles[article.length-1].id - 1;
    query.where('id', offset);
    let articles2 = await query.get();

    // get articles with search filter (type: 'push', 'author', 'title')
    query = ptt.select(Article)
      .where('boardname', 'C_Chat')
      .where('title', '閒聊')
      .where('title', '京阿尼')
      .where('push', '20');
    articles = await query.get();
  
    // get the content of specific article
    query.where('id', articles[articles.length-1].id);
    let article = await query.getOne();

    // get board list
    query = ptt.select(Board).where('entry', 'class');
    let classBoards = await query.get();

    // get hot board list
    query = ptt.select(Board).where('entry', 'hot');
    let hotBoards = await query.get();
  
    // get your favorite list
    query = ptt.select(Board).where('entry', 'favorite');
    let favorites = await query.get();

    // search board by prefix
    query = ptt.select(Board).where('prefix', 'c_cha');
    let boards = await query.get();
  
    // get favorite list in a folder
    if (favorites[0].folder) {
      query.where('offsets', [favorites[0].id]);
      let favorites2 = await query.get();
    }

    // get mails
    query = ptt.select(Mail);
    let mails = await query.get();

    // get mail
    query.where('id', mails[0].sn);
    let mail = await query.getOne();

    await ptt.logout();

  });
})();

Development

npm run test
npm run build

License

MIT

ptt-client's People

Contributors

dependabot[bot] avatar josepht5566 avatar jubeatwww avatar kevinptt0323 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

ptt-client's Issues

How to use this package in JavaScript but not TypeScript?

I'm trying to use this great package in Node v10, in JavaScript but not TypeScript.

However, it seems the process just exit without any stdout or stderr.

Here is my reproduce: https://repl.it/repls/FlawedMiserlyKeygen, the Promise { <pending> } stdout is only show on Repl.it platform, if you reproduce this JavaScript file in local Node environment like node index.js, process just exit immediately without any stdout or stderr too.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.