Skip to content

Instantly share code, notes, and snippets.

loop do
$stdout.write "content: #{$stdin.read}"
sleep 1
end
# For given shop, category, competitor shop, old&new prefixes and prefix shop:
# - find urls for category products competitors(by competitorshop_id)
# - generate urls with new prefixes(often 'html' -> 'html?prefix=PR')
# - add urls with prefixes to tasks
# All in one SQL request. Be careful while debugging, young padawan.
# example: shop_id 11245, category_id 1318231, competitorshop_id 12933,
# old_prefix 'pr=Q49', new_prefix 'pr=Q51', prefixshop_id 1390
def fill_prefix_shop_for_competitor(shop_id, category_id, competitorshop_id, old_prefix, new_prefix, prefixshop_id, force = false)
Db.session do |db|
query =<<-SQL
# Not so good
i=0
while i<=colnum-1 do #adding the columns
database.alter_table tabname.to_sym do
add_column colnames[i], String
i+=1
end
end
# Better
@jaturken
jaturken / gist:780f2652cfcef3032647
Last active August 29, 2015 14:18
groups_categories.rb
def create_category(category_node)
url = category_node.xpath("./a[1]/@href").text
name = url.sub("http://catalog.onliner.by/","").delete('/')
name_ru = category_node.xpath("./a[last()]").text
is_new = category_node.xpath("./a[2]/img[@class='img_new']").any?
Category.create({ name: name, name_ru: name_ru, url: url, is_new: is_new })
end
def create_group(group_node)
name = group_node.text.delete("0-9")
def get_info
if storage == 'file'
get_info_from_file
elsif storage == 'mysql'
get_info_from_mysql
elsif storage == 'api'
get_storage_from_api
else
raise RuntimeError, 'Unknown storage'
end
@jaturken
jaturken / request.rb
Last active July 1, 2017 15:20
Curl + useragents
def special_request(url)
user_agents = ['Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:39.0) Gecko/20100101 Firefox/39.0', 'Mozilla/5.0 (Windows NT 6.1; rv:35.0) Gecko/20100101 Firefox/35.0', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36']
data = Curl.get(url) do |http|
http.ssl_verify_peer = false
http.headers["User-Agent"] = user_agents.sample
end
data.perform
data.body_str
end
@jaturken
jaturken / 123.rb
Created October 20, 2015 14:14
123
products_names_from_one_page = page_content.xpath("//a[span[@class='snippet-card__header-text']]").map(&:text)
products_urls_from_one_page = page_content.xpath("//a[span[@class='snippet-card__header-text']]
/@href").map(&:text)
############# #HACK
products_new_one_from_one_page = []
products_names_from_one_page.each do |name|
val = page_content.xpath("//h3[a[span[@class='snippet-card__header-text']
[contains(text(),'#{name}')]]]/div/
div[@class='stickers__sticker stickers__sticker_type_new']
[contains(text(),'Новинка')]").map(&:text)[0]
@jaturken
jaturken / 123.rb
Last active October 20, 2015 14:30
1234
products_data = page_content.xpath("//div[contains(@class, 'snippet-list')]/div[contains(@class, 'snippet-card')]")
products_data.map do |product_data|
name = product_data.xpath(".//span[@clas='snippet-card__header-text']").text
url = product_data.xpath(".//a[span[@class='snippet-card__header-text']]/@href").text
is_new = !product_data.xpath(".//div[@class='stickers__sticker stickers__sticker_type_new']").empty?
products_names << name
products_utls << url
products_new_one << is_new
end
@jaturken
jaturken / rb
Created February 16, 2016 11:09
class BaseReport
def not_implemented *args
fail "Not implemented. Implement in your report class"
end
alias_method :load_db_data, :not_implemented
alias_method :load_parsed_data, :not_implemented
alias_method :validate_data, :not_implemented
alias_method :reparse_data, :not_implemented
alias_method :build_report, :not_implemented
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-78451618-1', 'auto');
ga('send', 'pageview');
</script>