Wednesday, 27 November 2013

Adb shell - opening/copying files by selecting as wildcard

Let's say we have very long app and we do not want rewrite it because it takes too much time or we sense that the percentage of wrong misspelling will be occured.. here is the solution

We know that:
ls - list files
| - is a pipe for i.e. grep
grep <text> - filters text and returns text which is accepted with our filter

Problem: we need to open directory which is too long, we need use example wildcard:
cd $(ls|grep App_Back) - we need just few letters of directory which we want to open

Example:
shell@mako:/storage/emulated/legacy $ ls
ls
Alarms
Android
App_Backup_Restore
DCIM
Download
EditedOnlinePhotos
FREEdiYouTubePlayer
Movies
Music
NoobRoomLatestAdditionsCache
Notifications
Pictures
Podcasts
Ringtones
aquery
shell@mako:/storage/emulated/legacy $ cd $(ls|grep App_Backup)
cd $(ls|grep App_Backup)
shell@mako:/storage/emulated/legacy/App_Backup_Restore $

Tuesday, 26 November 2013

Working example - check if word exist - using wiki url for checking

Here is working example how check if our word real exist.
We will use for it wikipedia url. If we put to url our wolrd and if page opens it, it will means word exist.
However If we do it by in example firefox we will see text "we do not have much information about this, do you want add some for it?" or something like that.

If we use it by writting in code, response would be quite different. We would get responseCode which is very helpful.

/**
 * Created with IntelliJ IDEA.
 * User: Michall
 * Date: 23.11.13
 * Time: 17:58
 * To change this template use File | Settings | File Templates.
 */
public class WikiBig {

    private int cc = 0;

    public static void main(String[] args) {
        new WikiBig().doStuff();
    }

    /**
     * We set path to words to check if any from the list exist
     */
    private void doStuff() {
        CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));

        //word per line like (in polish language) example:
        //dom
        //kot
        //drzew
        //13921hx9x1 <- this will not exist so wiki will return us 400 and text will not be appendted to list of real words
        File file = new File("C:\\temp\\words.txt");
        loadTextFromFIle(file.getAbsolutePath());

    }

    /**
     * Unused but it converts con.getInputstream to string
     * @param is
     * @return
     */
    static String convertStreamToString(java.io.InputStream is) {
        java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    }

    /**
     * for every word we take from file we add it to url
     * if wiki returns us 200 it means word exists
     * if not 400
     *
     * If we find existing word we will append it to new file with words existing
     * @param path
     */
    private void loadTextFromFIle(String path) {
        try {

            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(
                    path), "cp1250"));

            String strLine;
            // Read File Line By Line
            while (((strLine = br.readLine()) != null)) {
                askWordWiki(strLine);
            }
            br.close();


        } catch (FileNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    /**
     * save text to list of existing words (to file of course)
     * @param strLine
     */
    private void askWordWiki(final String strLine) {
        if (cc > 12229)
        {
            try {

                String wordWithUrl = "http://pl.wiktionary.org/wiki/" + strLine;
                System.out.print(".");
                URL obj = null;

                obj = new URL(wordWithUrl);

                HttpURLConnection con = (HttpURLConnection) obj.openConnection();
                con.setRequestMethod("GET");
                int responseCode = con.getResponseCode();
                if (responseCode == 200) {
                    System.out.println(strLine);
                    writeAppendToFile(strLine);
                }
            } catch (MalformedURLException e) {
                askWordWiki(strLine);//prevent from timeout
            } catch (ProtocolException e) {
                askWordWiki(strLine);//prevent from timeout
            } catch (IOException e) {
                askWordWiki(strLine);//prevent from timeout
            }
        }
    }

    private void writeAppendToFile(String strLine) throws IOException {

        BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\temp\\foundwords.txt", true));
        bw.append(strLine);
        bw.newLine();
        bw.close();
    }
}

Working example with jsoup - parse from site actors and top of best movies

Here is an example how to use jsoup to parse some text from site.
This site has also small "security bridge". To get what we want we must have first cookie.

So first we will take cooke from home page, and then we will parse some text.

I used in string something like this %1$s < percentage with number tells about number of argument, dolar with s means that this will be a string (d if for decimal and etc.)
So after this move i can reuse string and putting into additional string element

