#include "apue.h"
#include <netdb.h>
#include <arpa/inet.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <time.h>
/**
*Author: Li Yuan <liyuan462@gmail.com>
*Create Date: 2011-10-06
*Version: 0.1
*/
unsigned long downloaded = 0;
unsigned long total = 0;
double difftimeval(const struct timeval *tv1,
const struct timeval *tv2)
{
double d;
time_t s;
suseconds_t u;
s = tv1->tv_sec - tv2->tv_sec;
u = tv1->tv_usec - tv2->tv_usec;
d = s;
d *= 1000000.0;
d += u;
return d;
}
void *speed_thr(void *arg)
{
struct timeval tv_last, tv_this;
time_t s;
suseconds_t u;
int err;
unsigned long down_last, down_this;
double timediff;
if ((err = gettimeofday(&tv_last, NULL)) < 0) {
fprintf(stderr, "get time error: %s\n", strerror(errno));
exit(1);
}
down_last = downloaded;
printf("\033[s");
for (;;) {
if ((err = gettimeofday(&tv_this, NULL)) < 0) {
fprintf(stderr, "get time error: %s\n", strerror(errno));
exit(1);
}
timediff = difftimeval(&tv_this, &tv_last);
if (downloaded == total) {
printf("\033[u");
printf("\033[K");
printf("100%%\t%lu\t%.0fK/s\n", total,
(timediff/1000000.0) * (total-down_last) / 1024);
break;
}
if (timediff >= 1000000.0) {
down_this = downloaded;
printf("\033[u");
printf("\033[K");
printf("%.1f%%\t%lu\t%luK/s", 100*(float)down_this/total,
down_this, (down_this-down_last)/1024);
tv_last = tv_this;
down_last = down_this;
}
}
return((void *)0);
}
int resolve(char *src, char *web, char *file, char *local, int *port)
{
char *pa, *pb, *tp;
char *http = "http://";
if (!src || !(*src)) {
fprintf(stderr, "url resolve error: src is null\n");
return -1;
}
pa = src;
if (!strncmp(pa, http, strlen(http)))
pa += strlen(http);
else {
fprintf(stderr, "url resolve error: src is not a valid http url\n");
return -1;
}
pb = strchr(pa, '/');
if (pb) {
memcpy(web, pa, strlen(pa)-strlen(pb));
if (*(pb+1)) {
memcpy(file, pb+1, strlen(pb)-1);
for (tp = file+strlen(file)-1; tp !=file; tp--) {
if (*tp == '/')
break;
}
if (tp == file)
strcpy(local, file);
else
strcpy(local, tp+1);
} else {
fprintf(stderr, "url resolve error: no file exists\n");
return -1;
}
} else {
fprintf(stderr, "url resolve error: no file exists\n");
return -1;
}
pa = strchr(web, ':');
if (pa) {
*port = atoi(pa+1);
*pa = '\0';
}
return 0;
}
int main(int argc, char *argv[])
{
struct addrinfo *ailist, *aip;
struct addrinfo hint;
struct sockaddr_in *sinp;
const char *addr;
char server_name[256];
char server_file[1024];
char local_file[256];
int port, n, i, j, toread;
int fd, sockfd, err, speed_tid;
char abuf[INET_ADDRSTRLEN];
char request[1024];
char buffer[1024];
char line[1024];
char *pc;
if (argc != 2) {
fprintf(stderr, "usage: %s url\n", argv[0]);
exit(1);
}
memset(server_name, 0, sizeof(server_name));
memset(server_file, 0, sizeof(server_file));
memset(local_file, 0, sizeof(local_file));
port = 80;
setvbuf(stdout, NULL, _IONBF, 0);
if (resolve(argv[1], server_name, server_file, local_file, &port) < 0)
exit(1);
hint.ai_flags = AI_CANONNAME;
hint.ai_family = AF_INET;
hint.ai_socktype = SOCK_STREAM;
hint.ai_protocol = 0;
hint.ai_addrlen = 0;
hint.ai_canonname = NULL;
hint.ai_addr = NULL;
hint.ai_next = NULL;
printf("resolving the host %s... ", server_name);
if ((err = getaddrinfo(server_name, "www", &hint, &ailist)))
err_quit("getaddrinfo error: %s", gai_strerror(err));
aip = ailist;
sinp = (struct sockaddr_in *)aip->ai_addr;
addr = inet_ntop(AF_INET, &sinp->sin_addr, abuf, INET_ADDRSTRLEN);
if (!addr) {
fprintf(stderr, "fail to resolve\n");
exit(1);
}
printf("%s\n", addr);
printf("connecting %s|%s|:%d... ", server_name, addr, port);
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
fprintf(stderr, "can't connect to %s: %s\n",
server_name, strerror(errno));
exit(1);
}
if (connect(sockfd, aip->ai_addr, aip->ai_addrlen) < 0) {
fprintf(stderr, "can't connect to %s: %s\n",
server_name, strerror(errno));
exit(1);
}
printf("connected.\n");
sprintf(request, "GET /%s HTTP/1.0\r\nHost:%s\r\n\r\n",
server_file, server_name);
if (write(sockfd, request, strlen(request)) != strlen(request)) {
fprintf(stderr, "send error: %s\n", strerror(errno));
exit(1);
}
printf("http request sent, waiting for response... ");
toread = 1;
i = j = 0;
while ((n = read(sockfd, buffer, 1)) > 0) {
if (buffer[0] != '\r' && buffer[0] != '\n') {
i = 0;
line[j++] = buffer[0];
}
else {
i++;
if (i == 2 && buffer[0] == '\n') {
line[j] = '\0';
if (!strncmp(line, "HTTP", strlen("HTTP"))) {
pc = strchr(line, ' ');
printf("%s\n", pc+1);
if (strncmp(pc+1, "200", 3)) {
exit(1);
}
} else if (!strncmp(line, "Content-Length",
strlen("Content-Length"))) {
printf("%s\n", line);
pc = strchr(line, ' ');
total = atoi(pc+1);
} else if (!strncmp(line, "Content-Type",
strlen("Content-Type"))) {
printf("%s\n", line);
}
j = 0;
}
}
if (i == 4)
break;
}
printf("downloading %s\n", local_file);
if ((fd = creat(local_file, FILE_MODE)) < 0)
err_sys("creat error");
downloaded = 0;
err = pthread_create(&speed_tid, NULL, speed_thr, NULL);
if (err != 0)
err_quit("can't create speed thread: %s\n", strerror(err));
while ((n = read(sockfd, buffer, 1024)) > 0) {
if (write(fd, buffer, n) != n)
err_sys("write error");
downloaded += n;
}
if (n < 0)
err_sys("read error");
err = pthread_join(speed_tid, NULL);
if (err != 0)
err_quit("can't join with speed thread: %s\n", strerror(err));
printf("complete.\n");
exit(0);
}
|
#!/usr/bin/env python
import urllib2
import urllib
import cookielib
from HTMLParser import HTMLParser
import sys
import re
import json
import random
import getopt
import getpass
def usage():
print """Function: Visit others' home pages on www.renren.com
Usage: visitRenRen [-h] [-e email] [-p password]"""
class HomeParser(HTMLParser):
def __init__(self):
self.logoutLink = None
HTMLParser.__init__(self)
def handle_starttag(self, tag, attrs):
if tag != 'a': return
for key, value in attrs:
if key == 'href':
href = value
break
if 'Logout.do' in href:
self.logoutLink = href
def getLogoutLink(self):
return self.logoutLink
class FriendListParser():
def __init__(self):
self.content = None
self.friendList = []
def feed(self, content):
self.content = content
def fillFriendList(self):
p = re.compile('var friends=(.*);')
for line in self.content.split('\n'):
m = p.search(line)
if m:
break
else:
return
friendListJson = m.group(1)
friendListRaw = json.loads(friendListJson)
for friend in friendListRaw:
friendId = friend['id']
friendName = friend['name']
self.friendList.append((friendId, friendName))
def getFriendList(self):
return self.friendList
class VisitParser(HTMLParser):
def __init__(self, id):
self.statusLink = None
self.name = None
self.id = id
self.inName = False
self.validate = False
HTMLParser.__init__(self)
def handle_starttag(self, tag, attrs):
status = False
if tag == 'a':
for key, value in attrs:
if key == 'stats' and value == 'pf_tab_status':
status = True
if key == 'href':
href = value
if str(self.id) in href and status:
self.statusLink = href
if tag == 'label':
for key, value in attrs:
if key == 'for' and value == 'personalInfo5qValidateCode':
self.validate = True
if tag == 'h1':
for key, value in attrs:
if key == 'class' and "username" in value:
self.inName = True
def handle_data(self, data):
if self.inName:
self.name = data
self.inName = False
def getStatusLink(self):
return self.statusLink
def getValidate(self):
return self.validate
def getName(self):
return self.name
try:
opts, args = getopt.gnu_getopt(sys.argv[1:], "he:p:")
except getopt.GetoptError, err:
print str(err)
usage()
sys.exit()
email = None
password = None
for o, a in opts:
if o == "-h":
usage()
sys.exit()
elif o == "-e":
email = a
elif o == "-p":
password = a
if email is None:
email = raw_input('Email: ')
if password is None:
password = getpass.getpass()
cj = cookielib.LWPCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
#urllib2.install_opener(opener)
print "logging in..."
loginData = {
'email':email,
'password':password
}
loginData = urllib.urlencode(loginData)
f = opener.open('http://www.renren.com/PLogin.do', loginData)
parser = HomeParser()
parser.feed(f.read())
logged = parser.getLogoutLink()
if logged is None:
print "login fail"
sys.exit()
print "login succeed"
#for ind, cookie in enumerate(cj):
# print "%d-%s" % (ind, cookie)
#cj.save("cookies.dat")
#f = opener.open('http://friend.renren.com/myfriendlistx.do')
#parser = FriendListParser()
#parser.feed(f.read())
#parser.fillFriendList()
#friendList = parser.getFriendList()
#print "total amount of friends:", len(friendList)
#
#toVisitList = random.sample(friendList, len(friendList))
#lenSucceed = 0
#lenFail = 0
#failedOnce = False
#for id, name in toVisitList:
# f = opener.open('http://www.renren.com/profile.do?\
#portal=homeFootprint&ref=home_footprint&id=' + str(id))
# encoding = f.info().getparam('charset')
# page = f.read().decode(encoding)
# parser = VisitParser(id)
# parser.feed(page)
# visited = parser.getStatusLink()
# if visited is None:
# print "visit " + str(id) + " " + name + " fail"
# lenFail += 1
# if failedOnce:
# break
# failedOnce = True
# else:
# print "visit " + str(id) + " " + name + " succeed"
# lenSucceed += 1
# failedOnce = False
#print "visit:", str(lenFail+lenSucceed)
#print "succeed:", str(lenSucceed)
#print "fail:", str(lenFail)
baseId = 222000000
visitCount = 0
while True:
offset = random.randint(0, 999999)
realId = baseId + offset
f = opener.open('http://www.renren.com/profile.do?\
portal=homeFootprint&ref=home_footprint&id=' + str(realId))
encoding = f.info().getparam('charset')
page = f.read().decode(encoding)
parser = VisitParser(realId)
parser.feed(page)
visited = parser.getStatusLink()
validate = parser.getValidate()
name = parser.getName()
if validate:
print "visit enough pages. wait and try again."
break
if visited is None:
continue
print "visit", str(realId), name, "succeed"
visitCount += 1
print "you have successfully visited", str(visitCount), "persons' home pages"
|