Skip to content

Commit

Permalink
Merge pull request #649 from napple-team/windyakin_20240520_5b9f1f1104
Browse files Browse the repository at this point in the history
x.com を受け取れるようにした
  • Loading branch information
windyakin authored May 20, 2024
2 parents a8f48b5 + 672504d commit 3037404
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/controller/save-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ export class SaveController {
static async execute(req: Request, res: Response): Promise<void> {
const twitterClient = req.app.get('twitterClient')

const matchPettern = new RegExp('^https://twitter.com/([a-zA-Z0-9_]+)/status/([0-9]+)', 'i');
const matchPattern = new RegExp('^https://(twitter|x).com/([a-zA-Z0-9_]+)/status/([0-9]+)', 'i');
const requestTweetUrl = req.body.tweetUrl || ''

if (!requestTweetUrl || requestTweetUrl.match(matchPettern).length === 0) {
if (!requestTweetUrl || requestTweetUrl.match(matchPattern).length === 0) {
res.status(422).send('Not match tweet url pattern')
return
}
Expand Down
8 changes: 4 additions & 4 deletions src/lib/twitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ class Twitter {
}

async lookupTweet(tweetUrl: string): Promise<Tweet> {
const tweetUrlMatchPattern = new RegExp('^https://twitter.com/([a-zA-Z0-9_]+)/status/([0-9]+)$', 'i');
const tweetUrlMatchPattern = new RegExp('^https://(twitter|x).com/([a-zA-Z0-9_]+)/status/([0-9]+)$', 'i');
const matchPettern = tweetUrl.match(tweetUrlMatchPattern);
if (matchPettern === null) {
throw new Error('Not match tweet url pattern');
}
const [_, user, id] = matchPettern;
const [_, server, user, id] = matchPettern;
const response = await this.fetchTweet(user, id);
const html: HTMLElement = parse(response);
const urlElement = html.querySelector('link[rel="canonical"]');
Expand All @@ -31,9 +31,9 @@ class Twitter {
throw new Error('Failed fetching data');
}

const identifier = url.replace(tweetUrlMatchPattern, '$2');
const identifier = url.replace(tweetUrlMatchPattern, '$3');

const userId = url.replace(tweetUrlMatchPattern, '$1');
const userId = url.replace(tweetUrlMatchPattern, '$2');

if (!identifier || !userId) {
throw new Error('Failed parsing data');
Expand Down
10 changes: 10 additions & 0 deletions test/lib/twitter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ describe('Twitter lib test', () => {
]);
});

it('return tweet data when x.com', async () => {
const actual = await twitterClient.lookupTweet('https://x.com/MITLicense/status/1652865752331091968');
expect(actual.identifier).toBe('1652865752331091968');
expect(actual.url).toBe('https://twitter.com/MITLicense/status/1652865752331091968');
expect(actual.userId).toBe('MITLicense');
expect(actual.imageUrls).toEqual([
'https://pbs.twimg.com/media/FvAopGGaEAAp-Dk.jpg'
]);
});

it('retrun tweet data when fetching multi pictures post', async () => {
const actual = await twitterClient.lookupTweet('https://twitter.com/MITLicense/status/1652160368901505027');
expect(actual.identifier).toBe('1652160368901505027');
Expand Down

0 comments on commit 3037404

Please sign in to comment.