Reply to topic

[SOLVED] Help with comic engine

User avatar TheBlackCat
Registered Member
Posts
2945
Karma
8
OS

[SOLVED] Help with comic engine

Sun Jan 18, 2009 9:06 pm
I am trying to write a comic engine. I know there is standing offer to make such engines, but I would prefer to do it myself. That being said, I am having some difficulty. I think it may be a problem with my regular expressions. How exactly do I handle the "reference any character" (the period) and "reference any digit" (d) items? Is it just . or \. and is it just d or is it \d? The use of a literal period also seems to be inconsistent. I know the regular expressions work with an ecmascript tester, but then the xkcd engine seems to use a different syntax that does not work. Also, does match[1] match the first () or does it match the whole thing?

Here is an example:
Code: Select all
if (comic.identifier < 1) {
            re = new RegExp("z(\d+).((png|gif))");
            match = re.exec(data);
            if (match != null) {
                comic.identifier = match[1];
            }
 }


I am trying to match a file of the form:

z#.png or z#.gif, where # is a number with 1-4 digits (if the number is less than 100 it is zero-padded to 3 digits). What I actually want is the number itself, so I am trying to get this by matching the first parenthesis. Is this going to do what I expect? Is there any way to test to see where the problems in my code are occuring?

Last edited by TheBlackCat on Sun Jan 18, 2009 9:37 pm, edited 1 time in total.


Man is the lowest-cost, 150-pound, nonlinear, all-purpose computer system which can be mass-produced by unskilled labor.
-NASA in 1965
pinotree
KDE Developer
Posts
222
Karma
7
OS

[SOLVED] Help with comic engine

Tue Jan 20, 2009 1:27 am
TheBlackCat wrote:
Code: Select all
re = new RegExp("z(\d+).((png|gif))");


I am trying to match a file of the form:

z#.png or z#.gif, where # is a number with 1-4 digits (if the number is less than 100 it is zero-padded to 3 digits).


There you go:
Code: Select all
re = new RegExp("z(\d{1,4})\.(png|gif)");


This means:
- 'z' character
- a match of a digit repeated a number of times between 1 and 4 (both included)
- a literal dot
- one of 'png' or 'gif'
The usage of (ie quoting) is needed when writing strings in languages like C/C++/JavaScript, as you want to write literally a '' in the string.
Also, you need to quote the dot in the regexp, otherwise it would mean "any character" (as you wrote).
About the final matching... I don't remember regexps much in JS, but usually 0 means "the whole match", 1 the first group and so on. Keep also in mind alternatives (ie "(png|gif)") are groups too, so you can know which alternative was actually matched.

Last tip: you can use KRegExpEditor (KDE 3 application available in kdeutils) to test you regexps.


Pino Toscano
mat69
KDE Developer
Posts
45
Karma
0

[SOLVED] Help with comic engine

Sat Jan 24, 2009 10:49 pm
I am not exactly commenting on that RegExp part, as pino already did that and as the following website would explain that probably a lot better (even with examples) than I would: http://www.w3schools.com/jsref/jsref_obj_regexp.asp and http://www.w3schools.com/JS/js_obj_regexp.asp (introduction)

In general you should have a similar structure for all the files like the other plugins on kde-files.org
If you have created a plugin and want to test it simply type
Code: Select all
plasmapkg -t comic -i myComic.comic
kbuildsyoca4

in a terminal and then
Code: Select all
plasmoidviewer comic

Now choose your comic and look if it works or if you get any error messages in the console output.

If you don't get any output and it does not work, then open the main file from
Code: Select all
~/.kde4/share/apps/plasma/comics/myComic/contents/code/main.es

and add something like:
Code: Select all
print("****page");//in the comic.Page section, so that you know that comic.Page is actually entered
print("****match: " + match);//will print all matches --> first the whole matched string, then everything between parenthesis
if (match != null) {
....
print("****imgUrl: " + url); // there has to be a url object in fact, this is just an example
...
}
...
//as last resort you could also "print(data)" to see if the data you got is what you expected

This way you can judge what does work and what not, I'm always using something like "****" in the prints as it is easier to spot and search for in the output, you could use whatever you like.


Then if everything works remove all the lines with print and save the main file where you originally stored it, create the package and upload it. You also need to "deinstall" (remove) your manually installed comic.
Code: Select all
plasmapkg -t comic -r myComic

After all that you will find it in the get hot new stuff dialog, though sometimes that can take a while.

Last edited by mat69 on Sat Jan 24, 2009 10:57 pm, edited 1 time in total.


mat69, proud to be a member of KDE forums since 2008-Oct.
User avatar TheBlackCat
Registered Member
Posts
2945
Karma
8
OS

[SOLVED] Help with comic engine

Sun Jan 25, 2009 1:54 am
#@%$. After so many hours trying to find the problem I was just missing a period in two of my "comic.identifier" calls. Thanks for the help, it seems to be working now.

Last edited by TheBlackCat on Sun Jan 25, 2009 2:00 am, edited 1 time in total.


Man is the lowest-cost, 150-pound, nonlinear, all-purpose computer system which can be mass-produced by unskilled labor.
-NASA in 1965
User avatar TheBlackCat
Registered Member
Posts
2945
Karma
8
OS

[SOLVED] Help with comic engine

Sun Jan 25, 2009 3:22 am
I seem to be having another weird problem now. It seems "function pageRetrieved(id, data)" is only running for the first comic. If I browse back and forth to other comics this function does not seem to be called. If I browse back to the first comic manually it also does not seem to be called. But if I do "Go to current comic" that function is called again. I can't figure out why.


Man is the lowest-cost, 150-pound, nonlinear, all-purpose computer system which can be mass-produced by unskilled labor.
-NASA in 1965
mat69
KDE Developer
Posts
45
Karma
0

[SOLVED] Help with comic engine

Sun Jan 25, 2009 2:45 pm
TheBlackCat wrote:#@%$. After so many hours trying to find the problem I was just missing a period in two of my "comic.identifier" calls. Thanks for the help, it seems to be working now.

Yeah I know that feeling, happened to me very often as well, but now with all the prints I find problems relatively fast. Sometimes when I was "blinded" to find the problem I ended with a lot of print statements like print("****a"); ranging to print("*****g") etc. :D

TheBlackCat wrote:I seem to be having another weird problem now. It seems "function pageRetrieved(id, data)" is only running for the first comic. If I browse back and forth to other comics this function does not seem to be called. If I browse back to the first comic manually it also does not seem to be called. But if I do "Go to current comic" that function is called again. I can't figure out why.

That is because that these comics have been cached allready, if you visit a comic you have not visited before than pageRetrieved will be called.

In general it works that way for the comic dataengine:
  1. check if the comic has been cached before (based on the identifier) --> no 2. yes 3.
  2. load the comic plugin (your plugin in this case) that should get the data --> 4.
  3. load the cached comic and the data (your plugin is not involved)
  4. offer the data to whoever asked for it

A comic is cached if there is a nextIdentifier.

In some cases though you do not need to specifiy previousIdentifier and nextIdentifier yourself if the comics are identified by ...
  1. numbers and none is left out (1,2,3,.... --> end-to-end) there you only need to find the lastIdentifier and firstIdentifier (if not set it will become 1) and then the dataengine will care for the rest:
    • If the identifier is 0 it will choose the lastIdentifier (yeah, not ideal if the firstIdentifier is 0, then you have to work around that by setting the firstIdentifier to 1 and decreasing it in your plugin e.g. H.G. Wells War of The Worlds),
    • if identifer identifier = firstIdentifier
    • identifier > lastIdentifier --> identifier = lastIdentifier
    • if identifier - 1 >= firstIdentifier --> previousIdentifier = identifier - 1
    • if identifier + 1 identifier = nextIdentifier + 1;
  2. date and no day is left out (like Garfield --> a comic every day since the start --> end-to-end), here you only need to the firstIdentifier (if not set it is open ended) and the lastIdentifier (if not set it will become today) and the dataengine will care for the rest:
    • identifier identifier = firstIdentifier
    • identifier > lastIdentifier --> identifier = lastIdentifier
    • if identifier - 1 day >= firstIdentifier --> previousIdentifier = identifier - 1 day
    • if identifier + 1 day nextIdentifier = identifier + 1 day
The console output only shows what you defined, so if you did not define the next and previous identifiers because of the cases outlined above there still will be next/previous identifiers based on the rules above.

To test your plugin you should go to
Code: Select all
cd ~/.kde4/share/apps/plasma_engine_/comic
rm myComic*

and no comics are cached anymore you can not turn caching off though, so as soon as you look at a comic that has a nextIdentifier then it will be cached.


I'm sorry that there is no documentation, have to do that. I also hope that everything I posted out of my head is correct. :)

Last edited by mat69 on Sun Jan 25, 2009 2:58 pm, edited 1 time in total.


mat69, proud to be a member of KDE forums since 2008-Oct.
User avatar TheBlackCat
Registered Member
Posts
2945
Karma
8
OS

[SOLVED] Help with comic engine

Sun Jan 25, 2009 5:03 pm
I see, caching must be doing it. Thanks!

What is the initial comic.identifier if you do not specify a lastidentifier up front?

Also, how will the engine handle updates to the comic while the comic engine is still open? Does it re-do the "init" part periodically?

Last edited by TheBlackCat on Sun Jan 25, 2009 5:06 pm, edited 1 time in total.


Man is the lowest-cost, 150-pound, nonlinear, all-purpose computer system which can be mass-produced by unskilled labor.
-NASA in 1965
User avatar TheBlackCat
Registered Member
Posts
2945
Karma
8
OS

[SOLVED] Help with comic engine

Sun Jan 25, 2009 5:40 pm
I figured out my first question, but I still am not sure about the second. If the comic is updated but the person does not logout, how will the comic engine now?

Last edited by TheBlackCat on Sun Jan 25, 2009 5:40 pm, edited 1 time in total.


Man is the lowest-cost, 150-pound, nonlinear, all-purpose computer system which can be mass-produced by unskilled labor.
-NASA in 1965
mat69
KDE Developer
Posts
45
Karma
0

[SOLVED] Help with comic engine

Sun Jan 25, 2009 7:38 pm
TheBlackCat wrote:I figured out my first question, but I still am not sure about the second. If the comic is updated but the person does not logout, how will the comic engine now?


Currently it won't know that.

It only provides the data it was asked for and if nothing exactly was specified (like "garfield:" --> your first question) it will ask for a comic from 1. today 2. # 0 3. empty string
Though I have to admit I'm not really satisfied with that way. Imho it would be better to seperate that like having "bool wantsMostRecent" or whatever, but a change like that should also work with any plugins and versions of the dataengine. Otherwise it would be a pita to support plugins for 4.2 and 4.3+.


But back to your original question: The comic applet itself tells the data engine when to update it.

In the applet there is a timer that checks if the day changed and if that happens the comic is reloaded.

Probably I'll also add an option like "check each X minutes for new comics", better worded in fact.

Last edited by mat69 on Sun Jan 25, 2009 7:40 pm, edited 1 time in total.


mat69, proud to be a member of KDE forums since 2008-Oct.
User avatar TheBlackCat
Registered Member
Posts
2945
Karma
8
OS

[SOLVED] Help with comic engine

Sun Jan 25, 2009 8:04 pm
Good, daily checks will work nicely. Your "check every X amount of time" idea is good, it might also be good to add an option to check at a certain time on certain days of the week, for a comic like XKCD that updates with a fixed schedule. Being able to specify the default checking behavior within the comic engine might also be worthwhile, since the one writing the engine should know if there is a regular update schedule.

Will there be, or is there already, support for comics that are split up across multiple image files? http://www.captainsnes.com/ and http://www.schlockmercenary.com/ both do this.


Man is the lowest-cost, 150-pound, nonlinear, all-purpose computer system which can be mass-produced by unskilled labor.
-NASA in 1965
mat69
KDE Developer
Posts
45
Karma
0

[SOLVED] Help with comic engine

Sun Jan 25, 2009 8:25 pm
TheBlackCat wrote:[...]
Being able to specify the default checking behavior within the comic engine might also be worthwhile, since the one writing the engine should know if there is a regular update schedule.

I'm not sure if that is really needed. And if that was added problems with timezones could occur if not everybody would use UTC.
I think reloading the current strip all X minutes would do the job as well, especially as that only needs download if the strip is not cached already. In fact reloading would be only done if there is a connection or if the status of the connection is unknown.

Will there be, or is there already, support for comics that are split up across multiple image files? http://www.captainsnes.com/ and http://www.schlockmercenary.com/ both do this.

Yes there is. Currently Deo Ignito uses that, also ****! to some degree. Best is you look at the way I did it in the first as I could not say that out of my head. Though ignore all the "compatility" settings, those are only needed in this specific case as here comics are mixed with news on the website sometimes --> have to make that a more generic way.

Ok, looking at it:
20-21: global variables created
54-64: an array of pictures is created
86: first image is downloaded and removed of the array
115-123: the rest of the images is downloaded and combined


mat69, proud to be a member of KDE forums since 2008-Oct.
mat69
KDE Developer
Posts
45
Karma
0

[SOLVED] Help with comic engine

Sun Feb 01, 2009 10:05 pm
OK, I created a tutorial on how to create comic plugins. You can find a link to it here: http://forum.kde.org/creating-comic-plu ... 29950.html

Last edited by mat69 on Sun Feb 01, 2009 10:54 pm, edited 1 time in total.


mat69, proud to be a member of KDE forums since 2008-Oct.
User avatar TheBlackCat
Registered Member
Posts
2945
Karma
8
OS

[SOLVED] Help with comic engine

Mon Feb 02, 2009 5:27 pm
Any additional questions should probably go in the tutorial thread instead of here, so I will close this thread.


Man is the lowest-cost, 150-pound, nonlinear, all-purpose computer system which can be mass-produced by unskilled labor.
-NASA in 1965

 
Reply to topic

Bookmarks



Who is online

Registered users: Baidu [Spider], Bing [Bot], boblundgren, boospy007, claydoh, fayl, Google [Bot], Hokasito, kde-camillem, Lupccs, rblackwell, Yahoo [Bot]