| 
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 | # File 'lib/Request.rb', line 7
def self.URL(url, method = 'GET', data = nil, retryCount = 0)
    retryCount += 1
    
    uri = URI(url)
    https = Net::HTTP.new(uri.host, uri.port)
    https.use_ssl = true
    if method.upcase == "GET"
        request = Net::HTTP::Get.new(uri)
        request['User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.17.375.766 Safari/537.36';
    else
        request = Net::HTTP::Post.new(uri)
        request['Content-Type'] = 'application/json'
        if !data.nil?
            request.body = JSON.dump(data)
        end
    end
    if !$cookie_sid.nil? && !$cookie_uid.nil?
      request['Cookie'] = "sid=#{$cookie_sid}; uid=#{$cookie_uid}";
    end
    response = https.request(request);
    
        if response.code.to_i == 429
      raise "Error: Too Manay Reqeust, blocked by Medium.\n####################\n### Please try again later.\n### Ref: https://zhgchg.li/posts/en-medium-to-jekyll/#paywall-posts-require-a-medium-account-with-access-permissions-and-cookies-author-or-medium-member \n####################\n### ERROR URL: #{url}"
    elsif response.code.to_i >= 300 && response.code.to_i <= 399 && !response['location'].nil? && response['location'] != ''
        if retryCount >= 10
            raise "Error: Retry limit reached. path: #{url}"
        else
            location = response['location']
            if !location.match? /^(http)/
                location = "#{uri.scheme}://#{uri.host}#{location}"
            end
            
            response = self.URL(location, method, data, retryCount)
        end
    end
    response
end |