📄 using_flickrj.java
字号:
import java.util.*;import java.io.*;import java.net.*;import org.w3c.dom.*;import javax.xml.parsers.*;import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import com.aetrion.flickr.*;import com.aetrion.flickr.contacts.*;import com.aetrion.flickr.people.*;public class using_flickrj{ public int depth = 0; private URL url = null; public String id = ""; public Flickr flickr = null; public User userGen; private ContactsInterface ci; private PeopleInterface people; // class with details for a contact static class PersonContact { public String userURL; // contact url public String relationURL; // base url, the url you start the search public int degree; // depth of the contacts' tree public String refURL; // contact parent's url } private ArrayList<PersonContact> listaContacte; // class constructor public using_flickrj(String apiKey, String sharedSecret) { try { this.flickr = new Flickr(apiKey, sharedSecret, new REST()); // main API class. you need an apiKey and a sharedSecret, you can obtain them from the address http://www.flickr.com/services/api/ this.listaContacte = new ArrayList<PersonContact>(); // the contacts' list ci = this.flickr.getContactsInterface(); // the interface needed to work with the contacts people = this.flickr.getPeopleInterface(); // the interface needed to work with people informations (you need it to gain the details for a given username) } catch(Exception e) { System.out.println(e.getMessage()); } } // read the input and set the remaining class variables public void ReadInput(String xmlFile) { try { // citesc xml-ul // Create a factory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// Use the factory to create a builder
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File(xmlFile)); Element elem = doc.getDocumentElement(); if(elem.getTagName().compareTo("config") == 0) { NodeList children = elem.getChildNodes(); for(int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if(child.getNodeName().compareTo("url") == 0) { url = new URL(child.getChildNodes().item(0).getNodeValue()); } if(child.getNodeName().compareTo("username") == 0) { id = child.getChildNodes().item(0).getNodeValue(); PeopleInterface people = flickr.getPeopleInterface(); userGen = people.findByUsername(id); } if(child.getNodeName().compareTo("depth") == 0) { depth = Integer.parseInt(child.getAttributes().getNamedItem("value").getNodeValue()); } } } } catch(Exception e) { System.out.println(e.getMessage()); } } // function for building the contacts list public void ProcessContact(String username, int degree, String parentId) { try { User userLoc = people.findByUsername(username); System.out.println("Username: " + username + ", id: " + userLoc.getId() + ", parent id: " + parentId); // build the current contact PersonContact person = new PersonContact(); person.userURL = "http://www.flickr.com/people/" + userLoc.getId() + "/"; person.relationURL = "http://www.flickr.com/people/" + userGen.getId() + "/"; person.degree = depth - degree; person.refURL = "http://www.flickr.com/people/" + parentId + "/"; listaContacte.add(person); // add the contact to the contacts list if(degree > 0) { Iterator<Contact> it = ci.getPublicList(userLoc.getId()).iterator(); for(int i = 0; i < ci.getPublicList(userLoc.getId()).size(); i++) { Contact contact = it.next(); // recursive function, call it for someone's contacts ProcessContact(contact.getUsername(), degree-1, userLoc.getId()); } } } catch(Exception e) { System.out.println(e.getMessage()); } } // output creation function, based on the function provided here: http://www.roseindia.net/xml/dom/createdomchildelement.shtml public void CreateOutput(String outXmlFile) { try { File f = new File(outXmlFile); //Create instance of DocumentBuilderFactory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //Get the DocumentBuilder DocumentBuilder docBuilder = factory.newDocumentBuilder(); //Create blank DOM Document Document doc = docBuilder.newDocument(); //create the root element Element root = doc.createElement("socialnetwork"); //add it to the xml tree doc.appendChild(root); for(int i = 0; i < listaContacte.size(); i++) { Element userElement = doc.createElement("user"); //Add the atribute to the child userElement.setAttribute("url", listaContacte.get(i).userURL); Element relationElement = doc.createElement("relation"); relationElement.setAttribute("url", listaContacte.get(i).relationURL); relationElement.setAttribute("degree", (new Integer(listaContacte.get(i).degree)).toString()); relationElement.setAttribute("ref", listaContacte.get(i).refURL); userElement.appendChild(relationElement); root.appendChild(userElement); } TransformerFactory tranFactory = TransformerFactory.newInstance(); Transformer aTransformer = tranFactory.newTransformer(); Source src = new DOMSource(doc); Result dest = new StreamResult(f); aTransformer.transform(src, dest); } catch(Exception e) { System.out.println(e.getMessage()); } } public static void main(String[] args) { try { using_flickrj uf = new using_flickrj("your_key", "your_secret"); // replace the 2 Strings with the key and secret obtained from http://www.flickr.com/services/api/ uf.ReadInput("config.xml"); uf.ProcessContact(uf.id, uf.depth, ""); uf.CreateOutput("socialnetwork.xml"); } catch(Exception e) { System.out.println(e.getMessage()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -