Class: Coupang

Inherits:
Object
  • Object
show all
Defined in:
lib/deltago.rb

Instance Method Summary collapse

Constructor Details

#initializeCoupang

Returns a new instance of Coupang.


1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
# File 'lib/deltago.rb', line 1408

def initialize
    @start_chrome = Thread.new do
        begin
            system('"'+"C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"+'"'+" https://naver.com/ --remote-debugging-port=9222 --user-data-dir=C:/ChromeTEMP")
            system('"'+"C:/Program Files/Google/Chrome/Application/chrome.exe"+'"'+" https://naver.com/ --remote-debugging-port=9222 --user-data-dir=C:/ChromeTEMP")
        rescue => exception
            puts exception
        end
    end
    sleep(3)
    Selenium::WebDriver::Chrome.driver_path = './chromedriver.exe'
    options = Selenium::WebDriver::Chrome::Options.new
    options.add_option("debuggerAddress", "127.0.0.1:9222")
    @driver = Selenium::WebDriver.for :chrome, options: options
    @driver.manage.window.maximize
    @wait = Selenium::WebDriver::Wait.new(:timeout => 5)
end

Instance Method Details

#get_product_data(name) ⇒ Object


1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
# File 'lib/deltago.rb', line 1456

def get_product_data(name)
    json = {
        'page' => {
            'pageNumber' => 0,
            'size' => 36
        },
        'deliveryTypes' => [],
        'filter' => name
    }

    http = HTTP.cookies(@cookie).post('https://partners.coupang.com/api/v1/search', :json => json)
    data = JSON.parse(http.to_s)['data']['products']
    puts data
    return data
end

#get_product_data2(where) ⇒ Object


1472
1473
1474
1475
1476
1477
# File 'lib/deltago.rb', line 1472

def get_product_data2(where)
    http = HTTP.cookies(@cookie).get('https://partners.coupang.com/api/v1/discovery/'+where+'?limit=20')
    data = JSON.parse(http.to_s)['data']['products']
    puts data
    return data
end

#get_product_url(product_data) ⇒ Object


1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
# File 'lib/deltago.rb', line 1479

def get_product_url(product_data)
    json = {
        'product' => {
            'deliveryBadgeImage' => product_data['deliveryBadgeImage'],
            'discountRate' => nil,
            'image' => product_data['image'],
            'itemId' => product_data['itemId'],
            'originPrice' => product_data['originPrice'],
            'productId' => product_data['productId'],
            'salesPrice' => product_data['salesPrice'],
            'title' => product_data['title'],
            'type' => product_data['type'],
            'vendorItemId' => product_data['vendorItemId']
        }
    }
    begin
        http = http = HTTP.headers('x-token' => @cookie['AFATK']).cookies(@cookie).post('https://partners.coupang.com/api/v1/banner/iframe/url', :json => json)
        json = JSON.parse(http.to_s)
        puts json
        url = json['data']['shortUrl']
    rescue => exception
        puts exception
        puts '10초후 재시도 http error...'
        sleep(10)
        retry
    end
    puts url
    return url
end

#login(user_id, user_pw) ⇒ Object


1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
# File 'lib/deltago.rb', line 1426

def (user_id,user_pw)
    @driver.get('https://partners.coupang.com/')
    begin
        sleep(3)
        element = @wait.until { @driver.find_element(:xpath, '//*[@id="app-header"]/div[2]/div/button[1]') }
        element.click

        e = @wait.until { @driver.find_element(:xpath, '//*[@id="login-email-input"]') }
        e.send_keys(user_id)
        @driver.find_element(:xpath, '//*[@id="login-password-input"]').send_keys(user_pw)
        sleep(1)
        #@driver.find_element(:xpath, '/html/body/div[1]/div/div/form/div[4]/label/span').click
        #sleep(1)
        @driver.find_element(:xpath, '/html/body/div[1]/div/div/form/div[5]/button').click
        sleep(5)

    rescue => exception
        puts exception
        puts 'logined'
    end
    sleep(2)
    @cookie = Hash.new
    @driver.manage.all_cookies.each do |cookie|
        @cookie[cookie[:name]] = cookie[:value]
    end
    puts @cookie
    sleep(2)
    @driver.close
end