mtg-playerinfo 1.4.0 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mtg-playerinfo",
3
- "version": "1.4.0",
3
+ "version": "1.4.1",
4
4
  "description": "A simple NPM module and CLI tool to pull Magic: The Gathering player data from various sources",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -1,5 +1,6 @@
1
1
  const { request } = require('../utils/httpClient')
2
2
  const cheerio = require('cheerio')
3
+ const { extractHandle, getPlatformName } = require('../utils/socialMediaExtractor')
3
4
 
4
5
  class MeleeFetcher {
5
6
  async fetchById (username) {
@@ -33,17 +34,10 @@ class MeleeFetcher {
33
34
  $('.social-link').each((i, el) => {
34
35
  const href = $(el).attr('href')
35
36
  if (href) {
36
- try {
37
- const urlObj = new URL(href)
38
- const platform = urlObj.hostname.replace('www.', '').split('.')[0]
39
- let handle = urlObj.pathname.split('/').filter(Boolean).pop()
40
- if (handle) {
41
- handle = decodeURIComponent(handle)
42
- const label = platform.charAt(0).toLowerCase() + platform.slice(1)
43
- data[label] = handle
44
- }
45
- } catch (e) {
46
- console.log('Invalid URL in social link ' + href + ': ' + e.message)
37
+ const handle = extractHandle(href)
38
+ const platform = getPlatformName(href)
39
+ if (handle && platform) {
40
+ data[platform] = handle
47
41
  }
48
42
  }
49
43
  })
@@ -1,5 +1,6 @@
1
1
  const httpClient = require('../utils/httpClient')
2
2
  const cheerio = require('cheerio')
3
+ const { extractHandle } = require('../utils/socialMediaExtractor')
3
4
 
4
5
  class TopdeckFetcher {
5
6
  async fetchById (handle) {
@@ -84,12 +85,18 @@ class TopdeckFetcher {
84
85
 
85
86
  const twitterLink = $('a[href*="twitter.com"]').attr('href')
86
87
  if (twitterLink) {
87
- data.twitter = twitterLink
88
+ const handle = extractHandle(twitterLink)
89
+ if (handle) {
90
+ data.twitter = handle
91
+ }
88
92
  }
89
93
 
90
94
  const youtubeLink = $('a[href*="youtube.com"]').attr('href')
91
95
  if (youtubeLink) {
92
- data.youtube = youtubeLink
96
+ const handle = extractHandle(youtubeLink)
97
+ if (handle) {
98
+ data.youtube = handle
99
+ }
93
100
  }
94
101
 
95
102
  const statsMap = {
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Extracts social media handle from a URL
3
+ * @param {string} url - The full social media URL
4
+ * @returns {string|null} - The handle (without the URL), or null if extraction fails
5
+ */
6
+ function extractHandle (url) {
7
+ if (!url) {
8
+ return null
9
+ }
10
+
11
+ try {
12
+ const urlObj = new URL(url)
13
+ let handle = urlObj.pathname.split('/').filter(Boolean).pop()
14
+ if (handle) {
15
+ handle = decodeURIComponent(handle)
16
+ return handle
17
+ }
18
+ } catch (e) {
19
+ console.log('Invalid URL in social link ' + url + ': ' + e.message)
20
+ }
21
+
22
+ return null
23
+ }
24
+
25
+ /**
26
+ * Extracts platform name from a URL hostname
27
+ * @param {string} url - The full social media URL
28
+ * @returns {string|null} - The platform name (e.g., 'twitter', 'youtube'), or null if extraction fails
29
+ */
30
+ function getPlatformName (url) {
31
+ if (!url) {
32
+ return null
33
+ }
34
+
35
+ try {
36
+ const urlObj = new URL(url)
37
+ const platform = urlObj.hostname.replace('www.', '').split('.')[0]
38
+ return platform.charAt(0).toLowerCase() + platform.slice(1)
39
+ } catch (e) {
40
+ console.log('Invalid URL: ' + url + ': ' + e.message)
41
+ }
42
+
43
+ return null
44
+ }
45
+
46
+ module.exports = {
47
+ extractHandle,
48
+ getPlatformName
49
+ }
50
+
@@ -24,8 +24,8 @@ test('TopdeckFetcher: parseHtml extracts stats from DOM when available', () => {
24
24
  assert.equal(result.url, url)
25
25
  assert.equal(result.name, 'Björn Kimminich')
26
26
  assert.equal(result.pronouns, 'He/Him', 'Should extract pronouns from badge')
27
- assert.equal(result.twitter, 'https://twitter.com/bkimminich', 'Should extract Twitter link')
28
- assert.equal(result.youtube, 'https://www.youtube.com/@Bj%C3%B6rnKimminich', 'Should extract YouTube link')
27
+ assert.equal(result.twitter, 'bkimminich', 'Should extract Twitter handle')
28
+ assert.equal(result.youtube, '@BjörnKimminich', 'Should extract YouTube handle')
29
29
  })
30
30
 
31
31
  test('TopdeckFetcher: fetchStats updates playerInfo with data from stats JSON', async () => {