Advertisement
Guest User

Untitled

a guest
Jun 11th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nim 5.38 KB | None | 0 0
  1. #[
  2. 20 episódios por página
  3. ]#
  4.  
  5. import httpclient, htmlparser, xmltree
  6. import strutils, streams, re
  7. import math, times
  8.  
  9. type SearchItem = object
  10.     title*: string
  11.     url*: string
  12.  
  13. type Information = object
  14.     title*: string
  15.     url*: string
  16.     genre*: string
  17.     author*: string
  18.     director*: string
  19.     company*: string
  20.     lastEpisode*: string
  21.     year*: string
  22.  
  23. type Episode = object
  24.     duration*: TimeInfo # parse(str, "mm:ss")
  25.     url*: string
  26.     image*: string
  27.     title*: string
  28.  
  29. #----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  30. # * $: Overload do método que transforma o objeto em string.
  31. #----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  32. proc `$`(x: SearchItem): string =
  33.     return "<Searchitem title='" & x.title & "' url='" & x.url & "'>"
  34.  
  35. #----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  36. # * $: Overload do método que transforma o objeto em string.
  37. #----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  38. proc `$`(x: Information): string =
  39.     return "<Information title='" & x.title & "' url='" & x.url & "' genre='" & x.genre & "' author='" & x.author &
  40.         "' director='" & x.director & "' company='" & x.company & "' lastEpisode='" & x.lastEpisode & "' year='" & x.year & "'>"
  41.  
  42. #----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  43. # * Search: Busca por um determinado anime no site.
  44. #----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  45. proc search(tag: string): seq[SearchItem] =
  46.     result = @[]
  47.     let url = "http://www.superanimes.com/anime?&letra=" & tag
  48.     var client = newHttpClient()
  49.     var stream = newStringStream(client.getContent(url))
  50.    
  51.     let html: Xmlnode = parseHtml(stream)
  52.     for node in html.findAll("div"):
  53.         if node.attr("class") != "boxLista2Nome" or node.child("a") == nil:
  54.             continue
  55.         if node.child("a")[0].rawtag == "h2":
  56.             var item = SearchItem(title: node[0][0].innerText, url: node[0].attr("href"))
  57.             result.add(item)
  58.  
  59. #----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  60. # * getEpisode: Obtém o endereço do arquivo do episódio.
  61. #----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  62. proc getEpisodes(item: SearchItem, pageIndex: int): seq[Episode] =
  63.     #[var page: int = (index div 20) + 1
  64.     var localIndex: int  = (index mod 20) + 1
  65.     var fullUrl: string = item.url & "?&pagina=" & intToStr(page)]#
  66.  
  67.     var url = item.url & "?&pagina=" & intToStr(pageIndex)
  68.     var client = newHttpClient()
  69.     var stream = newStringStream(client.getContent(url))
  70.  
  71.     let html: XmlNode = parseHtml(stream)
  72.     for node in html.findAll("div"):
  73.         if node.attr("class") != "epsBox" or node.child("div").attr("class") != "epsBoxImg": continue
  74.         let a = node.child("a")
  75.         #let duration: TimeInfo = parse(node.child("div")[0].innerText, "mm:ss")
  76.         let url = a.attr("href")
  77.         let title = a.attr("title")
  78.         let img = a.child("img").attr("src")
  79.  
  80. #----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  81. # * loadAnimeInfo: Obtém a lista de episódios.
  82. #----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  83. proc loadAnimeInfo(item: SearchItem): Information =
  84.     result = Information()
  85.     var client = newHttpClient()
  86.     var stream = newStringStream(client.getContent(item.url))
  87.  
  88.     let html: XmlNode = parseHtml(stream)
  89.     for node in html.findAll("div"):
  90.         if node.attr("class") != "boxAnimeSobre": continue
  91.         var fields: seq[XmlNode] = @[]
  92.         node.findAll("div", fields)
  93.  
  94.         result = Information()
  95.         result.title = item.title
  96.         result.url = item.url
  97.         result.genre = fields[1].innerText.replace("Genero: ", "")
  98.         result.author = fields[2].innerText.replace("Autor: ", "")
  99.         result.director = fields[3].innerText.replace("Direção: ", "")
  100.         result.company = fields[4].innerText.replace("Estudio: ", "")
  101.         result.lastEpisode = replace(fields[7].innerText.replace("Episódios: ", ""), re"\s+")
  102.         result.year = fields[12].innerText.replace("Ano: ", "")
  103.  
  104. let results: seq[SearchItem] = search("Dungeon")
  105. #echo results[0].loadAnimeInfo()
  106. echo results[0].getEpisodes(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement