Advertisement
Guest User

Untitled

a guest
Jun 25th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 4.02 KB | None | 0 0
  1. proc handle_bgexec {callback chan} {
  2.   append ::bgexec_data($chan) [read $chan]
  3.   if {[eof $chan]} {
  4.     {*}$callback $::bgexec_data($chan)
  5.     close $chan
  6.     unset ::bgexec_data($chan)
  7.   }
  8. }
  9.  
  10. proc bgexec {callback args} {
  11.   set chan [open "| $args" r]
  12.   fconfigure $chan -blocking false
  13.   fileevent $chan readable [list handle_bgexec $callback $chan]
  14.   return
  15. }
  16. set U 0
  17. proc job_done {url result} {
  18.   incr ::U
  19.   if {$result eq "-1" || $result eq "null"} {
  20.       puts "Error: $url"
  21.   } else {
  22.     #puts $result
  23.         puts "OK -- $url"
  24.     app "$result\n"
  25.   }
  26.   puts "\#$::U"
  27.   set ::LAST($url) 1
  28. }
  29. console show
  30.  
  31. proc app {txt} {
  32.     set fp [open "products.txt" a+]
  33.     puts $fp $txt
  34.   puts $fp "~~~~~"
  35.     close $fp
  36. }
  37.  
  38. proc generateExport {} {
  39.     set header {id;Title;_vc_post_settings;pp_sidebar_layout;pp_woo_thumbnail_style;slide_template;_thumbnail_id;pp_parallax_opacity;pp_woo_parallax_type;_wc_rating_count;_wc_review_count;_wc_average_rating;desc;desc-short;image;price;second_price;categ}
  40.     set lineOut {ID;TITLE;"a:1:{s:10:""vc_grid_id"";a:0:{}}";full-width;vertical;default;2624;0;default;a:0:{};0;0;DESC;DESCSHORT;IMG;PRICE;;CATEG }"
  41.  
  42.  
  43.    set incom [open "1.txt" r]
  44.    set ::exportFile "exp.csv"
  45.  
  46.    set fp [open $::exportFile w+]
  47.    fconfigure $fp
  48.  
  49.    puts $fp $header
  50.    set id 1
  51.  
  52.    while {[gets $incom line] != -1} {
  53.        regsub -all -- " " $line " " line
  54.        regsub -all -- ";" $line "***" line
  55.        if {[string match {PRICE:*} $line]} {
  56.            regexp {PRICE:(.*)} $line => price
  57.            regsub "PRICE" $lineOut $price lineOut
  58.        }
  59.        if {[string match {TITLE:*} $line]} {
  60.            regexp {TITLE:(.*)} $line => title
  61.            regsub {\([0-9A-Z]{8}\)} $title "" title
  62.            regsub "TITLE" $lineOut $title lineOut
  63.        }
  64.        if {[string match {IMG:*} $line]} {
  65.            regexp {IMG:(.*)} $line => img
  66.            regsub "IMG" $lineOut $img lineOut
  67.        }
  68.        if {[string match {DESC:*} $line]} {
  69.            regexp {DESC:(.*)} $line => desc
  70.            regsub "DESC" $lineOut $desc lineOut
  71.        }
  72.        if {[string match {DESCSHORT:*} $line]} {
  73.            regexp {DESCSHORT:(.*)} $line => desc_short
  74.            regsub "DESCSHORT" $lineOut $desc_short lineOut
  75.        }
  76.        if {[string match {CATEG:*} $line]} {
  77.            regexp {CATEG:(.*)} $line => ct
  78.            regsub "CATEG" $lineOut $ct lineOut
  79.        }
  80.  
  81.        if {$line == "~~~~~"} {
  82.            regsub "ID" $lineOut $id lineOut
  83.            incr id
  84.            #regsub -all -- "%br%" $lineOut "\r\n" lineOut
  85.            puts $fp $lineOut
  86.            set lineOut {ID;TITLE;"a:1:{s:10:""vc_grid_id"";a:0:{}}";full-width;vertical;default;2624;0;default;a:0:{};0;0;DESC;DESCSHORT;IMG;PRICE;;CATEG }
  87.        }
  88.    }
  89.  
  90.    close $fp
  91.    close $incom
  92. }
  93.  
  94.  
  95.  
  96.  
  97. if {[file exist "products.txt"]} {file delete "products.txt"}
  98.  
  99. set fp [open "urls.txt" r]
  100. set urls [read $fp]
  101. close $fp
  102. set ::lastCat {!!!!!}
  103.  
  104. foreach url [split $urls "\n"] {
  105.  if {[string length [string trim $url]] < 3} continue;
  106.  if {[string index $url 0] eq "!"} {
  107.    set ::lastCat [string range $url 1 end]
  108.    continue
  109.  }
  110.  set ::LAST($url) 0
  111.  puts $url
  112.  if {[catch {bgexec "job_done $url" phantomjs.exe --output-encoding=cp1251 --disk-cache=true "parser.js" $url "$::lastCat"}]} {
  113.    puts "ERROR $url"
  114.  }
  115.  vwait ::LAST($url)
  116.  update idletasks
  117. }
  118.  
  119. set incom [open "products.txt" r]
  120. set fp [open 1.txt w+]
  121. fconfigure $fp -encoding utf-8
  122. while {[gets $incom line] != -1} {
  123.         if {[string match {PRICE:*} $line]} {
  124.             puts $fp $line
  125.    } elseif {[string match {TITLE:*} $line]} {
  126.         puts $fp $line
  127.    } elseif {[string match {IMG:*} $line]} {
  128.         puts $fp $line
  129.    } elseif {[string match {CATEG:*} $line]} {
  130.         puts $fp $line
  131.    } elseif {[string match {DESC:*} $line]} {
  132.         puts $fp $line
  133.    } elseif {[string match {DESCSHORT:*} $line]} {
  134.      puts $fp $line
  135.    } elseif {$line == "~~~~~"} {
  136.         puts $fp "~~~~~\n"
  137.     }
  138. }
  139. close $fp
  140.  
  141. generateExport
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement