import os
import requests
import json
from pymongo import MongoClient
from dotenv import load_dotenv
import urllib.parse
import datetime

load_dotenv()

MONGO = os.getenv("MONGO_URI")
client = MongoClient(MONGO)
db = client.test

keyword = 'nendoroid miku'
item = 200
url = 'https://gql.tokopedia.com/'
data = '[{"operationName":"GlobalNavigation","variables":{"keyword":"' + keyword + '","device":"desktop","size":4},"query":"query GlobalNavigation($keyword: String, $device: String, $size: Int) {\\n  global_search_navigation(keyword: $keyword, device: $device, size: $size) {\\n    data {\\n      title\\n      keyword\\n      nav_template\\n      see_all_applink\\n      see_all_url\\n      list {\\n        name\\n        info\\n        imageURL: image_url\\n        url\\n        __typename\\n      }\\n      __typename\\n    }\\n    __typename\\n  }\\n}\\n"},{"operationName":"SearchProductQuery","variables":{"params":"scheme=https&device=desktop&related=true&st=product&q=' +  urllib.parse.quote(keyword) + '&ob=23&page=1&start=0&rows=60&user_id=5106422&unique_id=7cb8a0d7a9b205f82a528eaa97ece9cf&safe_search=false&source=search&nuq="},"query":"query SearchProductQuery($params: String) {\\n  searchProduct(params: $params) {\\n    source\\n    totalData: count\\n    totalDataText: count_text\\n    additionalParams: additional_params\\n    redirection {\\n      redirectionURL: redirect_url\\n      departmentID: department_id\\n      __typename\\n    }\\n    responseCode: response_code\\n    keywordProcess: keyword_process\\n    suggestion {\\n      suggestion\\n      suggestionCount\\n      currentKeyword\\n      instead\\n      insteadCount\\n      suggestionText: text\\n      suggestionTextQuery: query\\n      __typename\\n    }\\n    related {\\n      relatedKeyword: related_keyword\\n      otherRelated: other_related {\\n        keyword\\n        url\\n        __typename\\n      }\\n      __typename\\n    }\\n    isQuerySafe\\n    ticker {\\n      text\\n      query\\n      __typename\\n    }\\n    catalogs {\\n      id\\n      name\\n      price\\n      priceMin: price_min\\n      priceMax: price_max\\n      countProduct: count_product\\n      description\\n      imageURL: image_url\\n      url\\n      category: department_id\\n      __typename\\n    }\\n    products {\\n      id\\n      name\\n      childs\\n      url\\n      imageURL: image_url\\n      imageURL300: image_url_300\\n      imageURL500: image_url_500\\n      imageURL700: image_url_700\\n      price\\n      priceRange: price_range\\n      category: department_id\\n      categoryID: category_id\\n      categoryName: category_name\\n      categoryBreadcrumb: category_breadcrumb\\n      discountPercentage: discount_percentage\\n      originalPrice: original_price\\n      shop {\\n        id\\n        name\\n        url\\n        isPowerBadge: is_power_badge\\n        isOfficial: is_official\\n        location\\n        city\\n        reputation\\n        clover\\n        __typename\\n      }\\n      wholesalePrice: whole_sale_price {\\n        quantityMin: quantity_min\\n        quantityMax: quantity_max\\n        price\\n        __typename\\n      }\\n      courierCount: courier_count\\n      condition\\n      labels {\\n        title\\n        color\\n        __typename\\n      }\\n      labelGroups: label_groups {\\n        position\\n        type\\n        title\\n        __typename\\n      }\\n      badges {\\n        title\\n        imageURL: image_url\\n        show\\n        __typename\\n      }\\n      isFeatured: is_featured\\n      rating\\n      countReview: count_review\\n      stock\\n      GAKey: ga_key\\n      preorder: is_preorder\\n      wishlist\\n      shop {\\n        id\\n        name\\n        url\\n        goldmerchant: is_power_badge\\n        location\\n        city\\n        reputation\\n        clover\\n        official: is_official\\n        __typename\\n      }\\n      __typename\\n    }\\n    __typename\\n  }\\n}\\n"},{"operationName":"TopadsProductQuery","variables":{"adParams":"st=product&q=' +  urllib.parse.quote(keyword) + '&ob=23&page=1&dep_id=&ep=product&src=search&device=desktop&item=' + item + '&minimum_item=10&user_id=5106422"},"query":"query TopadsProductQuery($adParams: String) {\\n  displayAdsV3(displayParams: $adParams) {\\n    data {\\n      clickTrackUrl: product_click_url\\n      product {\\n        id\\n        name\\n        wishlist\\n        image {\\n          imageUrl: s_ecs\\n          trackerImageUrl: s_url\\n          __typename\\n        }\\n        url: uri\\n        relative_uri\\n        price: price_format\\n        wholeSalePrice: wholesale_price {\\n          quantityMin: quantity_min_format\\n          quantityMax: quantity_max_format\\n          price: price_format\\n          __typename\\n        }\\n        count_talk_format\\n        countReviewFormat: count_review_format\\n        category {\\n          id\\n          __typename\\n        }\\n        preorder: product_preorder\\n        product_wholesale\\n        free_return\\n        isNewProduct: product_new_label\\n        cashback: product_cashback_rate\\n        rating: product_rating\\n        top_label\\n        bottomLabel: bottom_label\\n        labelGroups: label_group {\\n          position\\n          type\\n          title\\n          __typename\\n        }\\n        campaign {\\n          discountPercentage: discount_percentage\\n          originalPrice: original_price\\n          __typename\\n        }\\n        __typename\\n      }\\n      shop {\\n        id\\n        name\\n        domain\\n        location\\n        city\\n        tagline\\n        goldmerchant: gold_shop\\n        gold_shop_badge\\n        official: shop_is_official\\n        lucky_shop\\n        uri\\n        owner_id\\n        is_owner\\n        badges {\\n          title\\n          imageURL: image_url\\n          show\\n          __typename\\n        }\\n        __typename\\n      }\\n      __typename\\n    }\\n    __typename\\n  }\\n}\\n"}]'
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'

response = requests.post('https://gql.tokopedia.com/', headers={'User-Agent': user_agent}, data=data)

# Get results
products = json.loads(response.text)
product = [p for p in products for val in p['data'] if 'searchProduct' in val][0]
product['keyword'] = keyword
product['date'] = datetime.datetime.utcnow()

# Insert a document into a collection
results = db.results
results.insert_one(product)

d9e30cd03eebd89f27df43cc1063a9d643a74de7
<mask token>

<mask token>
load_dotenv()
<mask token>
results.insert_one(product)

<mask token>
load_dotenv()
MONGO = os.getenv('MONGO_URI')
client = MongoClient(MONGO)
db = client.test
keyword = 'nendoroid miku'
item = 200
url = 'https://gql.tokopedia.com/'
data = ('[{"operationName":"GlobalNavigation","variables":{"keyword":"' +
    keyword +
    '","device":"desktop","size":4},"query":"query GlobalNavigation($keyword: String, $device: String, $size: Int) {\\n  global_search_navigation(keyword: $keyword, device: $device, size: $size) {\\n    data {\\n      title\\n      keyword\\n      nav_template\\n      see_all_applink\\n      see_all_url\\n      list {\\n        name\\n        info\\n        imageURL: image_url\\n        url\\n        __typename\\n      }\\n      __typename\\n    }\\n    __typename\\n  }\\n}\\n"},{"operationName":"SearchProductQuery","variables":{"params":"scheme=https&device=desktop&related=true&st=product&q='
     + urllib.parse.quote(keyword) +
    '&ob=23&page=1&start=0&rows=60&user_id=5106422&unique_id=7cb8a0d7a9b205f82a528eaa97ece9cf&safe_search=false&source=search&nuq="},"query":"query SearchProductQuery($params: String) {\\n  searchProduct(params: $params) {\\n    source\\n    totalData: count\\n    totalDataText: count_text\\n    additionalParams: additional_params\\n    redirection {\\n      redirectionURL: redirect_url\\n      departmentID: department_id\\n      __typename\\n    }\\n    responseCode: response_code\\n    keywordProcess: keyword_process\\n    suggestion {\\n      suggestion\\n      suggestionCount\\n      currentKeyword\\n      instead\\n      insteadCount\\n      suggestionText: text\\n      suggestionTextQuery: query\\n      __typename\\n    }\\n    related {\\n      relatedKeyword: related_keyword\\n      otherRelated: other_related {\\n        keyword\\n        url\\n        __typename\\n      }\\n      __typename\\n    }\\n    isQuerySafe\\n    ticker {\\n      text\\n      query\\n      __typename\\n    }\\n    catalogs {\\n      id\\n      name\\n      price\\n      priceMin: price_min\\n      priceMax: price_max\\n      countProduct: count_product\\n      description\\n      imageURL: image_url\\n      url\\n      category: department_id\\n      __typename\\n    }\\n    products {\\n      id\\n      name\\n      childs\\n      url\\n      imageURL: image_url\\n      imageURL300: image_url_300\\n      imageURL500: image_url_500\\n      imageURL700: image_url_700\\n      price\\n      priceRange: price_range\\n      category: department_id\\n      categoryID: category_id\\n      categoryName: category_name\\n      categoryBreadcrumb: category_breadcrumb\\n      discountPercentage: discount_percentage\\n      originalPrice: original_price\\n      shop {\\n        id\\n        name\\n        url\\n        isPowerBadge: is_power_badge\\n        isOfficial: is_official\\n        location\\n        city\\n        reputation\\n        clover\\n        __typename\\n      }\\n      wholesalePrice: whole_sale_price {\\n        quantityMin: quantity_min\\n        quantityMax: quantity_max\\n        price\\n        __typename\\n      }\\n      courierCount: courier_count\\n      condition\\n      labels {\\n        title\\n        color\\n        __typename\\n      }\\n      labelGroups: label_groups {\\n        position\\n        type\\n        title\\n        __typename\\n      }\\n      badges {\\n        title\\n        imageURL: image_url\\n        show\\n        __typename\\n      }\\n      isFeatured: is_featured\\n      rating\\n      countReview: count_review\\n      stock\\n      GAKey: ga_key\\n      preorder: is_preorder\\n      wishlist\\n      shop {\\n        id\\n        name\\n        url\\n        goldmerchant: is_power_badge\\n        location\\n        city\\n        reputation\\n        clover\\n        official: is_official\\n        __typename\\n      }\\n      __typename\\n    }\\n    __typename\\n  }\\n}\\n"},{"operationName":"TopadsProductQuery","variables":{"adParams":"st=product&q='
     + urllib.parse.quote(keyword) +
    '&ob=23&page=1&dep_id=&ep=product&src=search&device=desktop&item=' +
    item +
    '&minimum_item=10&user_id=5106422"},"query":"query TopadsProductQuery($adParams: String) {\\n  displayAdsV3(displayParams: $adParams) {\\n    data {\\n      clickTrackUrl: product_click_url\\n      product {\\n        id\\n        name\\n        wishlist\\n        image {\\n          imageUrl: s_ecs\\n          trackerImageUrl: s_url\\n          __typename\\n        }\\n        url: uri\\n        relative_uri\\n        price: price_format\\n        wholeSalePrice: wholesale_price {\\n          quantityMin: quantity_min_format\\n          quantityMax: quantity_max_format\\n          price: price_format\\n          __typename\\n        }\\n        count_talk_format\\n        countReviewFormat: count_review_format\\n        category {\\n          id\\n          __typename\\n        }\\n        preorder: product_preorder\\n        product_wholesale\\n        free_return\\n        isNewProduct: product_new_label\\n        cashback: product_cashback_rate\\n        rating: product_rating\\n        top_label\\n        bottomLabel: bottom_label\\n        labelGroups: label_group {\\n          position\\n          type\\n          title\\n          __typename\\n        }\\n        campaign {\\n          discountPercentage: discount_percentage\\n          originalPrice: original_price\\n          __typename\\n        }\\n        __typename\\n      }\\n      shop {\\n        id\\n        name\\n        domain\\n        location\\n        city\\n        tagline\\n        goldmerchant: gold_shop\\n        gold_shop_badge\\n        official: shop_is_official\\n        lucky_shop\\n        uri\\n        owner_id\\n        is_owner\\n        badges {\\n          title\\n          imageURL: image_url\\n          show\\n          __typename\\n        }\\n        __typename\\n      }\\n      __typename\\n    }\\n    __typename\\n  }\\n}\\n"}]'
    )
user_agent = (
    'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
    )
response = requests.post('https://gql.tokopedia.com/', headers={
    'User-Agent': user_agent}, data=data)
products = json.loads(response.text)
product = [p for p in products for val in p['data'] if 'searchProduct' in val][
    0]
product['keyword'] = keyword
product['date'] = datetime.datetime.utcnow()
results = db.results
results.insert_one(product)

import os
import requests
import json
from pymongo import MongoClient
from dotenv import load_dotenv
import urllib.parse
import datetime
load_dotenv()
MONGO = os.getenv('MONGO_URI')
client = MongoClient(MONGO)
db = client.test
keyword = 'nendoroid miku'
item = 200
url = 'https://gql.tokopedia.com/'
data = ('[{"operationName":"GlobalNavigation","variables":{"keyword":"' +
    keyword +
    '","device":"desktop","size":4},"query":"query GlobalNavigation($keyword: String, $device: String, $size: Int) {\\n  global_search_navigation(keyword: $keyword, device: $device, size: $size) {\\n    data {\\n      title\\n      keyword\\n      nav_template\\n      see_all_applink\\n      see_all_url\\n      list {\\n        name\\n        info\\n        imageURL: image_url\\n        url\\n        __typename\\n      }\\n      __typename\\n    }\\n    __typename\\n  }\\n}\\n"},{"operationName":"SearchProductQuery","variables":{"params":"scheme=https&device=desktop&related=true&st=product&q='
     + urllib.parse.quote(keyword) +
    '&ob=23&page=1&start=0&rows=60&user_id=5106422&unique_id=7cb8a0d7a9b205f82a528eaa97ece9cf&safe_search=false&source=search&nuq="},"query":"query SearchProductQuery($params: String) {\\n  searchProduct(params: $params) {\\n    source\\n    totalData: count\\n    totalDataText: count_text\\n    additionalParams: additional_params\\n    redirection {\\n      redirectionURL: redirect_url\\n      departmentID: department_id\\n      __typename\\n    }\\n    responseCode: response_code\\n    keywordProcess: keyword_process\\n    suggestion {\\n      suggestion\\n      suggestionCount\\n      currentKeyword\\n      instead\\n      insteadCount\\n      suggestionText: text\\n      suggestionTextQuery: query\\n      __typename\\n    }\\n    related {\\n      relatedKeyword: related_keyword\\n      otherRelated: other_related {\\n        keyword\\n        url\\n        __typename\\n      }\\n      __typename\\n    }\\n    isQuerySafe\\n    ticker {\\n      text\\n      query\\n      __typename\\n    }\\n    catalogs {\\n      id\\n      name\\n      price\\n      priceMin: price_min\\n      priceMax: price_max\\n      countProduct: count_product\\n      description\\n      imageURL: image_url\\n      url\\n      category: department_id\\n      __typename\\n    }\\n    products {\\n      id\\n      name\\n      childs\\n      url\\n      imageURL: image_url\\n      imageURL300: image_url_300\\n      imageURL500: image_url_500\\n      imageURL700: image_url_700\\n      price\\n      priceRange: price_range\\n      category: department_id\\n      categoryID: category_id\\n      categoryName: category_name\\n      categoryBreadcrumb: category_breadcrumb\\n      discountPercentage: discount_percentage\\n      originalPrice: original_price\\n      shop {\\n        id\\n        name\\n        url\\n        isPowerBadge: is_power_badge\\n        isOfficial: is_official\\n        location\\n        city\\n        reputation\\n        clover\\n        __typename\\n      }\\n      wholesalePrice: whole_sale_price {\\n        quantityMin: quantity_min\\n        quantityMax: quantity_max\\n        price\\n        __typename\\n      }\\n      courierCount: courier_count\\n      condition\\n      labels {\\n        title\\n        color\\n        __typename\\n      }\\n      labelGroups: label_groups {\\n        position\\n        type\\n        title\\n        __typename\\n      }\\n      badges {\\n        title\\n        imageURL: image_url\\n        show\\n        __typename\\n      }\\n      isFeatured: is_featured\\n      rating\\n      countReview: count_review\\n      stock\\n      GAKey: ga_key\\n      preorder: is_preorder\\n      wishlist\\n      shop {\\n        id\\n        name\\n        url\\n        goldmerchant: is_power_badge\\n        location\\n        city\\n        reputation\\n        clover\\n        official: is_official\\n        __typename\\n      }\\n      __typename\\n    }\\n    __typename\\n  }\\n}\\n"},{"operationName":"TopadsProductQuery","variables":{"adParams":"st=product&q='
     + urllib.parse.quote(keyword) +
    '&ob=23&page=1&dep_id=&ep=product&src=search&device=desktop&item=' +
    item +
    '&minimum_item=10&user_id=5106422"},"query":"query TopadsProductQuery($adParams: String) {\\n  displayAdsV3(displayParams: $adParams) {\\n    data {\\n      clickTrackUrl: product_click_url\\n      product {\\n        id\\n        name\\n        wishlist\\n        image {\\n          imageUrl: s_ecs\\n          trackerImageUrl: s_url\\n          __typename\\n        }\\n        url: uri\\n        relative_uri\\n        price: price_format\\n        wholeSalePrice: wholesale_price {\\n          quantityMin: quantity_min_format\\n          quantityMax: quantity_max_format\\n          price: price_format\\n          __typename\\n        }\\n        count_talk_format\\n        countReviewFormat: count_review_format\\n        category {\\n          id\\n          __typename\\n        }\\n        preorder: product_preorder\\n        product_wholesale\\n        free_return\\n        isNewProduct: product_new_label\\n        cashback: product_cashback_rate\\n        rating: product_rating\\n        top_label\\n        bottomLabel: bottom_label\\n        labelGroups: label_group {\\n          position\\n          type\\n          title\\n          __typename\\n        }\\n        campaign {\\n          discountPercentage: discount_percentage\\n          originalPrice: original_price\\n          __typename\\n        }\\n        __typename\\n      }\\n      shop {\\n        id\\n        name\\n        domain\\n        location\\n        city\\n        tagline\\n        goldmerchant: gold_shop\\n        gold_shop_badge\\n        official: shop_is_official\\n        lucky_shop\\n        uri\\n        owner_id\\n        is_owner\\n        badges {\\n          title\\n          imageURL: image_url\\n          show\\n          __typename\\n        }\\n        __typename\\n      }\\n      __typename\\n    }\\n    __typename\\n  }\\n}\\n"}]'
    )