/**
 * Created with IntelliJ IDEA.
 * Date: 10.11.13
 * Time: 21:44
 * To change this template use File | Settings | File Templates.
 */
public class Seriale {
    private static boolean writeOut = true;
    private static String start = "http://www.filmweb.pl/";
    private static String series = "http://www.filmweb.pl/rankings/series/poland";
    private static String actors = "http://www.filmweb.pl/search/person?q=&sex=0&startBirthYear=&endBirthYear=&professionIds=null&startRate=&endRate=&startCount=&endCount=&sort=COUNT&sortAscending=false&c=portal&page=%1$s";
    private static URL obj;
    private static HttpURLConnection con;
    private static int responseCode;

    public static void main(String[] args) throws IOException {

        CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));

        //we need first cookie, if we won't get it we will see annoying advertistment
        initStart();

        getSeries();
        getActors();

    }

    private static void initStart() throws IOException {
        obj = new URL(start);
        con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");
        responseCode = con.getResponseCode();
    }

    private static void getSeries() throws IOException {
        obj = new URL(series);
        con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");
        responseCode = con.getResponseCode();

        String str = convertStreamToString(con.getInputStream());
        Document doc = Jsoup.parse(str);
        Element awt = doc.select("table.awT.rankingTable").get(0);

        Elements filmPoster = awt.select("div.ohidden");
        for (Element el : filmPoster) {
            System.out.println(el.select("a[href]").get(0).text());
        }
    }

    private static void getActors() throws IOException {
        for (int i = 0; i < 5; i++) {
            obj = new URL(String.format(actors, i));
            con = (HttpURLConnection) obj.openConnection();
            con.setRequestMethod("GET");
            responseCode = con.getResponseCode();

            String str = convertStreamToString(con.getInputStream());
            Document doc = Jsoup.parse(str);
            Elements awt = doc.select("div.hitDescWrapper>h3");

            for (Element el : awt) {
                System.out.println(el.text());
            }
        }
    }

    static String convertStreamToString(java.io.InputStream is) {
        java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    }
}

Working example for grasemonkey - show me only elements where post has >= 1k shares

Here is short example.
Few weeks ago I started learning javascript. This language is great it is very useful for websites where you want make little autobot or any part-site remover.
Here is an working example on real site, it is very easy.

// ==UserScript==
// @name        kwejk low rate remover
// @namespace  
// @include     http://kwejk.pl/*
// @version     1
// @grant       none
// ==/UserScript==

//window.onload = function(){
    //alert("loaded...");
    var elements = document.getElementsByClassName("mediaPair");
    for(var i=0;i<elements.length;i++)
    {
        //alert(i);
        var fbc = elements[i].getElementsByClassName("fb_share_ab")[0]
        .getElementsByClassName("facebook_share_size_Small")[0]
        .getElementsByClassName("facebook_share_count_inner")[0].innerHTML;
       
        //alert("["+fbc+"]" + " " +fbc.length);

            //alert(i + "/" + elements.length + " votes " + fbc + " isNumber? " + isNumber(fbc) + " " + (Number(fbc) < 1000));
            if(isNumber(fbc))
            {
                if(Number(fbc) < 1000)
                {
                    elements[i].outerHTML = "";
                    i--;
                }
            }
            //alert("stop");
    }
//}

function isNumber (o) {
  return ! isNaN (o-0) && o !== null && o !== "" && o !== false;
}

Sunday, 17 November 2013

Noobroom another changes

Hi,
I've changed some things in apk. I added list menu with categories to be able check list ordered by last date, by IMDB rating or release date.
There are some things to fix like: make data to load faster or handle wrong parsing.
Part of them will be fixed today before 8 PM GMT+01:00)
I'm glad people like my app and it is still useful. :)

I've found on Google services quite interesting trend diagram for noobroom. Here is the diagram showing from where (only USA) people are search Noobroom.

Wednesday, 13 November 2013

Sunday, 3 November 2013

Noobroom Latest Additions fix

Today I've fixed my app which you can find here Google Play
Now it should be working.
Please feel free pm/mail me for any problems.
Sometimes I do not have so much time to take car of my app but I'll try fix all problems asap.

Nothing much was changed except problem with logging.
I've added new information about problems with app :)