Advertisement
Guest User

Untitled

a guest
May 27th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. package com.sinfonier.bolts;
  2.  
  3. import java.text.DateFormat;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Calendar;
  6.  
  7. public class PuntoChatParser extends BaseSinfonierBolt {
  8.  
  9. // Class variables
  10. String htmlfield;
  11.  
  12. // Must implement constructor. Do not touch
  13. public PuntoChatParser(String path) {
  14. super(path);
  15. }
  16.  
  17. @Override
  18. public void userprepare() {
  19. // Initialize your DB Connections or non-serializable classes.
  20. // This method will be executed once.
  21.  
  22. this.htmlfield = this.getParam("htmlfield");
  23. }
  24.  
  25. @Override
  26. public void userexecute() {
  27.  
  28. String htmlstring = (String) this.getField(this.htmlfield);
  29.  
  30. try{
  31. //username parse
  32. String usernameBegin = "<h2 id=\"nick_scheda\" style=\"margin:0px;\">";
  33.  
  34. Integer beginNick = htmlstring.indexOf(usernameBegin);
  35. String username = htmlstring.subSequence(beginNick + usernameBegin.length(), htmlstring.length()).toString();
  36.  
  37. username = username.subSequence(0, username.indexOf("<")).toString();
  38. this.addField("username", username);
  39. //fin username parse
  40. }catch(Exception ex){
  41. ex.printStackTrace();
  42. this.addField("username", "null");
  43. }
  44.  
  45. try{
  46. //location parse
  47. String locationBegin = "class=\"superblu\">";
  48.  
  49. Integer beginLocation = htmlstring.indexOf(locationBegin);
  50. String location = htmlstring.subSequence(beginLocation + locationBegin.length(), htmlstring.length()).toString();
  51.  
  52. location = location.subSequence(0,location.indexOf("<")).toString();
  53. this.addField("location", location);
  54. //fin location parse
  55. }catch(Exception ex){
  56. ex.printStackTrace();
  57. this.addField("location", "null");
  58. }
  59.  
  60. try{
  61. //age parse
  62. String ageBegin = "Ho <b>";
  63.  
  64. Integer beginAge = htmlstring.indexOf(ageBegin);
  65. String age = htmlstring.subSequence(beginAge + ageBegin.length(), htmlstring.length()).toString();
  66.  
  67. age = age.subSequence(0,age.indexOf(" anni")).toString();
  68. this.addField("age", age);
  69. //fin age parse
  70. }catch(Exception ex){
  71. ex.printStackTrace();
  72. this.addField("age", "null");
  73. }
  74.  
  75. try{
  76. //lastConnection parse -
  77. String lastConnectionBegin = "Ultimo avvistamento: ";
  78.  
  79. Integer beginLastConnection = htmlstring.indexOf(lastConnectionBegin);
  80. String lastConnection = htmlstring.subSequence(beginLastConnection + lastConnectionBegin.length(), htmlstring.length()).toString();
  81.  
  82. lastConnection = lastConnection.subSequence(0,lastConnection.indexOf("<")).toString();
  83.  
  84. //Fecha actual
  85. DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
  86. Calendar fechaActual = Calendar.getInstance();
  87. Calendar last_connection = null;
  88.  
  89. //Calculo la fecha de la última conexión
  90. if(lastConnection == "Ormai una meteora!!")
  91. last_connection = fechaActual;
  92. else{
  93. if(lastConnection.contains("giorni")){
  94. Integer diaRestado = Integer.parseInt(lastConnection.substring(0, 1));
  95. fechaActual.add(Calendar.DAY_OF_YEAR, -diaRestado);
  96.  
  97. last_connection = fechaActual;
  98. }
  99. if(lastConnection.contains("mesi")){
  100. Integer mesRestado = Integer.parseInt(lastConnection.substring(0, 1));
  101. fechaActual.add(Calendar.MONTH, -mesRestado);
  102.  
  103. last_connection = fechaActual;
  104. }
  105. if(lastConnection.contains("Non disponibile")){
  106. last_connection = null;
  107. }
  108. }
  109. //end last connection parse
  110. this.addField("last_connection", last_connection);
  111.  
  112. }catch(Exception ex){
  113. ex.printStackTrace();
  114. this.addField("last_connection", "null");
  115. }
  116.  
  117. try{
  118. //bio parse
  119. String bioBegin = "<span class=\"verd12\">";
  120.  
  121. Integer beginBio = htmlstring.indexOf(bioBegin);
  122. String bio = htmlstring.subSequence(beginBio + bioBegin.length(), htmlstring.length()).toString();
  123.  
  124. bio = bio.subSequence(0, bio.indexOf("<")).toString();
  125. //end bio parse
  126. this.addField("bio", bio);
  127.  
  128. }catch(Exception ex){
  129. ex.printStackTrace();
  130. this.addField("bio", "null");
  131. }
  132.  
  133. this.emit();
  134. }
  135.  
  136. @Override
  137. public void usercleanup() {
  138. // Close connections, clean resources
  139. }
  140.  
  141. @Override
  142. public void tickTupleCase() {
  143. // Write tickTuple case. This method will be called every tickTuple.
  144. // Usually is used for flush data, send alarms, window event ...
  145. // If your module not use this feature leave empty
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement