-
Notifications
You must be signed in to change notification settings - Fork 3
/
AntFundXueqiu.py
100 lines (80 loc) · 3.48 KB
/
AntFundXueqiu.py
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
#!/usr/bin/python3
# encoding: utf-8
"""
@author: Yancy Yue
@contact: [email protected]
@file: AntFundXueqiu.py
@time: 2018/1/20 22:10
@desc: Export fund records from Ant Fortune to xueqiu.com/performance
"""
import csv
import sys
from datetime import datetime
from math import floor
import requests
import XueqiuUtils
if __name__ == "__main__":
print('Python3 is expected.\nCurrent Version is:' + sys.version)
errorLines = 0
with open(XueqiuUtils.filename) as csv_file:
rows = csv.reader(csv_file)
rownumber = 0
XueqiuUtils.parseXueqiuCookie()
for row in rows:
# print(row)
# to read 15 lines in which the first 4 lines are useless and the 5th line is title.
if (rownumber >= XueqiuUtils.processTopNRecords):
exit()
rownumber += 1
fundName = ''
sumMoney = 0
if row != "": # add other needed checks to skip titles
# cols = row.split("','")
if (len(row) < 8):
continue
elif (len(row[8].split("-")) > 1):
fundName =row[8].split("-")[1]
print(fundName)
else:
continue
formData = XueqiuUtils.xq_formData
transfertime = row[2].strip()
# DO NOT "SAVE" the csv file in excel which will reformat the datetime fields.
transfertime = datetime.strptime(transfertime, '%Y-%m-%d %H:%M:%S').strftime('%Y-%m-%d')
# print(row[8])
print("Processing Row #" + str(rownumber) + ": " + fundName)
sumMoney = row[9].strip()
print('sumMoney:'+sumMoney)
if fundName in XueqiuUtils.fundList:
print("Code/Rate of \'" + fundName + "\' is " + XueqiuUtils.fundList[fundName][0] + "/" + str(
XueqiuUtils.fundList[fundName][1]))
else:
print(fundName + "not found in fundList.")
errorLines += 1
# trans data
formData['data[date]'] = transfertime
# fund code
formData['data[symbol]'] = XueqiuUtils.fundList[fundName][0]
# trans cost
commission = floor(float(sumMoney) * XueqiuUtils.fundList[fundName][1]*100)/100
formData['data[commission]'] = commission
# fund price on trans date
jjjz = XueqiuUtils.getpriceFromSina(formData['data[symbol]'], formData['data[date]'])
formData['data[price]'] = jjjz
# trans shares
shares = (float(sumMoney) - commission)/ jjjz
shares = floor(shares*100)/100
formData['data[shares]'] = shares
#
formData['data[comment]'] = 'AutoImport'
print(formData)
request = requests.post('https://xueqiu.com/service/poster',
data=formData,
headers=XueqiuUtils.xq_headerData,
cookies=XueqiuUtils.xq_cookieData)
if(request.status_code != 200):
print("ERROR!"+". Request return code:"+str(request.status_code))
exit(1)
else:
print('Return Code:',request.status_code)
print('request.result:', request.text)