Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def writing_to_file(pack_of_news, pack_of_news_for_db, filename):
- """
- Writing information into 2 files: News_cache.txt and News_cache_json.json
- Creating 2 files because it's easier to read information into computer from JSON file than another file
- 1. Open file
- 2. Check if file is empty or not
- 3. If empty - append whole information
- If Not empty:
- 1. Check if the novelty in file
- 2. If in file: continue
- If Not in the file: Find out length of file (amount of news), put that number to incoming novelty
- If that novelty exists it will go to another novelty in limit
- It means that if you enter --limit 15 and 10 news are already in list it will add only 5 news to list
- """
- conn = sqlite3.connect("database.db")
- cursor = conn.cursor()
- cursor.execute('create table if not exists projects(num integer, title text, time text, source_link text, '
- 'description text, images_links text, alt_tx text, date_corrected integer, main_source text)')
- conn.commit()
- logging.info("Opening file News_cache.")
- with open(filename, 'a', encoding='utf-8') as news_cache:
- logging.info("Reading from News_cache.")
- content = reading_file('News_cache.txt')
- if not content:
- if cursor.execute("SELECT * FROM projects"):
- cursor.execute("DELETE FROM projects")
- conn.commit()
- for num, item in enumerate(pack_of_news):
- logging.info("Writing into file if it was empty.")
- news_cache.write(getting_info_into_file(item))
- news_cache.write("\n_ _ _")
- cursor.execute('insert into projects values (?,?,?,?,?,?,?,?,?)', pack_of_news_for_db[num])
- conn.commit()
- else:
- for number, item in enumerate(pack_of_news):
- if item.source_link in content:
- continue
- else:
- length = sum(1 for line in cursor.execute("SELECT * FROM projects")) + 1
- logging.info("Counting lines.")
- logging.info("Counted lines.")
- item.number_of_novelty = length
- logging.info("Writing into file if it was not empty.")
- news_cache.write(getting_info_into_file(item))
- news_cache.write("\n_ _ _")
- cursor.execute('insert into projects values (?,?,?,?,?,?,?,?,?)', pack_of_news_for_db[number])
- conn.commit()
- conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement