-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphoto.js
139 lines (110 loc) · 4.96 KB
/
photo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const path = require('path')
const cheerio = require('cheerio')
const Request = require('axer').request
const file = require('axer').file
const config = require('./config')
async function getPhoto(album) {
const cookieJarPath = path.resolve(config.storageDir, 'cookieJar.json')
const request = new Request(cookieJarPath)
const photoCount = album.photoCount
// 获取相册内照片连接
const albumResponse = await request.get(`http://photo.renren.com/photo/${album.ownerId}/album-${album.albumId}/v7`)
const regex = /photo = ((.|\n)*|);\n;/g
const m = regex.exec(albumResponse.body)
const photoListContent = m && m[1]
const photoListInfo = JSON.parse(photoListContent.replace(/'/g, '"')).photoList
// console.log(JSON.stringify(photoListInfo, null, 2))
var pageCount = 2
while (photoListInfo.photoList.length < photoCount) {
pageCount++
const albumResponseNext = await request.get(`http://photo.renren.com/photo/${album.ownerId}/album-${album.albumId}/bypage/ajax/v7?page=${pageCount}&pageSize=20`)
// console.log('------------------------------------------------')
// console.log(pageCount)
// console.log(albumResponseNext)
// console.log('------------------------------------------------')
const photoListNext = JSON.parse(albumResponseNext.body.replace(/'/g, '"')).photoList
// console.log('------------------------------------------------')
// console.log(photoListNext)
// console.log('------------------------------------------------')
photoListInfo.photoList = photoListInfo.photoList.concat(photoListNext)
// console.log('------------------------------------------------')
// console.log(pageCount)
// console.log(photoListInfo.photoList.length)
// console.log('------------------------------------------------')
delete albumResponseNext
delete photoListNext
}
// console.log('-----------------------------------------------------------------------------')
// console.log(album.albumName)
// console.log(album.photoCount)
// console.log(photoListInfo.photoList.length)
// console.log('-----------------------------------------------------------------------------')
for (let i = 0; i < photoListInfo.photoList.length; i++) {
const photo = photoListInfo.photoList[i]
// 创建相册目录
const albumPath = path.resolve(config.storageDir, `${album.ownerId}`, album.albumName)
await file.mkdir(albumPath)
const hdPhotoUrl = photo.url
var position = 0;
while (hdPhotoUrl[position] != '/' || hdPhotoUrl[position+1] != '2' || hdPhotoUrl[position+2] != '0')
position++
var photoName = hdPhotoUrl.slice(position+1,position+9)+"_"+hdPhotoUrl.slice(position+10,position+14) + "_" + photo.photoId
// console.log(hdPhotoUrl)
// console.log(photoName)
const originalPhotoUrl = hdPhotoUrl.replace('large', 'original')
const photoSavePath = path.resolve(albumPath, `${photoName}.jpg`)
// 下载照片
try {
await request.download(originalPhotoUrl, photoSavePath)
const checkContent = await file.cat(photoSavePath)
if (checkContent.indexOf('DOCTYPE') > 0) {
console.log('不存在原图,开始下载高清图')
await request.download(hdPhotoUrl, photoSavePath)
}
} catch (error) {
console.log('下载出现错误,将再次尝试一次下载')
await request.download(originalPhotoUrl, photoSavePath)
const checkContent = await file.cat(photoSavePath)
if (checkContent.indexOf('DOCTYPE') > 0) {
console.log('不存在原图,开始下载高清图')
await request.download(hdPhotoUrl, photoSavePath)
}
}
}
}
async function getAlbum(ownerId) {
const cookieJarPath = path.resolve(config.storageDir, 'cookieJar.json')
const request = new Request(cookieJarPath)
// 获取相册详情
const albumlistResponse = await request.get(`http://photo.renren.com/photo/${ownerId}/albumlist/v7`)
const regex = /photo = ((.|\n)*|);\nnx/g
const m = regex.exec(albumlistResponse.body)
const albumlistContent = m && m[1]
const albumlistInfo = JSON.parse(albumlistContent.replace(/'/g, '"'))
// console.log(JSON.stringify(albumlistInfo, null, 2))
for(let i = 0; i < albumlistInfo.albumList.albumList.length; i++) {
const album = albumlistInfo.albumList.albumList[i]
// 获取相册内到照片
try {
await getPhoto(album)
} catch (error) {
console.log('获取照片失败', error)
}
}
}
async function getSelfAlbum() {
const cookieJarPath = path.resolve(config.storageDir, 'cookieJar.json')
const request = new Request(cookieJarPath)
const homeResponse = await request.get('http://www.renren.com')
const $ = cheerio.load(homeResponse.body)
// 运行脚本获取数据并删除污染变量
let nxcontent = $('script').first().text()
// console.log(homeResponse.body)
eval(nxcontent)
const user = nx.user
delete nx
console.log(user)
await getAlbum(user.id)
}
exports.getSelfAlbum = getSelfAlbum
exports.getAlbum = getAlbum