Sync.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // Copyright (C) 2016 Inigo Valentin
  2. //
  3. // This file is part of the Gasteizko Margolariak IOS app.
  4. //
  5. // The Gasteizko Margolariak IOS app is free software: you can
  6. // redistribute it and/or modify it under the terms of the
  7. // GNU General Public License as published by the Free Software
  8. // Foundation, either version 3 of the License, or (at your
  9. // option) any later version.
  10. //
  11. // The Gasteizko Margolariak IOS app is distributed in the
  12. // hope that it will be useful, but WITHOUT ANY WARRANTY;
  13. // without even the implied warranty of MERCHANTABILITY or
  14. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  15. // Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public
  18. // License along with the Gasteizko Margolariak IOS app.
  19. // If not, see <http://www.gnu.org/licenses/>.
  20. import Foundation
  21. import CoreData
  22. import UIKit
  23. /**
  24. Class to handle server sync.
  25. */
  26. class Sync{
  27. var cnt: NSManagedObjectContext
  28. /**
  29. Starts the sync process.
  30. */
  31. init(cnt: NSManagedObjectContext){
  32. self.cnt = cnt
  33. let url = buildUrl();
  34. sync(url: url)
  35. }
  36. /**
  37. Builds the URL to perform the sync against.
  38. :returns: The URL.
  39. */
  40. func buildUrl() -> URL{
  41. let url = URL(string: "https://margolariak.com/API/v1/sync.php?client=com.margolariak.app")
  42. //TODO add parameters
  43. return url!
  44. }
  45. /**
  46. Performs an asynchronous sync.
  47. It fethes the info from the server and stores as Core Data
  48. :param: url The url to sync.
  49. */
  50. func sync(url: URL){
  51. //Synchronously get data
  52. let task = URLSession.shared.dataTask(with: url) { data, response, error in
  53. guard error == nil else {
  54. print("error")
  55. return
  56. }
  57. guard let data = data else {
  58. print("Data is empty")
  59. return
  60. }
  61. print("Got the Sync JSON")
  62. var strData = String(data:data, encoding: String.Encoding.utf8)
  63. let dataIdx = strData?.indexOf(target: "{\"data\"")
  64. strData = strData!.subStr(start: dataIdx!, end: strData!.length - 1)
  65. //TODO: do something with versions
  66. //Get tables
  67. let dataActivity : [String] = self.getTable(data: strData!, table: "activity")
  68. //let dataActivityComment : [String] = self.getTable(data: strData!, table: "activity_comment")
  69. //let dataActivityImage : [String] = self.getTable(data: strData!, table: "activity_image")
  70. //let dataActivityTag : [String] = self.getTable(data: strData!, table: "activity_tag")
  71. //let dataAlbum : [String] = self.getTable(data: strData!, table: "album")
  72. //let dataFestival : [String] = self.getTable(data: strData!, table: "festival")
  73. //let dataFestivalDay : [String] = self.getTable(data: strData!, table: "festival_day")
  74. //let dataFestivalEvent : [String] = self.getTable(data: strData!, table: "festival_event")
  75. //let dataFestivalEventImage : [String] = self.getTable(data: strData!, table: "festival_event_image")
  76. //let dataFestivalOffer : [String] = self.getTable(data: strData!, table: "festival_offer")
  77. //let dataPeople : [String] = self.getTable(data: strData!, table: "people")
  78. //let dataPhoto : [String] = self.getTable(data: strData!, table: "photo")
  79. //let dataPhotoAlbum : [String] = self.getTable(data: strData!, table: "photo_album")
  80. //let dataPhotoComment : [String] = self.getTable(data: strData!, table: "photo_comment")
  81. //let dataPlace : [String] = self.getTable(data: strData!, table: "place")
  82. //let dataPost : [String] = self.getTable(data: strData!, table: "post")
  83. //let dataPostComment : [String] = self.getTable(data: strData!, table: "post_comment")
  84. //let dataPostImage : [String] = self.getTable(data: strData!, table: "post_image")
  85. //let dataPostTag : [String] = self.getTable(data: strData!, table: "post_tag")
  86. //let dataSponsor : [String] = self.getTable(data: strData!, table: "sponsor")
  87. //One by one, save the received data
  88. self.saveTableActivity(entries : dataActivity)
  89. }
  90. task.resume()
  91. }
  92. func getTable(data: String, table: String) -> [String]{
  93. //Does the table exists?
  94. if data.indexOf(target: table) == nil{
  95. print("No data in table \(table)")
  96. return [String]()
  97. }
  98. let iS : Int? = data.indexOf(target: table)
  99. var str = data.subStr(start: iS!, end: data.length - 1)
  100. let iE : Int? = str.indexOf(target: "}]}")!
  101. str = str.subStr(start: 0, end: iE!)
  102. str = str.subStr(start: table.length + 3,end: str.length - 1)
  103. //Separate the entries
  104. var entries : [String] = str.components(separatedBy: "},{")
  105. //Modify the first and the last one to remove bracers
  106. entries[0] = entries[0].subStr(start: 1, end: entries[0].length - 1)
  107. entries[entries.count - 1] = entries[entries.count - 1].subStr(start: 0, end: entries[entries.count - 1].length - 2)
  108. //Return the array
  109. return entries
  110. }
  111. func saveTableActivity(entries : [String]){
  112. let dateFormatter = DateFormatter()
  113. dateFormatter.timeZone = TimeZone.ReferenceType.local
  114. let context = self.cnt //getContext()
  115. let entity = NSEntityDescription.entity(forEntityName: "Activity", in: context)
  116. var transc: NSManagedObject
  117. //TODO: Delete all previous entries
  118. //Loop new entries
  119. for entry in entries{
  120. //Get id
  121. var str = entry
  122. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  123. //print("Id: \(id)")
  124. //Get permalink
  125. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  126. let permalink : String = str.subStr(start : str.indexOf(target : "\"permalink\":")! + 13, end : str.indexOf(target : ",\"")! - 2)
  127. //print("Permalink: \(permalink)")
  128. //Get date
  129. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  130. dateFormatter.dateFormat = "yyyy-MM-dd"
  131. let date = dateFormatter.date(from : str.subStr(start : str.indexOf(target : "\"date\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  132. //print("Date: \(date)")
  133. //Get city
  134. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  135. let city : String = str.subStr(start : str.indexOf(target : "\"city\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  136. //print("City: \(city)")
  137. //Get title_es
  138. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  139. let title_es : String = str.subStr(start : str.indexOf(target : "\"title_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  140. //print("Title (es): \(title_es)")
  141. //Get title_en
  142. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  143. let title_en : String = str.subStr(start : str.indexOf(target : "\"title_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  144. //print("Title (en): \(title_en)")
  145. //Get title_eu
  146. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  147. let title_eu : String = str.subStr(start : str.indexOf(target : "\"title_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  148. //print("Title (eu): \(title_eu)")
  149. //Get text_es
  150. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  151. let text_es : String = str.subStr(start : str.indexOf(target : "\"text_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  152. //print("Text (es): \(text_es)")
  153. //Get text_eu
  154. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  155. let text_eu : String = str.subStr(start : str.indexOf(target : "\"text_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  156. //print("Text (eu): \(text_eu)")
  157. //Get text_en
  158. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  159. let text_en : String = str.subStr(start : str.indexOf(target : "\"text_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  160. //print("Text (en): \(text_en)")
  161. //Get after_es
  162. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  163. var after_es : String = str.subStr(start : str.indexOf(target : "\"after_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  164. if (after_es == "ul"){ //From "null"
  165. after_es = ""
  166. }
  167. //print("After (es): \(after_es)")
  168. //Get after_en
  169. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  170. var after_en : String = str.subStr(start : str.indexOf(target : "\"after_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  171. if (after_en == "ul"){ //From "null"
  172. after_en = ""
  173. }
  174. //print("After (en): \(after_en)")
  175. //Get after_eu
  176. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  177. var after_eu : String = str.subStr(start : str.indexOf(target : "\"after_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  178. if (after_eu == "ul"){ //From "null"
  179. after_eu = ""
  180. }
  181. //print("After (eu): \(after_eu)")
  182. //Get price
  183. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  184. let price : Int = Int(str.subStr(start : str.indexOf(target : "\"price\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  185. //print("Price: \(price)")
  186. //Get inscription
  187. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  188. let inscription : Int = Int(str.subStr(start : str.indexOf(target : "\"inscription\":")! + 15, end : str.indexOf(target : ",\"")! - 2))!
  189. //print("Inscription: \(inscription)")
  190. //Get max_people
  191. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  192. let maxPeople : Int = Int(str.subStr(start : str.indexOf(target : "\"max_people\":")! + 14, end : str.indexOf(target : ",\"")! - 2))!
  193. //print("Max People: \(maxPeople)")
  194. //Get album
  195. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  196. let albumPre = str.subStr(start : str.indexOf(target : "\"album\":")! + 9, end : str.length - 2)
  197. var album = -1
  198. if (albumPre != "ul"){ //From "null"
  199. album = Int(albumPre)!
  200. }
  201. //print("Album: \(album)")
  202. //Skipping dtime
  203. //Skipping comments
  204. //TODO: Save CoreData
  205. transc = NSManagedObject(entity: entity!, insertInto: context)
  206. //set the entity values
  207. transc.setValue(id, forKey: "id")
  208. transc.setValue(permalink, forKey: "permalink")
  209. transc.setValue(date, forKey: "date")
  210. transc.setValue(city, forKey: "city")
  211. transc.setValue(title_es, forKey: "title_es")
  212. transc.setValue(title_en, forKey: "title_en")
  213. transc.setValue(title_eu, forKey: "title_eu")
  214. transc.setValue(text_es, forKey: "text_es")
  215. transc.setValue(text_en, forKey: "text_en")
  216. transc.setValue(text_eu, forKey: "text_eu")
  217. transc.setValue(after_es, forKey: "after_es")
  218. transc.setValue(after_en, forKey: "after_en")
  219. transc.setValue(after_eu, forKey: "after_eu")
  220. transc.setValue(price, forKey: "price")
  221. transc.setValue(inscription, forKey: "inscription")
  222. transc.setValue(maxPeople, forKey: "max_people")
  223. if (album != -1){
  224. transc.setValue(album, forKey: "album")
  225. }
  226. do {
  227. try context.save()
  228. } catch let error as NSError {
  229. print("Could not store Activity with id \(id) \(error), \(error.userInfo)")
  230. } catch {
  231. }
  232. }
  233. }
  234. }