user_agent = (
    'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
    )
response = requests.post('https://gql.tokopedia.com/', headers={
    'User-Agent': user_agent}, data=data)
products = json.loads(response.text)
product = [p for p in products for val in p['data'] if 'searchProduct' in val][
    0]
product['keyword'] = keyword
product['date'] = datetime.datetime.utcnow()
results = db.results
results.insert_one(product)

import os
import requests
import json
from pymongo import MongoClient
from dotenv import load_dotenv
import urllib.parse
import datetime

load_dotenv()

MONGO = os.getenv("MONGO_URI")
client = MongoClient(MONGO)
db = client.test

keyword = 'nendoroid miku'
item = 200
url = 'https://gql.tokopedia.com/'
data = '[{"operationName":"GlobalNavigation","variables":{"keyword":"' + keyword + '","device":"desktop","size":4},"query":"query GlobalNavigation($keyword: String, $device: String, $size: Int) {\\n  global_search_navigation(keyword: $keyword, device: $device, size: $size) {\\n    data {\\n      title\\n      keyword\\n      nav_template\\n      see_all_applink\\n      see_all_url\\n      list {\\n        name\\n        info\\n        imageURL: image_url\\n        url\\n        __typename\\n      }\\n      __typename\\n    }\\n    __typename\\n  }\\n}\\n"},{"operationName":"SearchProductQuery","variables":{"params":"scheme=https&device=desktop&related=true&st=product&q=' +  urllib.parse.quote(keyword) + '&ob=23&page=1&start=0&rows=60&user_id=5106422&unique_id=7cb8a0d7a9b205f82a528eaa97ece9cf&safe_search=false&source=search&nuq="},"query":"query SearchProductQuery($params: String) {\\n  searchProduct(params: $params) {\\n    source\\n    totalData: count\\n    totalDataText: count_text\\n    additionalParams: additional_params\\n    redirection {\\n      redirectionURL: redirect_url\\n      departmentID: department_id\\n      __typename\\n    }\\n    responseCode: response_code\\n    keywordProcess: keyword_process\\n    suggestion {\\n      suggestion\\n      suggestionCount\\n      currentKeyword\\n      instead\\n      insteadCount\\n      suggestionText: text\\n      suggestionTextQuery: query\\n      __typename\\n    }\\n    related {\\n      relatedKeyword: related_keyword\\n      otherRelated: other_related {\\n        keyword\\n        url\\n        __typename\\n      }\\n      __typename\\n    }\\n    isQuerySafe\\n    ticker {\\n      text\\n      query\\n      __typename\\n    }\\n    catalogs {\\n      id\\n      name\\n      price\\n      priceMin: price_min\\n      priceMax: price_max\\n      countProduct: count_product\\n      description\\n      imageURL: image_url\\n      url\\n      category: department_id\\n      __typename\\n    }\\n    products {\\n      id\\n      name\\n      childs\\n      url\\n      imageURL: image_url\\n      imageURL300: image_url_300\\n      imageURL500: image_url_500\\n      imageURL700: image_url_700\\n      price\\n      priceRange: price_range\\n      category: department_id\\n      categoryID: category_id\\n      categoryName: category_name\\n      categoryBreadcrumb: category_breadcrumb\\n      discountPercentage: discount_percentage\\n      originalPrice: original_price\\n      shop {\\n        id\\n        name\\n        url\\n        isPowerBadge: is_power_badge\\n        isOfficial: is_official\\n        location\\n        city\\n        reputation\\n        clover\\n        __typename\\n      }\\n      wholesalePrice: whole_sale_price {\\n        quantityMin: quantity_min\\n        quantityMax: quantity_max\\n        price\\n        __typename\\n      }\\n      courierCount: courier_count\\n      condition\\n      labels {\\n        title\\n        color\\n        __typename\\n      }\\n      labelGroups: label_groups {\\n        position\\n        type\\n        title\\n        __typename\\n      }\\n      badges {\\n        title\\n        imageURL: image_url\\n        show\\n        __typename\\n      }\\n      isFeatured: is_featured\\n      rating\\n      countReview: count_review\\n      stock\\n      GAKey: ga_key\\n      preorder: is_preorder\\n      wishlist\\n      shop {\\n        id\\n        name\\n        url\\n        goldmerchant: is_power_badge\\n        location\\n        city\\n        reputation\\n        clover\\n        official: is_official\\n        __typename\\n      }\\n      __typename\\n    }\\n    __typename\\n  }\\n}\\n"},{"operationName":"TopadsProductQuery","variables":{"adParams":"st=product&q=' +  urllib.parse.quote(keyword) + '&ob=23&page=1&dep_id=&ep=product&src=search&device=desktop&item=' + item + '&minimum_item=10&user_id=5106422"},"query":"query TopadsProductQuery($adParams: String) {\\n  displayAdsV3(displayParams: $adParams) {\\n    data {\\n      clickTrackUrl: product_click_url\\n      product {\\n        id\\n        name\\n        wishlist\\n        image {\\n          imageUrl: s_ecs\\n          trackerImageUrl: s_url\\n          __typename\\n        }\\n        url: uri\\n        relative_uri\\n        price: price_format\\n        wholeSalePrice: wholesale_price {\\n          quantityMin: quantity_min_format\\n          quantityMax: quantity_max_format\\n          price: price_format\\n          __typename\\n        }\\n        count_talk_format\\n        countReviewFormat: count_review_format\\n        category {\\n          id\\n          __typename\\n        }\\n        preorder: product_preorder\\n        product_wholesale\\n        free_return\\n        isNewProduct: product_new_label\\n        cashback: product_cashback_rate\\n        rating: product_rating\\n        top_label\\n        bottomLabel: bottom_label\\n        labelGroups: label_group {\\n          position\\n          type\\n          title\\n          __typename\\n        }\\n        campaign {\\n          discountPercentage: discount_percentage\\n          originalPrice: original_price\\n          __typename\\n        }\\n        __typename\\n      }\\n      shop {\\n        id\\n        name\\n        domain\\n        location\\n        city\\n        tagline\\n        goldmerchant: gold_shop\\n        gold_shop_badge\\n        official: shop_is_official\\n        lucky_shop\\n        uri\\n        owner_id\\n        is_owner\\n        badges {\\n          title\\n          imageURL: image_url\\n          show\\n          __typename\\n        }\\n        __typename\\n      }\\n      __typename\\n    }\\n    __typename\\n  }\\n}\\n"}]'
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'

response = requests.post('https://gql.tokopedia.com/', headers={'User-Agent': user_agent}, data=data)

# Get results
products = json.loads(response.text)
product = [p for p in products for val in p['data'] if 'searchProduct' in val][0]
product['keyword'] = keyword
product['date'] = datetime.datetime.utcnow()

# Insert a document into a collection
results = db.results
results.insert_one(product)
