IOS download multiple files - ios

In the server side, there would be many text and images that I want to download.
The question is I do not want to get those things separately, I would like to pack them into a single file, so that it is easier to utilize pause-and-resume capability.
Should I zip them in server side and unzip the file in mobile side?
Is there any API that I can use to unzip in mobile side?
I am not sure about if my idea is correct or not. Is this the common way of doing this sort of things? Thank you.

Regarding the images, frequently we download them individually, and merely update the UI as stuff is downloaded, rather than forcing the user to wait for everything to download. Or lazy downloads are nice (such as afforded by UIImageView category, like SDWebImage's UIImageView+WebCache), effectively downloading the images as they're needed, that way the user can use the app without needing to wait for everything to download.
Regarding the text, unless they're huge, you can download them together. Rather than zipping a bunch of text files, though, I think post people would be inclined towards some JSON-formatted response that returns the text files in some nice, easy to consume format by your app. There's a point (say, the combined JSON is more than 100k), though, where combining the text files together hits a point of diminishing returns, in which case you might want to pursue individual downloads.
We need more background on what the app does and the breakdown of data (how big are the individual text files, how many, etc.) before providing meaningful counsel. But unless there absolutely no way for the app to function at all until everything is downloaded, I'd be inclined towards separate downloads for images, and some nice JSON-format single feed for the text.
Let's assume for a second that you have some text description of a series of products, an an image associated with each. Then you might have a JSON that looks like:
[
{
"id": 1,
"description": "This product is ...",
"image_url": "http://www.yourserver.com/images/img001.png"
},
{
"id": 2,
"description": "This different product is ...",
"image_url": "http://www.yourserver.com/images/img002.png"
},
{
"id": 3,
"description": "This third product is ...",
"image_url": "http://www.yourserver.com/images/img003.png"
},
{
"id": 4,
"description": "This last product is ...",
"image_url": "http://www.yourserver.com/images/img004.png"
}
]
Your app might download that JSON (in which case it would have all of the text strings and a bunch of image URLs), and then present that in the UI, and where it needs to display the image, use the UIImageView category of SDWebImage to update the image of the UIImageView like so:
[cell.imageView setImageWithURL:[NSURL URLWithString:imageUrlString]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];

Related

Use Apache Tika to convert documents to "sections"

Problem
I am looking to convert documents (primarily pdf and html) to something I can reliably search over in a search database (e.g. Elasticsearch).
To best search over long passages of text, it is often recommended to break up longer text into chunks that can be searched over instead. This gives a better chance of providing relevant results.
A common structure for this is to use hierarchical sections. An example based on Stack Overflow's Wikipedia page looks like this:
{
"sections": {
"title": "Stack Overflow",
"sections": [
{
"text": "Stack Overflow is a question and answer website for professional and enthusiast programmers. It is the flagship site of the Stack Exchange Network."
},
{
"title": "History",
"sections": [
{
"text": "The website was created by Jeff Atwood and Joel Spolsky in 2008"
},
{
"title": "Security breach",
"In early May 2019, an update was deployed to Stack Overflow's development version."
}
]
}
]
}
}
What I've learnt
I stumbled upon Apache Tika after seeing it referenced in the documentation of popular cloud based Search engines (e.g. Algolia's).
It seems like Tika is capable of translating pdfs > html, so it seems like I can initially just focus on converting html > "sections".
My rudimentary understanding of Tika is that to convert in this fashion requires some understanding of the structure of the document. However, in my case I don't necessarily have a predictable document structure.
One area I have seen this done well is in "reader view" type plugins (e.g. Mozilla's). They tend to attempt to format html documents in predictable ways using heuristics.
Question
What is the best way to use Tika to convert generic html documents to hierarchical sections (as shown above) similar to how "reader view" works?

Change Eve Python DOMAIN collection schema dynamically during runtime

I am using the Eve Python API Framework for MongoDB. I am writing a feature that allows my users to edit metadata sections for the documents that they are writing.
Example JSON:
{
"metadata": {
"document_type": ["story"],
"keywords": ["fantasy", "thriller"]
}
}
We have a CMS for the document editor that admins can use to allow them to do things like add new metadata fields for the authors (normal users) to add more information about their posts. For example, the site admin may want to add a field called "additional_authors" which is a list of strings. If they add this section to the frontend we would like some way to add it to the Eve Schema on the backend in real time without restarting the server. It is very important that it be real time and not part of a coding change in Eve or requiring Eve to restart.
Our current hard-coded metadata schema looks like this for the document collection:
{
"metadata": {
"type": "dict",
"schema": {
"document_type": {"type": "list", "required": True},
"keywords": {"type": "list", "required": True}
}
}
}
I understand that we can go with a non-strict approach when writing the "metadata" type dict so that it does not care what is inside but from my understanding this means we would not be able to use "projection" properly meaning that if I only wanted to return "metadata.additional_authors" of all documents through my API call, I would not be able to do so. Also, this would mean that we would have to deal with the required check ourselves using hooks instead of the built-in Eve schema check.
Is there anyway around this issue by essentially having a dynamic schema based on a MongoDB document that we can store the entire collection configuration dict in and retrieve it without restarting the server for it to take effect? Even if this means adding a hook to the new schema_dict collection and calling some internal Eve function I am all ears.
Thank you ahead of time for your help.

Alexa Smart Home Skills IoT Alexa.PlaybackController

I have an IoT device that needs to support various operations, one of which is next from the Alexa.PlaybackController. My device is a multimedia device and requires many of the other Controllers as well. I'm including the Alexa.PlaybackController in the discovery response for my devices like so:
{
"type": "AlexaInterface",
"interface": "Alexa.PlaybackController",
"version": "3",
"supportedOperations": ["Next"],
}
I've also tried:
{
"type": "AlexaInterface",
"interface": "Alexa.PlaybackController",
"version": "3",
"properties": {
"supported": [
{"name": "next"}
]
},
}
but neither work. I get a schema error on CloudWatch:
is not valid under any of the given schemas
Looking below at the schema, I see that PlaybackController indeed is not included inside the schema. However, all of the documentation makes it seem like this should be trivial. I'm wondering if I need to include something else to indicate that playback is something that I need.
Is PlaybackController special in some way and unable to be included in combination with other Controllers? I've tried googling about this schema error but it's too vague and nothing's coming up.
Any help would be much appreciated!
__
EDIT:
I see now that video devices seem to get a different set of available Controllers, but there is still reference to using PlaybackController in a lot of places around the regular Smart Home Skill for entertainment devices. Really hope that it's possible!
So should have probably figure this out sooner. I'm using the python validation class provided by Amazon. Turns out that the schema from the same repo simply doesn't include any reference to Alexa.PlaybackController. Therefore, the validation fails every time with the error about mismatching schemas. Maybe they've added some controllers recently and forgot to update the schema.
I submitted an issue to the Smart Home repo here: https://github.com/alexa/alexa-smarthome/issues/62

How do you format data so that it can be easily consumed by MV* frameworks like Angular, Backbone, Ember, etc.?

I'm asking this question to fill a hole in my knowledge, myself having historically been primarily a front-end developer with little concern for server-side code for the longest time. I basically need some way to structure my data so that all relevant information from multiple tables in my database all exist in one place. So, let's say I have a user profile page for a Rails-based site that will use Angular.js on the client. My Angular code might expect a data model like this:
var user = {
"first_name": "Arkady",
"last_name": "Dracul",
"courses": [
{
"name": "Intro to Chemistry",
"id": "CH101",
},
{
"name": "Intro to Computer Science",
"id": "CS101",
},
{
"name": "Intro to Whatever",
"id": "W101",
}
],
"clubs": [
{
"name": "Salsa",
"id": "SDA"
},
{
"name": "Tango",
"id": "TDA"
}
]
}
How on earth do I actually get the data from the various tables in my database to come out structured like this? Mind you, I'm guessing (!) that I may need to have different data models for different views but am uncertain as to whether that would be a good practice if two views are mostly similar. Really, I'm not sure how to go about structuring data for consumption by the front end. Apart from any answers you provide here, are there any books that provide useful beginner-/intermediate-level information for someone like myself?
I'm not sure what you are asking, but if you are on rails, you can use a someUser.to_json to convert your database object to json.
Beside this, if you are trying to implement an API with rails, I strongly recommend grape https://github.com/intridea/grape. It is in active development, and I love it!
You also can build JSON views along with grape using Rabl https://github.com/nesquena/rabl or json_builder https://github.com/dewski/json_builder

How can I populate a jQuery template (tmpl) using HTML5 Local Storage data

I am trying to build a simple jQuery UI template and populate it with data stored in the localStorage.
I need to set the local storage with a list of guests and have the user edit the list. When clicking update, the changes are sent back to the server.
<ul id="guests">
<li>
Name: ${name} <br />
Phone: ${phone} <br />
Email: ${email}
</li>
</ul>
I am really new at this and have no idea what to do. I am just interested in setting the local storage when the page loads and populating the template.
Can someone please provide a short tutorial?
I thought this is a simple question... Can someone please let me know in case it is not possible at all? Thanks!
you say you want to save the data to localStorage, but also that you want to send modified data to the server.
I would suggest that you divide this problem up into (Part 1) learning how to save locally to localStorage and rendering that content with templating and then (Part 2) learning how to store on a server. I can help you with Part 1, since quite frankly I'm still learning about Part 2 myself.
Okay so, two subtasks:
using localStorage to persist data
using jQuery templates to render data
Using localStorage
You haven't specified where your data is coming from, so I'll assume you have some JSON. For simplicity I'll just use this data:
(You might be wondering why I added content that isn't plain ASCII -- it's just a habit of mine, I believe in testing with realistic text from the get-go. When we finally render this data, it should look right in your browser.)
var philosophers = [
{
"phone": "1-800-123-1937",
"name": "H\u00e9l\u00e8ne Cixous",
"email": "helene#stanford.edu"
},
{
"phone": "1-800-000-0000",
"name": "\u041c\u0438\u0445\u0430\u0438\u0301\u043b \u0411\u0430\u043a\u0443\u0301\u043d\u0438\u043d",
"email": "mikhail#ispitondns.com"
},
{
"phone": "1-800-770-0830",
"name": "Jayar\u0101\u015bi Bha\u1e6d\u1e6da",
"email": "jay#ancientindia.edu"
}
]
So we need to get this into localStorage, just to have some data to start with.
The trick about localStorage is that you can't just directly store JSON objects. You can only store strings. There are some libraries out there designed to improve on this situation, but we'll just convert our objects ourselves. To do that we'll use JSON:
localStorage.philosophers = JSON.stringify(philosophers)
Unsurprisingly, JSON.stringify turns JSON objects into a string, and that can be set directly as an "attribute" of localStorage.
(If you're using an old browser, then you might not have the native JSON object -- there's a library you can include for that too.)
Okay, so now we have some contact data stashed in localStorage with the label of philosophers. (Hey, you never know when you might need to call a philosopher!)
To get that data back out and into a Javascript object we can do something with, we use another JSON method, JSON.parse.
philosophers = JSON.parse(localStorage.philosophers)
This is all pretty artificial, since we've got the philosophers data in the first place, then we stringify it, and then we store it, and then we take it right back out, and then we parse it, and then we're back where we started. But in reality such data will come from some other source -- perhaps an external file or a web service or whatever.
Using templates to render objects
Since you used what looks like jQuery template syntax in your template, I'm going to assume that's the library you're using. The jQuery docs show us how we can render a variable containing some objects (like what we have in our philosophers variable) with a template, here's the key bit of those docs:
// Convert the markup string into a named template
$.template( "summaryTemplate", "<li>${Name}</li>" );
function renderList() {
// Render the movies data using the named template: "summaryTemplate"
$.tmpl( "summaryTemplate", movies ).appendTo( "#moviesList" );
}
Here's one way you can get your template to work (there are other, arguably cleaner methods, but jQuery templates are a topic unto themselves):
var myTemplate = "<li>Name: ${name}<br/>Phone: ${phone}<br/>Email: ${email}</li>";
$.template("contactLi", myTemplate);
That creates a template and stores it in a variable named contentLi. (Note that $.template wants that given variable name given as a string, which strikes me as weird. I find the way jQuery templates names and defines these methods confusing, which is one of the reasons I prefer Mustache for templating. Shrug.)
Also, note that we don't have the ul included in the template, because that's not going to be repeated for each rendered object. Rather, we're going to add the ul as a hook in the markup, and render the assembled template repeatedly as a child of that. Which just takes a single line with jQuery templates, rather nice:
$.tmpl( "contactLi", philosophers ).appendTo( "#guests" );
So there you go, a rendered list.
I know this doesn't answer your whole question but there's a lot here to start with.
Here's an example you can try out, it ends up rendering something like:
Name: Hélène Cixous
Phone: 1-800-123-1937
Email: helene#stanford.edu
Name: Михаи́л Баку́нин
Phone: 1-800-000-0000
Email: mikhail#ispitondns.com
Name: Jayarāśi Bhaṭṭa
Phone: 1-800-770-0830
Email: jay#ancientindia.edu
(Hehe, boy, SO's syntax highlighting doesn't handle that Unicode text very well!)
try AmplifyJS -- can extract your data as json the same way you would as $.getJSON

Resources