Sha256: dd14b3c218027d816ed22004300c6d2d755ddb06e606b1502c5c6c1519aca8c1

Contents?: true

Size: 1.85 KB

Versions: 34

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

module Onebox
  module Engine
    class WechatMpOnebox
      include Engine
      include LayoutSupport
      include HTML

      always_https
      matches_regexp(/^https?:\/\/mp\.weixin\.qq\.com\/s.*$/)

      def tld
        @tld || @@matcher.match(@url)["tld"]
      end

      def http_params
        {
          'User-Agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A405 Safari/7534.48.3',
          'Accept-Encoding' => 'plain'
        }
      end

      private

      def extract_script_value(var_name)
        if (script_elem = raw.css("script").select { |script| script.inner_text.include? "var #{var_name} = " }) && script_elem.any?
          e = Nokogiri::HTML(script_elem[0].inner_text.match(/var\s+#{Regexp.quote(var_name)}\s+=\s+"(.*?)";/)[1])
          return CGI::unescapeHTML(e.text.scan(/(?:\\x([a-f0-9]{2}))|(.)/i).map { |x| x[0] ? [x[0].to_i(16)].pack('U') : x[1] }.join)
        end
      end

      # TODO need to handle hotlink protection from wechat
      def image
        if banner_image = extract_script_value("msg_cdn_url")
          return banner_image
        end

        if (main_image = raw.css("img").select { |img| not img['class'] }) && main_image.any?
          attributes = main_image.first.attributes

          return attributes["data-src"].to_s if attributes["data-src"]
        end
      end

      def data
        title = CGI.unescapeHTML(raw.css("title").inner_text)
        by_info = CGI.unescapeHTML(raw.css("span.rich_media_meta_text.rich_media_meta_nickname").inner_text)

        result = {
          link: extract_script_value("msg_link") || link,
          title: title,
          image: image,
          description: extract_script_value("msg_desc"),
          by_info: by_info
        }

        result
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
onebox-1.9.23 lib/onebox/engine/wechat_mp_onebox.rb
onebox-1.9.22 lib/onebox/engine/wechat_mp_onebox.rb
onebox-1.9.21 lib/onebox/engine/wechat_mp_onebox.rb
onebox-1.9.20 lib/onebox/engine/wechat_mp_onebox.rb
onebox-1.9.19 lib/onebox/engine/wechat_mp_onebox.rb
onebox-1.9.18 lib/onebox/engine/wechat_mp_onebox.rb
onebox-1.9.17 lib/onebox/engine/wechat_mp_onebox.rb
onebox-1.9.16 lib/onebox/engine/wechat_mp_onebox.rb
onebox-1.9.15 lib/onebox/engine/wechat_mp_onebox.rb
onebox-1.9.14 lib/onebox/engine/wechat_mp_onebox.rb
onebox-1.9.13 lib/onebox/engine/wechat_mp_onebox.rb
onebox-1.9.12 lib/onebox/engine/wechat_mp_onebox.rb
onebox-1.9.11 lib/onebox/engine/wechat_mp_onebox.rb
onebox-1.9.10 lib/onebox/engine/wechat_mp_onebox.rb
onebox-1.9.9 lib/onebox/engine/wechat_mp_onebox.rb
onebox-1.9.8 lib/onebox/engine/wechat_mp_onebox.rb
onebox-1.9.7 lib/onebox/engine/wechat_mp_onebox.rb
onebox-1.9.6 lib/onebox/engine/wechat_mp_onebox.rb
onebox-1.9.5 lib/onebox/engine/wechat_mp_onebox.rb
onebox-1.9.4 lib/onebox/engine/wechat_mp_onebox.rb