-
-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for hashtag feeds (#312)
* feat: add hashtag params proto * feat: add support for hashtags * chore: add test * docs: update API ref * fix(tests): remove unneeded `#` from param * fix: do not throw when missing the header
- Loading branch information
Showing
12 changed files
with
299 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { YTNode } from '../helpers.js'; | ||
import Text from './misc/Text.js'; | ||
import type { RawNode } from '../index.js'; | ||
|
||
class HashtagHeader extends YTNode { | ||
static type = 'HashtagHeader'; | ||
|
||
hashtag: Text; | ||
hashtag_info: Text; | ||
|
||
constructor(data: RawNode) { | ||
super(); | ||
this.hashtag = new Text(data.hashtag); | ||
this.hashtag_info = new Text(data.hashtagInfoText); | ||
} | ||
} | ||
|
||
export default HashtagHeader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import FilterableFeed from '../../core/FilterableFeed.js'; | ||
import { InnertubeError } from '../../utils/Utils.js'; | ||
import HashtagHeader from '../classes/HashtagHeader.js'; | ||
import RichGrid from '../classes/RichGrid.js'; | ||
import Tab from '../classes/Tab.js'; | ||
|
||
import type Actions from '../../core/Actions.js'; | ||
import type { ApiResponse } from '../../core/Actions.js'; | ||
import type ChipCloudChip from '../classes/ChipCloudChip.js'; | ||
import type { IBrowseResponse } from '../index.js'; | ||
|
||
export default class HashtagFeed extends FilterableFeed<IBrowseResponse> { | ||
header?: HashtagHeader; | ||
contents: RichGrid; | ||
|
||
constructor(actions: Actions, response: IBrowseResponse | ApiResponse) { | ||
super(actions, response); | ||
|
||
if (!this.page.contents_memo) | ||
throw new InnertubeError('Unexpected response', this.page); | ||
|
||
const tab = this.page.contents_memo.getType(Tab).first(); | ||
|
||
if (!tab.content) | ||
throw new InnertubeError('Content tab has no content', tab); | ||
|
||
if (this.page.header) { | ||
this.header = this.page.header.item().as(HashtagHeader); | ||
} | ||
|
||
this.contents = tab.content.as(RichGrid); | ||
} | ||
|
||
/** | ||
* Applies given filter and returns a new {@link HashtagFeed} object. Use {@link HashtagFeed.filters} to get available filters. | ||
* @param filter - Filter to apply. | ||
*/ | ||
async applyFilter(filter: string | ChipCloudChip): Promise<HashtagFeed> { | ||
const response = await super.getFilteredFeed(filter); | ||
return new HashtagFeed(this.actions, response.page); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { | ||
tsValueToJsonValueFns, | ||
jsonValueToTsValueFns, | ||
} from "../../../runtime/json/scalar.js"; | ||
import { | ||
WireMessage, | ||
} from "../../../runtime/wire/index.js"; | ||
import { | ||
default as serialize, | ||
} from "../../../runtime/wire/serialize.js"; | ||
import { | ||
tsValueToWireValueFns, | ||
wireValueToTsValueFns, | ||
} from "../../../runtime/wire/scalar.js"; | ||
import { | ||
default as deserialize, | ||
} from "../../../runtime/wire/deserialize.js"; | ||
|
||
export declare namespace $.youtube.Hashtag { | ||
export type Params = { | ||
hashtag: string; | ||
type: number; | ||
} | ||
} | ||
|
||
export type Type = $.youtube.Hashtag.Params; | ||
|
||
export function getDefaultValue(): $.youtube.Hashtag.Params { | ||
return { | ||
hashtag: "", | ||
type: 0, | ||
}; | ||
} | ||
|
||
export function createValue(partialValue: Partial<$.youtube.Hashtag.Params>): $.youtube.Hashtag.Params { | ||
return { | ||
...getDefaultValue(), | ||
...partialValue, | ||
}; | ||
} | ||
|
||
export function encodeJson(value: $.youtube.Hashtag.Params): unknown { | ||
const result: any = {}; | ||
if (value.hashtag !== undefined) result.hashtag = tsValueToJsonValueFns.string(value.hashtag); | ||
if (value.type !== undefined) result.type = tsValueToJsonValueFns.int32(value.type); | ||
return result; | ||
} | ||
|
||
export function decodeJson(value: any): $.youtube.Hashtag.Params { | ||
const result = getDefaultValue(); | ||
if (value.hashtag !== undefined) result.hashtag = jsonValueToTsValueFns.string(value.hashtag); | ||
if (value.type !== undefined) result.type = jsonValueToTsValueFns.int32(value.type); | ||
return result; | ||
} | ||
|
||
export function encodeBinary(value: $.youtube.Hashtag.Params): Uint8Array { | ||
const result: WireMessage = []; | ||
if (value.hashtag !== undefined) { | ||
const tsValue = value.hashtag; | ||
result.push( | ||
[1, tsValueToWireValueFns.string(tsValue)], | ||
); | ||
} | ||
if (value.type !== undefined) { | ||
const tsValue = value.type; | ||
result.push( | ||
[3, tsValueToWireValueFns.int32(tsValue)], | ||
); | ||
} | ||
return serialize(result); | ||
} | ||
|
||
export function decodeBinary(binary: Uint8Array): $.youtube.Hashtag.Params { | ||
const result = getDefaultValue(); | ||
const wireMessage = deserialize(binary); | ||
const wireFields = new Map(wireMessage); | ||
field: { | ||
const wireValue = wireFields.get(1); | ||
if (wireValue === undefined) break field; | ||
const value = wireValueToTsValueFns.string(wireValue); | ||
if (value === undefined) break field; | ||
result.hashtag = value; | ||
} | ||
field: { | ||
const wireValue = wireFields.get(3); | ||
if (wireValue === undefined) break field; | ||
const value = wireValueToTsValueFns.int32(wireValue); | ||
if (value === undefined) break field; | ||
result.type = value; | ||
} | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type { Type as Params } from "./Params.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { | ||
Type as Params, | ||
encodeJson as encodeJson_1, | ||
decodeJson as decodeJson_1, | ||
encodeBinary as encodeBinary_1, | ||
decodeBinary as decodeBinary_1, | ||
} from "./(Hashtag)/Params.js"; | ||
import { | ||
jsonValueToTsValueFns, | ||
} from "../../runtime/json/scalar.js"; | ||
import { | ||
WireMessage, | ||
WireType, | ||
} from "../../runtime/wire/index.js"; | ||
import { | ||
default as serialize, | ||
} from "../../runtime/wire/serialize.js"; | ||
import { | ||
default as deserialize, | ||
} from "../../runtime/wire/deserialize.js"; | ||
|
||
export declare namespace $.youtube { | ||
export type Hashtag = { | ||
params?: Params; | ||
} | ||
} | ||
|
||
export type Type = $.youtube.Hashtag; | ||
|
||
export function getDefaultValue(): $.youtube.Hashtag { | ||
return { | ||
params: undefined, | ||
}; | ||
} | ||
|
||
export function createValue(partialValue: Partial<$.youtube.Hashtag>): $.youtube.Hashtag { | ||
return { | ||
...getDefaultValue(), | ||
...partialValue, | ||
}; | ||
} | ||
|
||
export function encodeJson(value: $.youtube.Hashtag): unknown { | ||
const result: any = {}; | ||
if (value.params !== undefined) result.params = encodeJson_1(value.params); | ||
return result; | ||
} | ||
|
||
export function decodeJson(value: any): $.youtube.Hashtag { | ||
const result = getDefaultValue(); | ||
if (value.params !== undefined) result.params = decodeJson_1(value.params); | ||
return result; | ||
} | ||
|
||
export function encodeBinary(value: $.youtube.Hashtag): Uint8Array { | ||
const result: WireMessage = []; | ||
if (value.params !== undefined) { | ||
const tsValue = value.params; | ||
result.push( | ||
[93, { type: WireType.LengthDelimited as const, value: encodeBinary_1(tsValue) }], | ||
); | ||
} | ||
return serialize(result); | ||
} | ||
|
||
export function decodeBinary(binary: Uint8Array): $.youtube.Hashtag { | ||
const result = getDefaultValue(); | ||
const wireMessage = deserialize(binary); | ||
const wireFields = new Map(wireMessage); | ||
field: { | ||
const wireValue = wireFields.get(93); | ||
if (wireValue === undefined) break field; | ||
const value = wireValue.type === WireType.LengthDelimited ? decodeBinary_1(wireValue.value) : undefined; | ||
if (value === undefined) break field; | ||
result.params = value; | ||
} | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters