Advertisement
quenti77

tweetPanel

Dec 23rd, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.53 KB | None | 0 0
  1. -- Loading API
  2. os.loadAPI("json")
  3.  
  4. -- Change if your config is different
  5. local info = {
  6.     monitor = "back",
  7.     noteblock = "top",
  8.     baseUrl = "YOUR LINK"
  9. }
  10.  
  11. local research = { YOUR HTAG AND OTHER }
  12. local authorPack = { Change Color for name }
  13.  
  14. local running = true
  15. local counter = 0
  16. local leftTweets = 0
  17. local sep = ""
  18.  
  19. local mon = peripheral.wrap(info.monitor)
  20. local nb = peripheral.wrap(info.noteblock)
  21. local tweets = {}
  22.  
  23. term.redirect(mon)
  24. local size = {term.getSize()}
  25. local mainWindow = window.create(term.current(), 1, 3, size[1], size[2] - 1, true)
  26.  
  27. local function update(search)
  28.     local url = info.baseUrl.."updateTweet.php?search="..search
  29.     local resultHttp = http.get(url)
  30.    
  31.     local content = json.decode(resultHttp.readAll())
  32.     resultHttp.close()
  33.  
  34.     return content
  35. end
  36.  
  37. local function getTweet()
  38.     local url = info.baseUrl.."getAndReadTweets.php"
  39.     local resultHttp = http.get(url)
  40.    
  41.     local content = json.decode(resultHttp.readAll())
  42.     resultHttp.close()
  43.  
  44.     return content
  45. end
  46.  
  47. local function isTweetExist(id_search, tab)
  48.     local counterNoStop = 0
  49.  
  50.     for k, v in pairs(tab) do
  51.         counterNoStop = counterNoStop + 1
  52.  
  53.         if counterNoStop > 100 then
  54.             counterNoStop = 0
  55.             os.sleep(0.1)
  56.         end
  57.  
  58.         if v.id_tweet == id_search then
  59.             return true
  60.         end
  61.     end
  62.  
  63.     return false
  64. end
  65.  
  66. local function isAuthorExist(authorSearch, tab)
  67.     for k, v in pairs(tab) do
  68.         if v == authorSearch then
  69.             return true
  70.         end
  71.     end
  72.  
  73.     return false
  74. end
  75.  
  76. local function updateBDD()
  77.     while running do
  78.         for k, v in pairs(research) do
  79.             update(v)
  80.         end
  81.  
  82.         os.sleep(20)
  83.     end
  84. end
  85.  
  86. local function updateTweets()
  87.     while running do
  88.         local result = getTweet()
  89.  
  90.         if result.nbTweetRead > 0 then
  91.             counter = 0
  92.             leftTweets = result.nbTweetLeft
  93.  
  94.             for k, v in pairs(result.data) do
  95.                 if not isTweetExist(v.id_tweet, tweets) then
  96.                     local contentModif = v.content
  97.                     contentModif = contentModif:gsub("u00e9","e")
  98.                     contentModif = contentModif:gsub("u00e7","c")
  99.                     contentModif = contentModif:gsub("u00e0","a")
  100.                     contentModif = contentModif:gsub("u00a0"," ")
  101.  
  102.                     local tweet = {
  103.                         id_tweet = v.id_tweet,
  104.                         content = contentModif,
  105.                         author = v.author,
  106.                         search = v.search,
  107.                         created = v.created
  108.                     }
  109.  
  110.                     table.insert(tweets, tweet)
  111.                 end
  112.             end
  113.         else
  114.             counter = counter + 1
  115.  
  116.             if counter > 25 then
  117.                 counter = 20
  118.             end
  119.         end
  120.  
  121.         os.sleep(1)
  122.     end
  123. end
  124.  
  125. local function drawIHM()
  126.     while running do
  127.  
  128.         term.redirect(mon)
  129.         term.setCursorPos(1, 1)
  130.         term.setTextColor(colors.white)
  131.         term.setBackgroundColor(colors.black)
  132.         term.clearLine()
  133.         term.setCursorPos(1, 1)
  134.         print(" Restant : ", #tweets, " # Non lu : ", leftTweets)
  135.         term.redirect(mainWindow)
  136.  
  137.         local ts = tweets[1]
  138.  
  139.         if ts ~= nil then
  140.             local sc = math.pow(2, math.random(2, 14))
  141.  
  142.             term.setTextColor(sc)
  143.             term.setBackgroundColor(colors.black)
  144.  
  145.             if isAuthorExist(ts.author, authorPack) then
  146.                 term.setBackgroundColor(colors.white)
  147.             end
  148.  
  149.             print(sep)
  150.             if #tweets < 20 then os.sleep(0.1) end
  151.             print(" "..ts.content)
  152.             if #tweets < 40 then os.sleep(0.1) end
  153.             print(" From : "..ts.author)
  154.             if #tweets < 100 then os.sleep(0.1) end
  155.             print(sep)
  156.             if #tweets < 300 then os.sleep(0.1) end
  157.             nb.playNote(0, math.random(1, 24))
  158.         end
  159.  
  160.         table.remove(tweets, 1)
  161.  
  162.         os.sleep(0.1)
  163.     end
  164. end
  165.  
  166. local sx, sy = term.getSize()
  167. for i = 1, sx do
  168.     sep = sep.."-"
  169. end
  170.  
  171. term.clear()
  172. term.setCursorPos(1, 1)
  173. term.redirect(mainWindow)
  174.  
  175. parallel.waitForAll(updateBDD, updateTweets, drawIHM)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement