Houdini-Developing PM2.5 value reading plug-in with Python (1)

Source: Internet
Author: User

Recently in free time to develop a Houdini more annoying plug-in, able to read the current PM2.5 values, mainly in Beijing, this environment is too disturbing, dirty also have to find some fun in the bitter. Plug-ins are not ready, now just the data read that part of the finished, and so on for some time and the design of the small partners to see how to do the program animation more fun.

Here is a brief idea, then the code.

Before you start, list the sites I crawled data from:

Pm2.5:http://pm25.in

City List (You can add your own city manually afterwards): http://europe.chinadaily.com.cn/travel/2014-12/16/content_19095561.htm

1: Read the city list.

I think that if you are using this plugin for the first time, you will be able to read a list of trusted cities on the web and store it in txt form on your local computer at the moment of node creation, so that you can directly pick up the local list the next time, and you can add or modify the desired city at will. In fact, you manually to write a list is not bad, but this step is more intelligent a little bit.

2: Select the city and fetch the data

After selecting the city you want to find in the Panel, click Update to navigate to the city's page. For example, choose "Chongqing", then the script will first point to http://pm25.in/chongqing. In fact, this site has provided a reliable API, but this API application audit a bit of trouble, so in the spirit of being able to not trouble others do not bother others principle I will still not bother to audit personnel, not to mention my real-time update speed is not required at all. It is not difficult to read page information directly with Python, there are ready-made urllib to use, and then use regular expressions to search for the part of the text that you want. Here I crawl values such as PM2.5 and carbon monoxide, as well as AQI's 24-hour historical data.

In the script section, I was divided into two modules, the first part is the node generation will be executed on one side, mainly to update the city list. The second part is the corresponding button to read the specific data. The following is the dry code:

oncreated section:

Import ReImport Urllib2Import stringImport HouUrl= "Http://europe.chinadaily.com.cn/travel/2014-12/16/content_19095561.htm"Cityfileread= Open("C:\Users\Administrator\Documents\houdini13.0\pythonFiles\Cities.txt","R") citylist=Cityfileread.Read ()# Open URL and get the contentdef geturlcontent(URL, citylist):if Len(citylist)< Ten: Request=Urllib2.Request (URL) request.Add_header (' User-agent ',' Magic-client ') Response=Urllib2.Urlopen (Request).Read ()Print Len(response)returnResponseElse:return Nonedef Findkeywords(content): KeyWord=Re.Compiler "" "(? <=<p>here is the full list of this year ' s ranking:</p>). *? (? =<div width= "100%" >) "" ", re.Dotall) Results=KeyWord.FindAll (content) Subkeyword=Re.Compiler "(? <=.). *? (?=</p>) ") Cities=Subkeyword.FindAll (results[0]) Cityfilewrite= Open("C:\Users\Administrator\Documents\houdini13.0\pythonFiles\Cities.txt","W") forCityinchCities:city=City.Lower () City=City.Replace"'","") Cityfilewrite.Write (city+ ",") Cityfilewrite.Close () content=Geturlcontent (URL, citylist)ifContent!= None: findkeywords (content) Cityfileread.Close ()#print "Loaded Again"

Next is the Pythonmodule section:

Import Urllib2Import Re Self =Hou.PWD ()#geo = Self.geometryCityfileread= Open("C:\Users\Administrator\Documents\houdini13.0\pythonFiles\Cities.txt","R") citylist=Cityfileread.Read () citylist=CityList.Split",") pmvalues={}def Getcityname(): Index=  Self.Parm"CityName").Eval ()#print Index     forIinch Range(Len(citylist)):ifI==IndexreturnCitylist[i]#print Citylist[i]def findurlcontent(city): URL= "http://pm25.in/" +City#print City     Self.Setparms ({"City": City}) Request=Urllib2.Request (URL) request.Add_header (' User-agent ',' Firefox ') Response=Urllib2.Urlopen (Request).Read ()returnResponsedef getpminformation(content):#get per hour data    #print ContentDaydatakeyword=Re.Compiler "(? <=data:). *? (? =,\n) ") Daydataresults=Daydatakeyword.FindAll (content) [0] Daydataresults=Daydataresults.Replace"[","") Daydataresults=Daydataresults.Replace"]","") histries=Daydataresults.Split",")#print histries    #get Different types of dataTypesvaluekeyword=Re.Compiler "' (? <=<div class=" value ">\n). *? (? =\n </div>) ", re.Dotall) Typesvalueresults=Typesvaluekeyword.FindAll (content) [:-1] Typestitlekeyword=Re.Compiler "' (? <=<div class=" caption ">\n). *? (? =\n </div>) ", re.Dotall) Typestitleresults=Typestitlekeyword.FindAll (content)deltypestitleresults[-2]deltypestitleresults[-2] forIinch Range(Len(typestitleresults)): Typestitleresults[i]=Typestitleresults[i].Replace"/","_") Typestitleresults[i]=Typestitleresults[i].Replace".","_") Pmvalues[typestitleresults[i]]=Typesvalueresults[i]#print pmvalues    returnHistriesdef Createattrib(pmhistry): forKeyinchPmvalues.Iterkeys (): Self.Setparms ({Key:pmvalues[key]}) forIinch Range(Len(pmhistry)): Name= "Histry%d" %I Self.Setparms ({Name:pmhistry[i]})def Update(): City=Getcityname ()#print CityContent=Findurlcontent (city)#print ContentPmhistry=Getpminformation (content) Createattrib (pmhistry)#print "Done"Cityfileread.Close ()

The code has my own local path, which needs to be set differently for different people. And the code here is not strong, because it is based on a specific Web page to set the regular expression, so in order to read other pages that the above code is basically obsolete.

On the final panel button, write the Hou.pwd (). Hdamodule (). Update () to execute.

In fact, although this small plug-in can be used, but there are some small bugs are being debugged. The problem is that each time a new node is created, you cannot click Update directly, you need to open the type properties point and apply it. Otherwise you will get an error: ' Moudel ' has no attribute ' update '. The question I'm estimating is that when the script is loaded at build time or the Python module does not read its object when it is just built. Anyway still testing, if you know where the problem is, please tell me the message.

Houdini-Developing PM2.5 value reading plug-in with Python (1)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.