Guest User

Untitled

a guest
Jan 5th, 2023
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. import requests
  2. import json
  3. import time
  4. import csv
  5. import pandas
  6.  
  7. start=250
  8.  
  9. with open('C:/Users/apskaita3/Desktop/number2.txt', "r") as f:
  10.  
  11. start= f.readlines()
  12.  
  13. start=int(start[0])
  14.  
  15. start=start + 70
  16.  
  17.  
  18. results = {"item": {}}
  19.  
  20. # Todo load json
  21. for i in range(0,9800): #<----- Just change range here to increase number of requests
  22. URL = f"https://api.news.eu.nasdaq.com/news/query.action?type=handleResponse&showAttachments=true&showCnsSpecific=true&showCompany=true&countResults=false&freeText=&company=&market=Main%20Market%2C+Helsinki&cnscategory=&fromDate=&toDate=&globalGroup=exchangeNotice&globalName=NordicMainMarkets&displayLanguage=en&language=en&timeZone=CET&dateMask=yyyy-MM-dd+HH%3Amm%3Ass&limit=50000000&start={i}&dir=ASC"
  23. r = requests.get(url = URL)
  24. #time.sleep(1)
  25. res = r.text.replace("handleResponse(", "")
  26. #print(res)
  27. #print(f'r is {r}')
  28. res_json = json.loads(res)
  29. #print(res_json)
  30. data = res_json
  31. a=i+1
  32. #print(data)
  33. print("Doing: " + str(i + 1) + "th")
  34. #data = r.json()
  35.  
  36. downloaded_entries = data["results"]["item"]
  37. new_entries = [d for d in downloaded_entries if d["headline"] not in results["item"]]
  38. start=str(start)
  39.  
  40. for entry in new_entries:
  41. if entry["market"] == 'Main Market, Helsinki' and entry["published"]>="2021-10-20 06:30:00":
  42. headline = entry["headline"].strip()
  43. published = entry["published"]
  44. market=entry["market"]
  45. market="Main Market, Helsinki"
  46. results["item"][headline] = {"company": entry["company"], "messageUrl": entry["messageUrl"], "published": entry["published"], "headline": headline}
  47. print(entry['market'])
  48. #time.sleep(5)
  49. print(f"Market: {market}/nDate: {published}/n")
  50. #print( results["item"][headline] )
  51. #print(results)
  52. #print(json.dumps({"item": list(results["item"].values())}, indent = 4))
  53.  
  54. with open("C:/Users/apskaita3/Finansų analizės ir valdymo sprendimai, UAB/Rokas Toomsalu - Power BI analitika/Integracijos/1_Public comapnies analytics/Databasesets/Others/market_news_helsinki.json", "w") as outfile:
  55. json_object = json.dumps({"item": list(results["item"].values())}, indent = 4)
  56. outfile.write(json_object)
  57. #print(json_object)
  58. with open("C:/Users/apskaita3/Desktop/number2.txt", "w") as outfile1:
  59. outfile1.write(start) # type: ignore
  60.  
  61. """""
  62. with open('C:/Users/apskaita3/Finansų analizės ir valdymo sprendimai, UAB/Rokas Toomsalu - Power BI analitika/Integracijos/1_Public comapnies analytics/Databasesets/Others/market_news_helsinki.json') as json_file:
  63. data = json.load(json_file)
  64.  
  65.  
  66. employee_data = data['item']
  67.  
  68.  
  69.  
  70. data_file = open("C:/Users/apskaita3/Desktop/data_file9.csv", 'a',newline='', encoding="utf-8")
  71.  
  72.  
  73. csv_writer = csv.writer(data_file)
  74.  
  75.  
  76. count=0
  77.  
  78. for emp in employee_data:
  79. if count == 0:
  80.  
  81. # Writing headers of CSV file
  82. header = emp.keys()
  83. csv_writer.writerow(header)
  84. count += 1
  85.  
  86. print(emp.values())
  87. # Writing data of CSV file
  88. csv_writer.writerow(emp.values())
  89.  
  90. data_file.close()
  91.  
  92.  
  93. csvFile = pandas.read_csv('C:/Users/apskaita3/Desktop/data_file9.csv', sep=',',encoding="utf-8")
  94.  
  95. csvFile=csvFile.drop_duplicates()
  96. #print(csvFile.iloc[0])
  97. csvFile.to_csv("C:/Users/apskaita3/Desktop/data_file15.csv",index = False)
  98.  
  99.  
  100.  
  101. with open("C:/Users/apskaita3/Desktop/data_file15.csv","r",encoding="utf-8") as inputfile, open("C:/Users/apskaita3/Desktop/data_file16.csv","a",encoding="utf-8") as outputfile:
  102. lines = inputfile.readlines()
  103. outputfile.write(lines[0])
  104. for i in range(1, len(lines)):
  105. if lines[i] != lines[0]:
  106. outputfile.write(lines[i])
  107.  
  108.  
  109. csvFile1 = pandas.read_csv('C:/Users/apskaita3/Desktop/data_file9.csv', sep=',',encoding="utf-8")
  110. csvFile1=csvFile.drop_duplicates()
  111. csvFile1 = csvFile1[csvFile1.company != "company"]
  112. csvFile1.to_csv("C:/Users/apskaita3/Desktop/data_file16.csv",index = False)
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119. # Function to convert a CSV to JSON
  120. # Takes the file paths as arguments
  121. def make_json(csvFilePath, jsonFilePath):
  122.  
  123. # create a dictionary
  124. data = {}
  125.  
  126. # Open a csv reader called DictReader
  127. with open(csvFilePath, encoding='utf-8') as csvf:
  128. csvReader = csv.DictReader(csvf)
  129.  
  130. # Convert each row into a dictionary
  131. # and add it to data
  132. for rows in csvReader:
  133.  
  134. # Assuming a column named 'No' to
  135. # be the primary key
  136. key = rows['headline']
  137. data[key] = rows
  138.  
  139. # Open a json writer, and use the json.dumps()
  140. # function to dump data
  141. with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
  142. jsonf.write(json.dumps(data, indent=4))
  143.  
  144. # Driver Code
  145.  
  146. # Decide the two file paths according to your
  147. # computer system
  148. csvFilePath = r'C:/Users/apskaita3/Desktop/data_file16.csv'
  149. jsonFilePath = r'C:/Users/apskaita3/Finansų analizės ir valdymo sprendimai, UAB/Rokas Toomsalu - Power BI analitika/Integracijos/1_Public comapnies analytics/Databasesets/Others/market_news_helsinki.json'
  150.  
  151. # Call the make_json function
  152. make_json(csvFilePath, jsonFilePath)
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171. #input("Press")
  172. #print(json_object)
  173. """
Add Comment
Please, Sign In to add comment