Last active
August 29, 2015 14:23
-
-
Save degtyarev-dm/bb7ccec94ac09eb40cf0 to your computer and use it in GitHub Desktop.
parse with regex, download with GET and parse for rust and perl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use utf8; | |
use v5.10; | |
use LWP::UserAgent; | |
use File::Slurp; | |
use Data::Dumper; | |
binmode(STDOUT,':utf8'); | |
my @city = get_city(); | |
my %city_hash; | |
my $ua = LWP::UserAgent->new; | |
my $i=0; | |
foreach(@city) | |
{ | |
my $content; | |
my $response = $ua->get('http://meteoinfo.ru/rss/forecasts/'.$_); | |
if ($response->is_success) { | |
$content=$response->decoded_content; # or whatever | |
} | |
else { | |
die $response->status_line; | |
} | |
if($content=~/title>(.*?)\,/) | |
{ | |
say( $_."=>".$1 ) ; | |
} | |
else | |
{ | |
say( $_."=>" ) ; | |
} | |
} | |
sub get_city | |
{ | |
my @content = read_file('tabfc4_1335.txt'); | |
my @city = (); | |
say $#content." lines."; | |
say 'date: '.$content[0]; | |
foreach(@content) | |
{ | |
chomp; | |
push(@city, $1) if(/(\d{5})/m); | |
} | |
return @city; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate hyper; | |
extern crate regex; | |
use std::io::Read; | |
use std::path::Path; | |
use std::fs::File; | |
use std::error::Error; | |
use std::io::BufReader; | |
use std::io::BufRead; | |
use std::vec::Vec; | |
use hyper::Client; | |
use hyper::header::Connection; | |
use regex::Regex; | |
fn main() { | |
let re = Regex::new(r"(\d{5})").unwrap(); | |
let path = Path::new("../tabfc4_1335.txt"); | |
let f = match File::open(&path) { | |
Err(e) => panic!("failed open {}: {}", path.display(), Error::description(&e)), | |
Ok(f) => f}; | |
let file = BufReader::new(&f); | |
let mut vec: Vec<String> = Vec::with_capacity(1335); | |
for ln in file.lines() { | |
let line = ln.unwrap(); | |
if re.is_match(&line) { | |
let result = re.captures(&line).unwrap().at(1).unwrap().clone(); | |
let elem = String::from(result); | |
vec.push(elem); | |
} | |
} | |
for elem in vec { | |
let city = match_city(&elem); | |
println!("{}=>{}",elem,city); | |
} | |
} | |
fn match_city(code: &String) -> String { | |
let re = Regex::new(r"title>(.*?),").unwrap(); | |
let client = Client::new(); | |
let mut result = String::new(); | |
let mut url = "http://meteoinfo.ru/rss/forecasts/".to_string() + &code; | |
let mut res = client.get(&*url).header(Connection::close()).send().unwrap(); | |
let mut body = String::new(); | |
res.read_to_string(&mut body).unwrap(); | |
if re.is_match(&body) { | |
let word = re.captures(&body).unwrap().at(1).unwrap(); | |
result.push_str(word); | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment