Package com.aelitis.azureus.core.metasearch

Examples of com.aelitis.azureus.core.metasearch.SearchException


        }
        while(st.hasMoreTokens()) {
          try {
            jsonObject = ((JSONObject)jsonObject).get(st.nextToken());
          } catch(Throwable t) {
            throw new SearchException("Invalid entry path : " + resultsEntryPath,t);
          }
        }
      }
     
      try{
        resultArray = (JSONArray) jsonObject;
       
      }catch(Throwable t){
       
        throw new SearchException("Object is not a result array. Check the JSON service and/or the entry path");
      }
       
       
      if ( resultArray != null ){
       
        List results = new ArrayList();
       
        for(int i = 0 ; i < resultArray.size() ; i++) {
         
          Object obj = resultArray.get(i);
         
          if(obj instanceof JSONObject) {
            JSONObject jsonEntry = (JSONObject) obj;
           
            if ( absolute_max_matches >= 0 ){
              if ( --absolute_max_matches < 0 ){
                break;
              }
            }
           
            if ( listener != null ){
             
                // sort for consistent order
             
              Iterator it = new TreeMap( jsonEntry ).entrySet().iterator();
             
              String[]  groups = new String[ jsonEntry.size()];
             
              int  pos = 0;
             
              while( it.hasNext()){
               
                Map.Entry entry = (Map.Entry)it.next();
               
                Object key     = entry.getKey();
                Object value   = entry.getValue();
               
                if ( key != null && value != null ){
               
                  groups[pos++] = key.toString() + "=" + UrlUtils.encode( value.toString());
                 
                }else{
                 
                  groups[pos++] = "";
                }
              }
             
              listener.matchFound( this, groups );
            }
           
            WebResult result = new WebResult(this,getRootPage(),getBasePage(),getDateParser(),searchQuery);
                         
            for(int j = 0 ; j < mappings.length ; j++) {
              String fieldFrom = mappings[j].getName();
              if(fieldFrom != null) {
                int fieldTo = mappings[j].getField();
                Object fieldContentObj = ((Object)jsonEntry.get(fieldFrom));
                if(fieldContentObj != null) {
                  String fieldContent = fieldContentObj.toString();
                 
                  switch(fieldTo) {
                  case FIELD_NAME :
                    result.setNameFromHTML(fieldContent);
                    break;
                  case FIELD_SIZE :
                    result.setSizeFromHTML(fieldContent);
                    break;
                  case FIELD_PEERS :
                    result.setNbPeersFromHTML(fieldContent);
                    break;
                  case FIELD_SEEDS :
                    result.setNbSeedsFromHTML(fieldContent);
                    break;
                  case FIELD_CATEGORY :
                    result.setCategoryFromHTML(fieldContent);
                    break;
                  case FIELD_DATE :
                    result.setPublishedDateFromHTML(fieldContent);
                    break;
                  case FIELD_COMMENTS :
                    result.setCommentsFromHTML(fieldContent);
                    break;
                  case FIELD_CDPLINK :
                    result.setCDPLink(fieldContent);
                    break;
                  case FIELD_TORRENTLINK :
                    result.setTorrentLink(fieldContent);
                    break;
                  case FIELD_PLAYLINK :
                    result.setPlayLink(fieldContent);
                    break;
                  case FIELD_DOWNLOADBTNLINK :
                    result.setDownloadButtonLink(fieldContent);
                    break;
                  case FIELD_VOTES :
                    result.setVotesFromHTML(fieldContent);
                    break;
                  case FIELD_SUPERSEEDS :
                    result.setNbSuperSeedsFromHTML(fieldContent);
                    break;
                  case FIELD_PRIVATE :
                    result.setPrivateFromHTML(fieldContent);
                    break;
                  case FIELD_DRMKEY :
                    result.setDrmKey(fieldContent);
                    break;
                  case FIELD_VOTES_DOWN :
                    result.setVotesDownFromHTML(fieldContent);
                    break;
                  case FIELD_HASH :
                    result.setHash(fieldContent);
                    break;
                  default:
                    break;
                  }
                }
              }
            }
                         
            results.add(result);
          }
        }
       
        Result[] res = (Result[]) results.toArray(new Result[results.size()]);

        debugLog( "success: found " + res.length + " results" );
       
        return( res );
       
      }else{
     
        debugLog( "success: no result array found so no results" );
       
        return( new Result[0]);
      }
     
    }catch( Throwable e ){
     
      debugLog( "failed: " + Debug.getNestedExceptionMessageAndStack( e ));
     
      if ( e instanceof SearchException ){
       
        throw((SearchException)e );
      }
     
      throw( new SearchException( "JSON matching failed", e ));
    }
  }
View Full Code Here


       
        String  type = post_params.substring( 0, sep );
       
        if ( !type.equals( "post_basic" )){
         
          throw( new SearchException( "Only basic type supported" ));
        }
       
        post_params = post_params.substring( sep+1 );
       
          // already URL encoded
       
        initial_url_rd = rdf.create( initial_url, post_params );

        initial_url_rd.setProperty( "URL_Content-Type", "application/x-www-form-urlencoded" );
       
      }else{
     
        debugLog( "search_url: " + searchURL );
     
        initial_url = new URL(searchURL);
     
        initial_url_rd = rdf.create( initial_url );
      }
     
      setHeaders( initial_url_rd, headers );
       
      if ( needsAuth && local_cookies != null ){
       
        initial_url_rd.setProperty( "URL_Cookie", local_cookies );       
      }
       
      if ( only_if_modified ){
       
        String last_modified   = getLocalString( LD_LAST_MODIFIED );
        String etag        = getLocalString( LD_ETAG );

        if ( last_modified != null ){
         
          initial_url_rd.setProperty( "URL_If-Modified-Since", last_modified );
        }
       
        if ( etag != null ){
         
          initial_url_rd.setProperty( "URL_If-None-Match", etag );
        }
      }
     
      InputStream  is;
     
      String content_charset = "UTF-8";

      ResourceDownloader mr_rd = null;
     
      if ( initial_url.getProtocol().equalsIgnoreCase( "file" )){
       
          // handle file://c:/ - map to file:/c:/
       
        String  str = initial_url.toExternalForm();
       
        if ( initial_url.getAuthority() != null ){
       
          str = str.replaceFirst( "://", ":/" );
        }
               
        int  pos = str.indexOf( '?' );
       
        if ( pos != -1 ){
         
          str = str.substring( 0, pos );
        }
       
        is = new FileInputStream( new File( new URL( str ).toURI()));
       
      }else{
       
        mr_rd = rdf.getMetaRefreshDownloader( initial_url_rd );

        try{
       
          is = mr_rd.download();
         
        }catch( ResourceDownloaderException e ){
       
          Long  response = (Long)mr_rd.getProperty( "URL_HTTP_Response" );
         
          if ( response != null && response.longValue() == 304 ){
           
              // not modified
           
            return( new pageDetails( initial_url, initial_url, "" ));
           
          }else{
           
            throw( e );
          }
        }

     
        if ( needsAuth ){
         
          List  cookies_list = (List)mr_rd.getProperty( "URL_Set-Cookie" );
         
          List  cookies_set = new ArrayList();
         
          if ( cookies_list != null ){
           
            for (int i=0;i<cookies_list.size();i++){
             
              String[] cookies = ((String)cookies_list.get(i)).split(";");
             
              for (int j=0;j<cookies.length;j++){
               
                String  cookie = cookies[j].trim();
               
                if ( cookie.indexOf('=') != -1 ){
                 
                  cookies_set.add( cookie );
                }
              }
            }
          }
         
            // well, not much we can do with the cookies anyway as in general the ones
            // set are the ones missing/expired, not the existing ones. That is, we can't
            // deduce anything from the fact that a required cookie is not 'set' here
            // the most we could do is catch a server that explicitly deleted invalid
            // cookies by expiring it, but I doubt this is a common practice.
         
            // Also note the complexity of cookie syntax
            // Set-Cookie: old standard using expires=, new using MaxAge
            // Set-Cookie2:
            // Maybe use http://jcookie.sourceforge.net/ if needed
        }
       
        if ( only_if_modified ){
         
          String last_modified   = extractProperty( mr_rd.getProperty( "URL_Last-Modified" ));
          String etag        = extractProperty( mr_rd.getProperty( "URL_ETag" ));
         
          if ( last_modified != null ){
           
            setLocalString( LD_LAST_MODIFIED, last_modified );
          }
         
          if ( etag != null ){
           
            setLocalString( LD_ETAG, etag );
          }
        }
       
        List cts = (List)mr_rd.getProperty( "URL_Content-Type" );
                     
        if ( cts != null && cts.size() > 0 ){
         
          String  content_type = (String)cts.get(0);
         
          int  pos = content_type.toLowerCase().indexOf( "charset" );
         
          if ( pos != -1 ){
           
            content_type = content_type.substring( pos+1 );
           
            pos = content_type.indexOf('=');
           
            if ( pos != -1 ){
             
              content_type = content_type.substring( pos+1 ).trim();
             
              pos = content_type.indexOf(';');
             
              if ( pos != -1 ){
               
                content_type = content_type.substring(0,pos).trim();
              }
             
              try{
                if ( Charset.isSupported( content_type )){
                 
                  debugLog( "charset: " + content_type );
                 
                  content_charset = content_type;
                }
              }catch( Throwable e ){
               
                try{
                    // handle lowercase 'utf-8' for example
                 
                  content_type = content_type.toUpperCase();
                 
                  if ( Charset.isSupported( content_type )){
                   
                    debugLog( "charset: " + content_type );
                   
                    content_charset = content_type;
                  }
                }catch( Throwable f ){
                 
                  log( "Content type '" + content_type + "' not supported", f );
                }
              }
            }
          }
        }
      }
     
      ByteArrayOutputStream baos = new ByteArrayOutputStream(8192);
                 
      byte[] buffer = new byte[8192];     

      while( true ){
       
        int  len = is.read( buffer );
       
        if ( len <= 0 ){
         
          break;
        }
       
        baos.write( buffer, 0, len );
      }
     
      byte[] data = baos.toByteArray();
     
      if ( vuze_file ){
       
        try{
          VuzeFileHandler vfh = VuzeFileHandler.getSingleton();
         
          VuzeFile vf = vfh.loadVuzeFile( data );
         
          vfh.handleFiles( new VuzeFile[]{ vf }, VuzeFileComponent.COMP_TYPE_NONE );
         
        }catch( Throwable e ){
         
          Debug.out( e );
        }
       
        return( new pageDetails( initial_url, initial_url, null ));
      }
     
      String   page = null;
     
      String  content = new String( data, 0, Math.min( data.length, 2048 ), content_charset );
     
      String  lc_content = content.toLowerCase();
     
      {
          // first look for xml charset
     
          // e.g. <?xml version="1.0" encoding="windows-1251" ?>

        int  pos1 = lc_content.indexOf( "<?xml" );
       
        if ( pos1 != -1 ){
         
          int pos2 = lc_content.indexOf( "?>" );
         
          if ( pos2 != -1 ){
                     
            int pos3 = lc_content.indexOf( "encoding", pos1 );
           
            if ( pos3 != -1 ){
             
              pos3 = lc_content.indexOf( "\"", pos3 );
            }
           
            if ( pos3 > pos1 && pos3 < pos2 ){
                           
              pos3++;
             
              int pos4 = lc_content.indexOf( "\"", pos3 );
             
              if ( pos4 > pos3 && pos4 < pos2 ){
               
                String  encoding = content.substring( pos3, pos4 ).trim();
               
                try{
                  if ( Charset.isSupported( encoding )){
                   
                    debugLog( "charset from xml tag: " + encoding );
                   
                    content_charset = encoding;
                   
                      // some feeds have crap at the start which makes pos2 mismatch for the above '?' - adjust if necessary
                   
                    int data_start = pos2;
                   
                    int  max_skip  = 64;
                   
                    while( data[data_start] != '?' && max_skip-- > 0 ){
                     
                      data_start++;
                    }
                   
                    page = content.substring( 0, pos3 ) + "utf-8" + content.substring( pos4, pos2 ) + new String( data, data_start, data.length - data_start, content_charset );             
                  }
                }catch( Throwable e ){
                 
                  log( "Content type '" + encoding + "' not supported", e );
                }
              }
            }
          }
        }
      }
     
      if ( page == null ){
         
          // next look for http-equiv charset
       
          // e.g. <meta http-equiv="Content-Type" content="text/html; charset=windows-1251" />

        int  pos = 0;
       
        while( true ){
       
          int  pos1 = lc_content.indexOf( "http-equiv", pos );
       
          if ( pos1 != -1 ){
         
            int  pos2 = lc_content.indexOf( ">", pos1 );
           
            if ( pos2 != -1 ){
             
              int  pos3 = lc_content.indexOf( "charset", pos1 );
             
              if ( pos3 != -1 && pos3 < pos2 ){
             
                pos3 = lc_content.indexOf( "=", pos3 );
               
                if ( pos3 != -1 ){
                 
                  pos3++;
                 
                  int pos4 = lc_content.indexOf( "\"", pos3 );
                 
                  if ( pos4 != -1 ){
                   
                    int pos5 = lc_content.indexOf( ";", pos3 );
                 
                    if ( pos5 != -1 && pos5 < pos4 ){
                     
                      pos4 = pos5;
                    }
                   
                    String encoding = content.substring( pos3, pos4 ).trim();
                   
                    try{
                      if ( Charset.isSupported( encoding )){
                       
                        debugLog( "charset from http-equiv : " + encoding );
                       
                        content_charset = encoding;
                       
                          // some feeds have crap at the start which makes pos2 mismatch for the above '?' - adjust if necessary
                       
                        int data_start = pos2;
                       
                        int  max_skip  = 64;
                       
                        while( data[data_start] != '?' && max_skip-- > 0 ){
                         
                          data_start++;
                        }
                       
                        page = content.substring( 0, pos3 ) + "utf-8" + content.substring( pos4, pos2 ) + new String( data, data_start, data.length - data_start, content_charset );             
                      }
                    }catch( Throwable e ){
                     
                      log( "Content type '" + encoding + "' not supported", e );
                    }
                   
                    break;
                  }
                }
              }
             
              pos = pos2;
             
            }else{
             
              break;
            }
          }else{
           
            break;
          }
        }
      }
     
      if ( page == null ){
       
        page = new String( data, content_charset );
      }
     
      debugLog( "page:" );
      debugLog( page );

      // List   cookie = (List)url_rd.getProperty( "URL_Set-Cookie" );
     
      try {
        Matcher m = baseTagPattern.matcher(page);
        if(m.find()) {
          basePage = m.group(1);
         
          debugLog( "base_page: " + basePage );
        }
      } catch(Exception e) {
        //No BASE tag in the page
      }
     
      URL  final_url = initial_url;
     
      if ( mr_rd != null ){
       
        URL  x = (URL)mr_rd.getProperty( "URL_URL" );
 
        if ( x != null ){
         
          final_url = x;
        }
      }
     
      return( new pageDetails( initial_url, final_url, page ));
       
    }catch( SearchException e ){
     
      throw( e );
     
    }catch( Throwable e) {
     
      // e.printStackTrace();
     
      debugLog( "Failed to load page: " + Debug.getNestedExceptionMessageAndStack(e));
     
      throw( new SearchException( "Failed to load page", e ));
     
    }finally{
     
      TorrentUtils.setTLSDescription( null );
    }
View Full Code Here

      if ( e instanceof SearchException ){
       
        throw((SearchException)e );
      }
     
      throw( new SearchException( "RSS matching failed", e ));
    }
  }
View Full Code Here

              if ( e instanceof SearchException ){
               
                throw((SearchException)e );
              }
             
              throw new SearchException(e);
            }
          }
        });
     
      Result[] res = (Result[])task.run();
     
      debugLog( "success: found " + res.length + " results" );
     
      return( res );
     
    }catch( Throwable e ){
     
      debugLog( "failed: " + Debug.getNestedExceptionMessageAndStack( e ));
     
      if ( e instanceof SearchException ){
       
        throw((SearchException)e );
      }
     
      throw( new SearchException( "Regex matching failed", e ));
    }
  }
View Full Code Here

        if ( listener != null ){
       
          listener.engineFailed( this, e);
        }
       
        throw( new SearchException( "Search failed", e ));
      }
    }
  }
View Full Code Here

TOP

Related Classes of com.aelitis.azureus.core.metasearch.SearchException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.