import { TweetIndex } from "../types/Internal"; import { PartialTweetUser, PartialTweet, ClickedTweet } from "../types/ClassicTweets"; import { PartialTweetGDPR, GDPRClickedTweet } from "../types/GDPRTweets"; export interface TweetDateIndex { [year: string]: { [month: string]: TweetIndex; }; } export interface TweetLikeContainer { add(elements: T[]): void; month(month: string | number, year: string | number): T[]; fromThatDay(start?: Date): T[]; single(id_str: string): T | null; has(id_str: string): boolean; readonly index: TweetDateIndex; readonly length: number; readonly all: T[]; [Symbol.iterator](): Generator; } /** * Contains every tweet related to an archive. * * This object is automatically built when you load an archive inside `TwitterArchive` constructor, * or when tweets are loaded through `TwitterArchive.loadArchivePart()` / * `TwitterArchive.loadClassicArchivePart()` methods. */ export declare class TweetArchive implements TweetLikeContainer { /** ID-indexed tweets; This is always filled. */ protected _index: TweetIndex; /** * Year then month-indexed tweets; * This is not always generated, * use `.index` getter instead. */ protected _date_index: TweetDateIndex; /** * All tweets, in a single array; * This is not always generated, * use `.all` getter instead. */ protected _all: PartialTweet[]; protected user_cache: PartialTweetUser; protected _finder: TweetFinder; protected _clicked: { [id_str: string]: ClickedTweet; }; protected _clicked_urls: Set; /** ------------------ */ /** ARCHIVE MANAGEMENT */ /** ------------------ */ /** * Add tweets to this archive. * This method should not be called by end-programmer. * * Prefer usage of `TwitterArchive.loadClassicArchivePart()` method. */ add(tweets: PartialTweet[]): void; /** * Add unconverted GDPR tweets to this archive. * This method should not be called by end-programmer. * * Prefer usage of `TwitterArchive.loadArchivePart()` method. */ addGDPR(tweets: PartialTweetGDPR[]): void; /** * Add clicked actions to this container. */ addClicked(clicked_actions: GDPRClickedTweet[]): void; /** ---------------- */ /** TWEET NAVIGATION */ /** ---------------- */ /** Extract tweets from a specific month. Months are indexed from 1. */ month(month: string | number, year: string | number): PartialTweet[]; /** Find tweets made on the same day as {start} (= month, = day), but in all years. {start} defaults to now. */ fromThatDay(start?: Date): PartialTweet[]; /** Get tweets in a specific time interval. */ between(since: Date | string, until: Date | string): PartialTweet[]; /** Get a single tweet by ID. Returns `null` if tweet does not exists. */ single(id_str: string): PartialTweet | null; /** Check if tweet {id_str} exists in this archive. */ has(id_str: string): boolean; /** * Find tweets matching **query** in this tweet archive. * * This is a shortcut of `.finder.search(tweets, ...)` * * --- * * **Documentation from `TweetFinder.search()`** : * * Query can contain keywords. * Default available keywords are: * - `since:{YYYY-MM-DD} or {YYYY-MM} or {YYYY}` * - `until:{YYYY-MM-DD} or {YYYY-MM} or {YYYY}` * - `from:{screen_name,screen_name_2,screen_name_3}` * - `retweet_of:{screen_name} (use of , is allowed, like from:)` * * To be in the result array, a tweet must validate ALL the keywords. * * You can add your custom validators by pushing * to **.finder.validators** a object implementing `TweetSearchValidator`. * * @param query String to be searched for in the tweet.text / screen_name. * You should remember that in tweets, `>` and `<` are encoded as `>` and `<`. * * @param is_regex If the string should be considered as a regex during text.match or not. * - If **is_regex** is `true`, regex will be enabled without flags. * - If **is_regex** is `false`, **query** will be regex escaped. * - If **is_regex** is a `String`, regex will be enabled with **is_regex** as flags. * * Validators inside the query, such as `since:2018-01-01` will be removed from the query regex/string. * * @param static_validators Array of static validators names that should used. * See `.finder.static_validators`. * * Default defined static validators are: * - `retweets_only` * - `medias_only` * - `videos_only` * - `no_retweets` * * @param search_in Tweet properties to search. This is NOT dynamic you can't specify the property you want. * Available properties are: * - `text` * - `user.screen_name` * - `user.name` * * Default activated properties are `text` and `user.screen_name`. * * @throws {SyntaxError} Format `{keyword}: Invalid query` if user input is invalid * for a specific validator. * * @throws {ReferenceError} `Validator {name} does not exists` when a invalid static validator is used. */ find(query: string, is_regex?: boolean | string, static_validators?: string[], search_in?: string[]): PartialTweet[]; /** --------- */ /** ACCESSORS */ /** --------- */ /** * Index of tweets by years. * * Example, get index of tweets posted on 2019/01 : * **.years[2019][1]** */ get index(): TweetDateIndex; /** * Index of tweets by ID. */ get id_index(): TweetIndex; /** Number of tweets in this archive. */ get length(): number; /** * All tweets registered in this archive. * * Remember that it's not assured that tweets are sorted. * * To get ordered tweets, use `.sortedIterator()`. */ get all(): PartialTweet[]; /** * Returns the local `TweetFinder` instance assigned to this `TweetArchive`. * * A global exported instance also exists and named `TweetSearcher`. */ get finder(): TweetFinder; /** ------- */ /** CLICKED */ /** ------- */ get clicked(): ClickedTweet[]; get clicked_urls(): Set; /** * Tells if tweet {id_str} has been clicked. */ hasClickedTo(id_str: string): boolean; /** * Tells if URL {link} has been clicked by user. */ hasClickedLink(link: string): boolean; /** --------- */ /** ITERATORS */ /** --------- */ /** * Iterate through the tweets */ [Symbol.iterator](): Generator; /** * Iterate through tweets, sorted by year and month. * * At each iteration: [year: string, month: string, tweet: PartialTweet] * * @param order Order **months** and **years** by `asc` or `desc`. `asc` by default. * This do **NOT** sort tweets, use `.sortedIterator()` instead ! */ monthIterator(order?: "asc" | "desc"): Generator<[string, string, PartialTweet], void, void>; /** * Iterate over tweets, which are **genuinely** sorted (date, {order}). * * @param order Order by `asc` or `desc`. `desc` by default. */ sortedIterator(order?: "asc" | "desc"): Generator; /** * Iterate over clicked links sorted by tweet ID. * * @param order Order by `asc` or `desc`. `desc` by default. */ sortedClickedIterator(order?: "asc" | "desc"): Generator; /** * Iterate over clicked links sorted by click timestamp. * * @param order Order by `asc` or `desc`. `desc` by default. */ dateClickedIterator(order?: "asc" | "desc"): Generator; /** * Iterate array of tweets by month * * @param order Order by `asc` or `desc`. */ protected arrayMonthIterator(order: "asc" | "desc"): Generator<[string, string, PartialTweet[]], void, void>; /** ------- */ /** HELPERS */ /** ------- */ /** * Needed to convert GDPR tweets to PartialTweets. * * **You should not use this method! This is for internal purpose.** */ __initUserCache(user_info: { id_str: string; name: string; screen_name: string; profile_image_url_https: string; }): void; protected convertToPartial(tweet: PartialTweetGDPR): PartialTweet; protected refreshEntities(tweet: PartialTweet, owner: string, difference: number): void; } export default TweetArchive; export interface TweetFindOptions { use_regex?: boolean | string; static_validators?: string[]; search_in?: ('text' | 'user.name' | 'user.screen_name')[]; get_debug_results?: (debug: TweetFinderDebugObject) => any; } export interface TweetFinderDebugObject { validators: { [keyword: string]: Array<{ separator: string; callback: Function; text: string; }>; }; static_validators: Function[]; text: string; query?: RegExp; } export declare class TweetFinder { /** * Keywords to use in search query. * Each keyword represents a pair **keyword**:**content**. * * Keywords are used to add search parameters (conditions) that requires a user input * (like tweets before a specific date, retweets of a desired user...). * * If you want to add a condition that is static (test if is a retweet, * test if tweet has medias...), see `.static_validators`. * * See how to add keywords in `TweetSearchValidator` interface. */ validators: TweetSearchValidator[]; /** * Defined "static validators": Validators that does not depends on user query, * like test if a tweet is a retweet or keep only tweets with medias. * * Validators are represented by its name (in key) * and the validator itself (the check function, in value). * * You can use them when you search tweets by specifing the static validators names * in the `static_validators` parameter of `search()` method. */ static_validators: { [staticName: string]: TweetSearchStaticValidator; }; /** * Deprecated. Use `.find()` instead, that is more future proof. */ search(tweets: Iterable, query: string, is_regex?: boolean | string, static_validators?: string[], search_in?: string[]): PartialTweet[]; /** * Search into **tweets** using **query**. * * Query can contain keywords. * Default available keywords are: * - `since:{YYYY-MM-DD} or {YYYY-MM} or {YYYY}` * - `until:{YYYY-MM-DD} or {YYYY-MM} or {YYYY}` * - `from:{screen_name,screen_name_2,screen_name_3}` * - `retweet_of:{screen_name} (use of , is allowed, like from:)` * * To be matched, a tweet must validate ALL the keywords. * * You can add your custom validators by pushing * to **this.validators** a object implementing `TweetSearchValidator`. * * @param tweets Partial tweets array. * * @param query String to be searched for in the tweet.text / screen_name. * You should remember that in tweets, `>` and `<` are encoded as `>` and `<`. * * @param options.use_regex If the string should be considered as a regex during text.match or not. * - If **use_regex** is `true`, regex will be enabled without flags. * - If **use_regex** is `false`, **query** will be regex escaped. * - If **use_regex** is a `String`, regex will be enabled with **is_regex** as flags. * * Validators inside the query, such as `since:2018-01-01` will be removed from the query regex/string. * * @param options.static_validators Array of static validators names that should used. * See `.static_validators`. * * Default defined static validators are: * - `retweets_only` * - `medias_only` * - `videos_only` * - `no_retweets` * * @param options.search_in Tweet properties to search. This is NOT dynamic you can't specify the property you want. * Available properties are: * - `text` * - `user.screen_name` * - `user.name` * * Default activated properties are `text` and `user.screen_name`. * * @param options.get_debug_results A callable function that take compiled search options (static validators, validators, text). Defaults to `undefined`. * * @throws {SyntaxError} Format `{keyword}: Invalid query` if user input is invalid * for a specific validator. * * @throws {ReferenceError} `Validator {name} does not exists` when a invalid static validator is used. */ find(query: string, tweets: Iterable, options?: TweetFindOptions): Generator; /** Reset validators. This does NOT work if you have modified the inner initial objects ! */ reset(): void; } export declare const TweetSearcher: TweetFinder; export interface TweetSearchValidator { /** * Keyword (word used to identify you keyword, before the **separator**. * Will be interpoled into search regex. * Should **NOT** contain parenthesis ! */ keyword: string; /** * Method that generate a validator function with user input in your **query**. * This method **should** return a *function*. * * You should verify user-input stored inside {user_query}. * * If the return type is null-ish (`undefined`, `null`, `false`,...), search will be **aborted** * with thrown **unwell-formed query error**. * * --- * * For example, if you want to validate tweets that are above a date * entered by the user, with the schema: `keyword:YYYY-MM-DD`, * `YYYY-MM-DD` entered by the user will be given in parameter to this attribute (`.validator`), * which should return a function that test if the tweet date is above user entered `YYYY-MM-DD`. * * **EXAMPLE** * * *Do not use this example in real case, it does not check well user input.* * * ```ts * validator: user_query => { * // Parse user input * const time = (new Date(user_query)).getTime(); * * // Check some data validity * if (isNaN(time)) { * // Data could not be used, validator is invalid, returns undefined. * // This will throw an Error. * return undefined; * } * * // Return the validator function that returns true if tweet date >= user's date. * return tweet => dateFromTweet(tweet).getTime() >= time; * } * ``` */ validator: (user_query: string, separator: string) => ValidatorExecFunction; /** * Separator between keyword and user input. * * Default separator is `:`. * * Note that spaces are NOT allowed between keyword and separator, and between user input and separator. * * You can specify multiple allowed separators with a `string[]`. */ separator?: string | string[]; } export type ValidatorExecFunction = (tweet: PartialTweet) => boolean; export type TweetSearchStaticValidator = ValidatorExecFunction;