All files / src/profiles/services instagram.js

84.62% Statements 22/26
75% Branches 9/12
83.33% Functions 5/6
84% Lines 21/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62            1x 1x       1x 1x   1x 1x 1x             5x 5x   5x 2x 2x   5x               2x 2x 2x 2x             2x 2x     2x 2x                
/* @flow */
import cheerio from 'cheerio'
import { Service } from './service'
 
class Instagram extends Service {
  static getBaseUrls() {
    const baseUrls = ['https://www.instagram.com/', 'https://instagram.com/']
    return baseUrls
  }
 
  static getProofUrl(proof: Object) {
    const baseUrls = this.getBaseUrls()
    const normalizedProofUrl = this.normalizeInstagramUrl(proof)
 
    for (let i = 0; i < baseUrls.length; i++) {
      Eif (normalizedProofUrl.startsWith(`${baseUrls[i]}`)) {
        return normalizedProofUrl
      }
    }
    throw new Error(`Proof url ${proof.proof_url} is not valid for service ${proof.service}`)
  }
 
  static normalizeInstagramUrl(proof: Object) {
    let proofUrl = proof.proof_url
    proofUrl = super.prefixScheme(proofUrl)
 
    if (proofUrl.startsWith('https://instagram.com')) {
      const tokens = proofUrl.split('https://instagram.com')
      proofUrl = `https://www.instagram.com${tokens[1]}`
    }
    return proofUrl
  }
 
  static shouldValidateIdentityInBody() {
    return true
  }
 
  static getProofIdentity(searchText: string) {
    const $ = cheerio.load(searchText)
    const username = $('meta[property="og:description"]').attr('content')
    Eif (username !== undefined && username.split(':').length > 1) {
      return username.split(':')[0].match(/(@\w+)/)[0].substr(1)
    } else {
      return ''
    }
  }
 
  static getProofStatement(searchText: string) {
    const $ = cheerio.load(searchText)
    const statement = $('meta[property="og:description"]')
      .attr('content')
 
    Eif (statement !== undefined && statement.split(':').length > 1) {
      return statement.split(':')[1].trim().replace('“', '').replace('”', '')
    } else {
      return ''
    }
  }
}
 
export { Instagram }