Advertisement
contr0l

!mostsearched - recoded using text file not hash

Dec 3rd, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
mIRC 7.46 KB | None | 0 0
  1. on *:start: {
  2.   set %_mschan #elite-chat | set %_msb !mostsearched | set %_msdir $+($mircdir,mostsearched\) | set %_msitems $+(",%_msdir,msitems.txt,") | whilefix
  3.   if !$exists(%_msdir) { mkdir %_msdir } | if !$exists(%_msitems) { write -c %_msitems }
  4.  
  5.   ; Make a copy of the database daily
  6.   .timermscopy 0 86400 _mscopy
  7.  
  8.   ; Every 10 minutes purge the database of all entries with less than 3 searches for that term. Speeds up processing.
  9.   .timermspurge 0 600 _mspurge
  10. }
  11.  
  12. ; Clean up if unloaded. Deletes database and all backups, and then deletes the 'mostsearched' folder from your $mircdir then cleans up variables.
  13. on *:unload: { var %i $findfile(%_msdir,*.*,0,.remove $1-) | .remove %_msdir  | unset %_ms* }
  14.  
  15. ; whilefix alias uses whilefix.dll so mirc doesnt freeze during big loops.
  16. ; Just add /whilefix every iteration of your loops and mirc wont freeze. (whilefix.dll needs to be in $mircdir)
  17. alias whilefix { dll WhileFix.dll WhileFix . }
  18.  
  19. ; convert spaces to periods.
  20. alias ' { return $replace($1-,$chr(32),$chr(46)) }
  21.  
  22. ; msg chan the toal number of searched items indexed.
  23. alias _mstotal { return $lines(%_msitems) }
  24.  
  25. ;msg channel total number of search terms indexed.
  26. alias mst { msg %_mschan %_msb There are $+(,$bytes($_mstotal,b),) total searches Indexed. }
  27.  
  28. ; Import old data from hash tables into text file.
  29. alias _msimport { var %i $hget(ms,0).item | while %i { whilefix | write %_msitems $hget(ms,%i).data $hget(ms,%i).item | dec %i } }
  30.  
  31. ; backup database once a day in case of data loss.
  32. alias _mscopy { var %mscfn $+($remove(%_msitems,.txt,"),-BACKUP-,$asctime(mmddyy),-,$asctime(hhnntt),.txt) | copy -o %_msitems %mscfn | echo -a %_msb Backup made of the database. Name: $nopath(%mscfn) -Location: $+(,$nofile(%_msitems),) }
  33.  
  34. ; Purge database of all entries that don't have atleast been searched 3 times.
  35. alias _mspurge {
  36.   var %x $_mstotal | while %x {
  37.     whilefix
  38.     if $gettok($read(%_msitems,%x),1,32) <= 3 { write $+(-dl,%x) %_msitems }
  39.     dec %x
  40.   }
  41. }
  42.  
  43. ; Sorts entries in ascending order by the number of searches, and lists top 10.
  44. alias _mslist {
  45.   var %count = 1
  46.   if ($window(@Sort1,state)) { window -c @Sort1 } | if ($window(@Sort2,state)) { window -c @Sort2 }
  47.   window -h @Sort1 | window -h @Sort2
  48.  
  49.   ;write all lines to @Sort1
  50.   while (%count <= $_mstotal) { whilefix | aline @Sort1 $gettok($read(%_msitems,%count),2-,32) $gettok($read(%_msitems,%count),1,32) | inc %count }
  51.  
  52.   ;command to filter and sort the results asscending
  53.   filter -wwucte 2 32 @Sort1 @Sort2 *
  54.  
  55.   ; ** Display the Top 10 Most Searched Terms (or you can change the value below...)
  56.   ; ** - Change value of %v below to change the number of results to display (default is 10).
  57.   var %x = 1, %v = 10 | while (%x <= %v) { whilefix | msg %_mschan $+(,%x,.,)  $gettok($line(@Sort2,%x),1,32) :: Searched: $gettok($line(@Sort2,%x),2,32) times. | inc %x }
  58.  
  59.   window -c @Sort1 | window -c @Sort2
  60. }
  61.  
  62. ; Show list of top 10 most searched terms
  63. alias mss {
  64.   msg %_mschan %_msb :: Listing [Top 10] Most Searched Terms of the $+(,$bytes($_mstotal,b),) Indexed.
  65.   _mslist
  66.   msg %_mschan %_msb :: [EOF]
  67. }
  68.  
  69. on *:text:*:%_mschan:{
  70.   ; flood protection, so each nick can only do !mostsearched once every 5 seconds
  71.   if !%msfld. [ $+ [ $nick ] ] {
  72.     set -u5 %msfld. [ $+ [ $nick ] ] 1
  73.  
  74.     ; change spaces to periods, needs for tokens.
  75.     if $2- { var %msr = $'($2-) }
  76.  
  77.     ; increase the value of the term searched anytime someone does a !s or !search.
  78.     if (($1 == !s || $1 == !search)) && $2- {
  79.       if $read(%_msitems, w, $+(*,%msr,*)) {
  80.         var %msln $readn
  81.  
  82.         ; increase number of times this has been searched
  83.         var %msval $calc($gettok($ifmatch,1,32) + 1), %msval2 $gettok($ifmatch,2-,32)
  84.  
  85.         ; delete the line with the search term.
  86.         write $+(-dl,%msln) %_msitems
  87.  
  88.         ; write a line with new value increased by 1.
  89.         write %_msitems %msval %msval2
  90.         halt
  91.       }
  92.       ; if searched term has never been searched before, add with value of 1
  93.       else write %_msitems 1 %msr
  94.     }
  95.  
  96.     ; if user only types !mostsearched, then run command _mslist (/mss) which lists top 10.
  97.     if $1 == !mostsearched  && !$2- {
  98.       if $_mstotal > 0 { mss | halt }
  99.       else msg %_mschan %_msb :: No Searches Indexed yet...
  100.     }
  101.  
  102.     ; !mostsearched commands
  103.     if $1 == !mostsearched && $2- {
  104.       ; Code for help
  105.       if $2 != merge && $2 == help {
  106.         msg # :: Sending %_msb :: Help Command Reference to $+(,$nick,) via pm.
  107.         msg $nick %_msb :: Help Command Reference
  108.         msg $nick :: If a user only types !mostsearched, then list top 10 most searched terms.
  109.         msg $nick :: If a user types !mostsearched release.name.here (periods required), it will show the number of searches that release has.
  110.         msg $nick :: If an op (@) or halfop (%) types !mostsearched merge release.name1.here release.name2.here  - it will merge the 2 values of the specified releases, then delete release.name1.here. You are merging the first into the second, then deleting the first.
  111.         msg $nick %_msb :: EOF
  112.         halt
  113.       }
  114.  
  115.       ; Code for !mostsearched release.name - to show the number of searches for the specified search term.
  116.       if $2 && $2 != merge && $2 != help {
  117.         if $read(%_msitems, w, $+(*,$2-,*)) {
  118.           var %_msrdn $readn
  119.           msg # %_msb :: Number of searches for $+(,$2-,) is $iif($gettok($read(%_msitems,%_msrdn),1,32) > 0,$ifmatch,0) $+ .
  120.           halt
  121.         }
  122.         else msg # %_msb :: Error: $+(,$2-,) doesn't exist. | halt
  123.       }
  124.     }
  125.  
  126.     ; Merge command for ops and halfops to merge the values of one search term into a 2nd. (adds the 2 values to the second release.name, then deletes the first release.name)
  127.     ; Usage: !mostsearched merge first.release.name second.release.name
  128.     if ($1 == !mostsearched) && ($2 == merge) && ($3) && ($4) {
  129.       ;if $nick isop # || $nick ishop # || $nick isvoice # {
  130.  
  131.       ; no spaces allowed
  132.       if $chr(32) !iswm $3 && $chr(32) !iswm $4 {
  133.  
  134.         ; make sure first search term to merge exists, and set var with the line number
  135.         if $read(%_msitems, w, $+(*,$'($3),*)) {
  136.           var %ms3ln $readn
  137.  
  138.           ; make sure second search term to merge exists, and set var with the line number
  139.           if $read(%_msitems, w, $+(*,$'($4),*)) {
  140.             var %ms4ln $readn
  141.  
  142.             ; increase the value of first search term by adding the valueo f the second search term
  143.             var %mc $calc($gettok($read(%_msitems,%ms3ln),1,32) + $gettok($read(%_msitems,%ms4ln),1,32))
  144.  
  145.             ; delete the first search term merged
  146.             write $+(-dl,%ms3ln) %_msitems
  147.  
  148.             ; add a line with the new merged value
  149.             write %_msitems %mc $'($4)
  150.  
  151.             ; msg chan the merge completed and it's new value
  152.             msg # %_msb :: Merge Successful! $+(,$3,) has been merged with $+(,$'($4),) and the new value is $+(,%mc,.) The first release specified, $+(,$3,) $+ , has been deleted.
  153.             halt
  154.           }
  155.           ; Merge Error Messages
  156.           else msg # %_msb :: Error: Second Release specified, $+(,$4,) doesn't exist. Check your spelling.
  157.         }
  158.         else msg # %_msb :: Error: First Release specified, $+(,$3,) doesn't exist. Check your spelling.
  159.       }
  160.       else msg # %_msb :: Error: Release names indexed do not have spaces in the titles, use periods.
  161.       ;}
  162.     }
  163.   }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement