Skip to content

Commit

Permalink
getUser & getHashflags
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasngf committed Sep 23, 2021
1 parent 7a8db38 commit 36434ce
Show file tree
Hide file tree
Showing 11 changed files with 366 additions and 41 deletions.
42 changes: 39 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

> Get typed data from the public twitter api. No keys required.
[![NPM](https://img.shields.io/npm/v/twitter-api-scraper.svg)](https://www.npmjs.com/package/twitter-api-scraper) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
[![NPM](https://img.shields.io/npm/v/twitter-api-scraper.svg)](https://www.npmjs.com/package/twitter-api-scraper) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) [https://img.shields.io/github/license/matiasngf/twitter-api-scraper?style=flat-square](https://github.com/matiasngf/twitter-api-scraper/blob/main/LICENSE) [https://img.shields.io/github/issues/matiasngf/twitter-api-scraper](https://github.com/matiasngf/twitter-api-scraper/issues)

- [x] Search tweets
- [x] Get users by username
- [x] Get users by id
- [x] Get hashflags
- [ ] Get tweets from user
- [ ] Get replies from tweet
- [ ] Get trending topics

## Install

Expand All @@ -13,14 +21,17 @@ npm install --save twitter-api-scraper
## Usage

### Start client
It's important that you connect() the client first.

First, connect() the client.

```ts
import TwitterClient from 'twitter-api-scraper'
const client = new TwitterClient()
await client.connect()
```

### Search

```ts
const maxTweets = 3000
const result = await client.search(
Expand All @@ -35,6 +46,7 @@ const { tweets, users, nextToken } = result
```

### Typescript

```ts
import { SearchQuery, ParsedSearchResult } from 'twitter-api-scraper'

Expand All @@ -48,11 +60,12 @@ const result: ParsedSearchResult = await client.search(query)
```

### Search parameters

```ts
const query: SearchQuery = {
terms: '#typescript',
dateFrom: '2021-02-15',
dateTo: '2020-02-15',
dateTo: '2021-02-17',
minReplies: 10,
minRetweets: 10,
minFaves: 10,
Expand All @@ -62,6 +75,7 @@ client.search(query)
```

### Search multiple pages

```ts
const result = await client.search(query, 100)
const { nextToken } = result
Expand All @@ -70,6 +84,7 @@ const secondPageResult = await client.search(query, 100, nextToken)
```

### Original api response

```ts
const originalApiResult = await client.searchRaw(
{
Expand All @@ -78,6 +93,27 @@ const originalApiResult = await client.searchRaw(
)
```

## Users

### Get user

```ts
const user: UserInterface = await client.getUser('jack')
```

### Get user by id

```ts
const user: UserFromId = await client.getUserById('12')
```

## Hashflags

```ts
const hashflags: Hashflag[] = await client.getHashflags('2021-09-23')
```


## License

MIT © [matiasngf](https://github.com/matiasngf)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "twitter-api-scraper",
"version": "0.0.2",
"version": "0.0.3",
"description": "Scrape the twitter api, no keys required.",
"author": "matiasngf",
"license": "MIT",
Expand Down
35 changes: 32 additions & 3 deletions src/TwitterClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import { getBearerToken } from './utils/getBearerToken'
import { clientSearch, SearchQuery } from './utils/clientSearch'
import { ParsedSearchResult, parseSearch } from './utils/parseSearch'
import { SearchResults } from './types/searchResults'
import { getHashflags, Hashflag } from './utils/getHashflags'
import { getUser, getUserById } from './utils/getUser'
import { UserInterface } from './types/userInterface'
import { UserFromId } from './types/userFromIdInterface'

export default class TwitterClient {
// Options
Expand Down Expand Up @@ -95,13 +99,11 @@ export default class TwitterClient {
this.say('New bearer token setted.')
}

connect = async (maxRetries?: number) => {
connect = async () => {
this.say('Connecting...')
const retries = maxRetries || this.maxRetries
await this.getBearerToken()
await this.getGuestToken()
this.say('Connected to twitter')
return retries
}

private say = (message: any) => {
Expand Down Expand Up @@ -133,4 +135,31 @@ export default class TwitterClient {
this.say('Searched tweets')
return data
}

// Get User
getUser = async (username: string): Promise<UserInterface> => {
if (typeof username !== 'string') {
throw new Error('Username must be string.')
}
const name = username.replace('@', '')
this.say(`Getting user: ${name}`)
const data = await getUser(this.apiClient, name)
this.say('User OK')
return data
}

getUserById = async (userId: string | number): Promise<UserFromId> => {
this.say(`Getting user by id: ${userId}`)
const data = await getUserById(this.apiClient, userId)
this.say('User OK')
return data
}

// Get hashflags
getHashflags = async (date: string): Promise<Hashflag[]> => {
this.say('Getting hashflags...')
const data = await getHashflags(date)
this.say('Hashflags OK')
return data
}
}
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ export * from './utils/clientSearch'
export * from './utils/getApiClientInstance'
export * from './utils/getBearerToken'
export * from './utils/getGuestToken'
export * from './utils/getHashflags'
export * from './utils/getInitialOptions'
export * from './utils/getTweetsFromUser'
export * from './utils/getUser'
export * from './utils/parseSearch'

// Types
export * from './types/searchResults'
export * from './types/userFromIdInterface'
export * from './types/userInterface'

export default TwitterClient
84 changes: 84 additions & 0 deletions src/types/userFromIdInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* eslint-disable quotes */
/* eslint-disable camelcase */
export interface UserFromId {
data: Data
}

interface Data {
user: User
}

interface User {
id: string
rest_id: string
affiliates_highlighted_label: AffiliatesHighlightedLabel
legacy: Legacy
}

interface AffiliatesHighlightedLabel {}

interface Legacy {
created_at: string
default_profile: boolean
default_profile_image: boolean
description: string
entities: Entities
fast_followers_count: number
favourites_count: number
followers_count: number
friends_count: number
has_custom_timelines: boolean
is_translator: boolean
listed_count: number
location: string
media_count: number
name: string
normal_followers_count: number
pinned_tweet_ids_str: string[]
profile_banner_extensions: ProfileExtensions
profile_banner_url: string
profile_image_extensions: ProfileExtensions
profile_image_url_https: string
profile_interstitial_type: string
protected: boolean
screen_name: string
statuses_count: number
translator_type: string
verified: boolean
withheld_in_countries: any[]
}

interface Entities {
description: Description
}

interface Description {
urls: any[]
}

interface ProfileExtensions {
mediaColor: MediaColor
}

interface MediaColor {
r: R
}

interface R {
ok: Ok
}

interface Ok {
palette: Palette[]
}

interface Palette {
percentage: number
rgb: RGB
}

interface RGB {
blue: number
green: number
red: number
}
114 changes: 114 additions & 0 deletions src/types/userInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/* eslint-disable quotes */
/* eslint-disable camelcase */
export interface UserInterface {
data: Data
}

interface Data {
user: UserClass
}

interface UserClass {
result: Result
}

interface Result {
__typename: string
id: string
rest_id: string
affiliates_highlighted_label: AffiliatesHighlightedLabel
legacy: Legacy
smart_blocked_by: boolean
smart_blocking: boolean
legacy_extended_profile: LegacyExtendedProfile
is_profile_translatable: boolean
}

interface AffiliatesHighlightedLabel {}

interface Legacy {
created_at: string
default_profile: boolean
default_profile_image: boolean
description: string
entities: Entities
fast_followers_count: number
favourites_count: number
followers_count: number
friends_count: number
has_custom_timelines: boolean
is_translator: boolean
listed_count: number
location: string
media_count: number
name: string
normal_followers_count: number
pinned_tweet_ids_str: string[]
profile_banner_extensions: ProfileExtensions
profile_banner_url: string
profile_image_extensions: ProfileExtensions
profile_image_url_https: string
profile_interstitial_type: string
protected: boolean
screen_name: string
statuses_count: number
translator_type: string
url: string
verified: boolean
withheld_in_countries: any[]
}

interface Entities {
description: Description
url: Description
}

interface Description {
urls: URL[]
}

interface URL {
display_url: string
expanded_url: string
url: string
indices: number[]
}

interface ProfileExtensions {
mediaColor: MediaColor
}

interface MediaColor {
r: R
}

interface R {
ok: Ok
}

interface Ok {
palette: Palette[]
}

interface Palette {
percentage: number
rgb: RGB
}

interface RGB {
blue: number
green: number
red: number
}

interface LegacyExtendedProfile {
birthdate: Birthdate
}

interface Birthdate {
day: number
month: number
year: number
visibility: string
year_visibility: string
}
Loading

0 comments on commit 36434ce

Please sign in to comment.